-
Notifications
You must be signed in to change notification settings - Fork 37
Automatically check whether API key is valid #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The OA team provided an API key; see #80 for the update. |
Great! |
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. |
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
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.
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, butAlexConfig
is defined beforeWorks