Skip to content

Conversation

raffaem
Copy link
Contributor

@raffaem raffaem commented Jun 15, 2025

It is not extremely clean but is the cleanest approach I could come up with

The problem is that to check the API key we need Works to be defined, but AlexConfig is defined before Works

@J535D165
Copy link
Owner

The OA team provided an API key; see #80 for the update.

@raffaem
Copy link
Contributor Author

raffaem commented Jun 17, 2025

The OA team provided an API key; see #80 for the update.

Great!

@raffaem
Copy link
Contributor Author

raffaem commented Jun 18, 2025

The OA team provided an API key; see #80 for the update.

THis PR is still valid to check the user's API, right? Or am I missing something in your changes?

else:
logging.info("API key is working 👍")
```
pyalex will automatically check whether your API key is valid and throw a `ValueError` if it isn't.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, OpenAlex should check this server-side and raise an error if the key is not valid. I propose to file an issue in their issue tracker.

I propose to remove this logic here because it's implicit, which is not very Pythonic.

Comment on lines +1106 to +1124
def _check_api_key():
"""Check if the API key is valid."""
bk_cods = config.retry_http_codes
config.retry_http_codes = None
dt = f"{datetime.datetime.now().year}-01-01"
res = None
try:
Works().filter(from_updated_date=dt).get()
except requests.exceptions.HTTPError as e:
if e.response.status_code == 403:
res = False
else:
logging.error(f"Unexpected HTTP error: {e}")
raise
else:
res = True
finally:
config.retry_http_codes = bk_cods
return res
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this function. I propose the following API:

from pyalex.utils import check_api_key

check_api_key()
# raises ValueError if incorrect, returns None if valid. 

This way, we separate our custom logics from the pure wrapper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants