Skip to content

Commit 3027b8b

Browse files
authored
API keys Management (#1961)
2 parents d48b5c0 + 5458071 commit 3027b8b

File tree

6 files changed

+733
-0
lines changed

6 files changed

+733
-0
lines changed

libs/labelbox/src/labelbox/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,5 @@
9999
)
100100
from lbox.exceptions import *
101101
from labelbox.schema.taskstatus import TaskStatus
102+
from labelbox.schema.api_key import ApiKey
103+
from labelbox.schema.timeunit import TimeUnit

libs/labelbox/src/labelbox/client.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
from labelbox.schema.task import DataUpsertTask, Task
8181
from labelbox.schema.user import User
8282
from labelbox.schema.taskstatus import TaskStatus
83+
from labelbox.schema.api_key import ApiKey
84+
from labelbox.schema.timeunit import TimeUnit
8385

8486
logger = logging.getLogger(__name__)
8587

@@ -2456,3 +2458,61 @@ def cancel_task(self, task_id: str) -> bool:
24562458
"""
24572459
res = self.execute(mutation_str, {"id": task_id})
24582460
return res["cancelBulkOperationJob"]["success"]
2461+
2462+
def create_api_key(
2463+
self,
2464+
name: str,
2465+
user: Union[User, str],
2466+
role: Union[Role, str],
2467+
validity: int = 0,
2468+
time_unit: TimeUnit = TimeUnit.SECOND,
2469+
refresh_cache: bool = False,
2470+
) -> Dict[str, str]:
2471+
"""Creates a new API key.
2472+
2473+
Args:
2474+
name (str): The name of the API key.
2475+
user (Union[User, str]): The user object or user ID to associate with the API key.
2476+
role (Union[Role, str]): The role object or role ID to assign to the API key.
2477+
validity (int, optional): The validity period of the API key. Defaults to 0 (no expiration).
2478+
time_unit (TimeUnit, optional): The time unit for the validity period. Defaults to TimeUnit.SECOND.
2479+
refresh_cache (bool, optional): Whether to refresh cached permissions and roles. Defaults to False.
2480+
2481+
Returns:
2482+
Dict[str, str]: A dictionary containing the created API key information.
2483+
"""
2484+
warnings.warn(
2485+
"The creation of API keys is currently in alpha and its behavior may change in future releases.",
2486+
)
2487+
if refresh_cache:
2488+
# Clear cached attributes if they exist
2489+
if hasattr(self, "_cached_current_user_permissions"):
2490+
delattr(self, "_cached_current_user_permissions")
2491+
if hasattr(self, "_cached_available_api_key_roles"):
2492+
delattr(self, "_cached_available_api_key_roles")
2493+
2494+
return ApiKey.create_api_key(
2495+
self, name, user, role, validity, time_unit
2496+
)
2497+
2498+
def get_api_keys(self, include_expired: bool = False) -> List[ApiKey]:
2499+
"""Retrieves all API keys accessible to the current user.
2500+
2501+
Args:
2502+
include_revoked (bool, optional): Whether to include revoked API keys. Defaults to True.
2503+
2504+
Returns:
2505+
List[ApiKey]: A list of ApiKey objects.
2506+
"""
2507+
return ApiKey.get_api_keys(self, include_expired)
2508+
2509+
def get_api_key(self, api_key_id: str) -> Optional[ApiKey]:
2510+
"""Retrieves a single API key by its ID.
2511+
2512+
Args:
2513+
api_key_id (str): The unique ID of the API key.
2514+
2515+
Returns:
2516+
Optional[ApiKey]: The corresponding ApiKey object if found, otherwise None.
2517+
"""
2518+
return ApiKey.get_api_key(self, api_key_id)

libs/labelbox/src/labelbox/schema/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@
2727
import labelbox.schema.ontology_kind
2828
import labelbox.schema.project_overview
2929
import labelbox.schema.taskstatus
30+
import labelbox.schema.api_key
31+
import labelbox.schema.timeunit

0 commit comments

Comments
 (0)