11from datetime import datetime , timezone
2- from typing import Any , Dict , List , Optional , Union
2+ from typing import Any , Dict , List , Optional , Union , TYPE_CHECKING
33import logging
44
55from labelbox .orm .db_object import DbObject
1010from labelbox .schema .user import User
1111from labelbox .schema .role import Role
1212
13+ if TYPE_CHECKING :
14+ from labelbox import Client
15+
1316logger = logging .getLogger (__name__ )
1417
1518
@@ -115,8 +118,15 @@ def revoke(self) -> Dict[str, Any]:
115118 return result
116119
117120 @staticmethod
118- def _get_current_user_permissions (client ) -> List [str ]:
119- """Retrieve current user permissions from the client's organization role with caching."""
121+ def _get_current_user_permissions (client : "Client" ) -> List [str ]:
122+ """Retrieve current user permissions from the client's organization role with caching.
123+
124+ Args:
125+ client: The Labelbox client instance.
126+
127+ Returns:
128+ List[str]: A list of permission strings associated with the current user's role.
129+ """
120130 if hasattr (client , "_cached_current_user_permissions" ):
121131 return client ._cached_current_user_permissions
122132
@@ -135,53 +145,9 @@ def _get_current_user_permissions(client) -> List[str]:
135145 return perms
136146
137147 @staticmethod
138- def get_api_key (client , api_key_id : str ) -> Optional ["ApiKey" ]:
139- """Retrieves a single API key by its ID.
140-
141- Args:
142- client: The Labelbox client instance.
143- api_key_id (str): The unique ID of the API key.
144-
145- Returns:
146- Optional[ApiKey]: The corresponding ApiKey object if found, otherwise None.
147- """
148- query = """
149- query GetApiKeyPyApi($id: ID!) {
150- apiKey(where: {id: $id}) {
151- id
152- name
153- key
154- createdAt
155- expiresAt
156- revoked
157- user {
158- id
159- email
160- }
161- role {
162- id
163- name
164- permissions
165- }
166- }
167- }
168- """
169-
170- try :
171- result = client .execute (query , {"id" : api_key_id })
172-
173- if not result or "apiKey" not in result or not result ["apiKey" ]:
174- return None
175-
176- api_key_data = result ["apiKey" ]
177- return ApiKey (client , api_key_data )
178-
179- except Exception as e :
180- raise LabelboxError (f"Failed to retrieve API key: { str (e )} " )
181- return None
182-
183- @staticmethod
184- def get_api_keys (client , include_expired : bool = False ) -> List ["ApiKey" ]:
148+ def get_api_keys (
149+ client : "Client" , include_expired : bool = False
150+ ) -> List ["ApiKey" ]:
185151 """Retrieves all API keys accessible to the current user using the provided client.
186152
187153 Args:
@@ -244,8 +210,22 @@ def get_api_keys(client, include_expired: bool = False) -> List["ApiKey"]:
244210 return all_keys
245211
246212 @staticmethod
247- def _get_available_api_key_roles (client ) -> List [str ]:
248- """Get the list of built-in roles available for API key creation with caching."""
213+ def _get_available_api_key_roles (client : "Client" ) -> List [str ]:
214+ """Get the list of built-in roles available for API key creation with caching.
215+
216+ This method retrieves all roles available in the organization and filters them
217+ based on the current user's permissions. The results are cached on the client
218+ to avoid redundant API calls.
219+
220+ Args:
221+ client: The Labelbox client instance.
222+
223+ Returns:
224+ List[str]: A list of role names that can be assigned to API keys.
225+
226+ Raises:
227+ LabelboxError: If there's an error retrieving the user permissions.
228+ """
249229 if hasattr (client , "_cached_available_api_key_roles" ):
250230 return client ._cached_available_api_key_roles
251231 try :
@@ -275,7 +255,7 @@ def _get_available_api_key_roles(client) -> List[str]:
275255 return available_roles
276256
277257 @staticmethod
278- def _get_user (client , email : str ) -> Optional [str ]:
258+ def _get_user (client : "Client" , email : str ) -> Optional [str ]:
279259 """Checks if a user with the given email exists in the organization.
280260
281261 Args:
@@ -301,7 +281,7 @@ def _get_user(client, email: str) -> Optional[str]:
301281
302282 @staticmethod
303283 def create_api_key (
304- client ,
284+ client : "Client" ,
305285 name : str ,
306286 user : Union ["User" , str ],
307287 role : Union ["Role" , str ],
@@ -319,7 +299,7 @@ def create_api_key(
319299 time_unit (TimeUnit, optional): Time unit for validity period. Defaults to TimeUnit.SECOND.
320300
321301 Returns:
322- Dict[str, Any ]: Dictionary containing the created API key details including id and jwt
302+ Dict[str, str ]: Dictionary containing the created API key details including id and jwt
323303
324304 Raises:
325305 ValueError: If invalid parameters are provided
@@ -412,7 +392,7 @@ def create_api_key(
412392 raise LabelboxError (error_message ) from e
413393
414394 @staticmethod
415- def get_api_key (client , api_key_id : str ) -> Optional ["ApiKey" ]:
395+ def get_api_key (client : "Client" , api_key_id : str ) -> Optional ["ApiKey" ]:
416396 """Retrieves a single API key by its ID using the provided client.
417397
418398 Args:
0 commit comments