-
Notifications
You must be signed in to change notification settings - Fork 68
API keys Management #1961
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
API keys Management #1961
Conversation
917d43b
to
784487b
Compare
user_id = ApiKey._get_user(client, user_email) | ||
if not user_id: | ||
raise ValueError( | ||
f"User with email '{user_email}' does not exist in the organization" | ||
) | ||
|
||
role_name = role.name if hasattr(role, "name") else role | ||
if not role_name or not isinstance(role_name, str): | ||
raise ValueError("role must be a Role object or a valid role name") | ||
|
||
allowed_roles = ApiKey._get_available_api_key_roles(client) | ||
if role_name not in allowed_roles: | ||
raise ValueError( | ||
f"Invalid role specified. Allowed roles are: {allowed_roles}" | ||
) |
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.
are these checks really necessary? I would imagine that backend already has these checks in place and client.execute()
would just return appropriate error. Saving client extra bandwidth
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.
if validity > 0: | ||
if not isinstance(time_unit, TimeUnit): | ||
raise ValueError( | ||
"time_unit must be a valid TimeUnit enum value" | ||
) | ||
|
||
validity_seconds = validity * time_unit.value | ||
|
||
if validity_seconds < TimeUnit.MINUTE.value: | ||
raise ValueError("Minimum validity period is 1 minute") | ||
|
||
max_seconds = 25 * TimeUnit.WEEK.value | ||
if validity_seconds > max_seconds: | ||
raise ValueError( | ||
"Maximum validity period is 6 months (or 25 weeks)" | ||
) | ||
else: | ||
raise ValueError("validity must be a positive integer") |
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.
could just flip logic here and get rid of nested ifs for better code readability. Otherwise, too much mental gymnastic to perform for simple logic
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.
We still support 3.9. Otherwise, I would use pattern matching to simplify the structure for the bunch of tests.
I will update the test for "validity".
try: | ||
result = client.execute(query_str, params) | ||
api_key_result = result.get("createApiKey") | ||
|
||
if not api_key_result: | ||
raise LabelboxError( | ||
"Failed to create API key. No data returned from the server." | ||
) | ||
|
||
return api_key_result | ||
|
||
except Exception as e: | ||
if ( | ||
"permission" in str(e).lower() | ||
or "unauthorized" in str(e).lower() | ||
): | ||
raise LabelboxError( | ||
f"Permission denied: You don't have sufficient permissions to create API keys. Original error: {str(e)}" | ||
) | ||
else: | ||
error_message = f"Failed to create API key: {str(e)}" | ||
logger.error(error_message) | ||
raise LabelboxError(error_message) from e |
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.
here again I think most of that already handled by .execute()
method and backend. It performs lots of extra checks and throws errors. So i think it is fine to just call client.execute()
and return necessary data. But obviously should check before though 😬
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.
The exception handling was simplified.
4fe4841
to
4bedd10
Compare
2a9cad0
to
92d7a94
Compare
d0e87e5
to
5458071
Compare
) | ||
|
||
validity_seconds = 0 | ||
if validity < 0: |
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.
you probably wanna have <=
here. Otherwise 0
is a valid expiration value
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.
It will caught by another test:
if validity_seconds < TimeUnit.MINUTE.value:
raise ValueError("Minimum validity period is 1 minute")
```
try: | ||
result = client.execute(query_str, params) | ||
api_key_result = result.get("createApiKey") | ||
|
||
if not api_key_result: | ||
raise LabelboxError( | ||
"Failed to create API key. No data returned from the server." | ||
) | ||
|
||
return api_key_result | ||
|
||
except Exception as e: | ||
raise LabelboxError(str(e)) from e |
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 think .execute()
would already throw error here if something is not right. So you could simplified this further by removing try/catch block.
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.
There are 2 things:
execute()
didn't manage to create an API key and this justifies to raise an exception (execute wouldn't fail)execute()
threw an exception and I want it to be propagated.
Description
Initially, this was a critical feature request for Optum.
The core feature is to create API keys for different users.
Notes
Client
only contains wrappers for static methods inApiKey
.client.create_api_key()
shows a warning to emphasize its alpha status.This PR brings the following changes to the SDK:
Classes
ApiKey
: A class to describe API keysEnum
TimeUnit
: An enum to describe the validation time (number of seconds) of an API key as seen in the UIFunctions
Example:
Retrieve API keys (one/all)
Get one API key
Revoke an API key
Fixes # (issue)
Type of change
Please delete options that are not relevant.
All Submissions
New Feature Submissions
Changes to Core Features