Skip to content

Commit 784487b

Browse files
committed
API keys management
1 parent d48b5c0 commit 784487b

File tree

6 files changed

+687
-0
lines changed

6 files changed

+687
-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: 51 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,52 @@ 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+
) -> Dict[str, str]:
2470+
"""Creates a new API key.
2471+
2472+
Args:
2473+
name (str): The name of the API key.
2474+
user (Union[User, str]): The user object or user ID to associate with the API key.
2475+
role (Union[Role, str]): The role object or role ID to assign to the API key.
2476+
validity (int, optional): The validity period of the API key. Defaults to 0 (no expiration).
2477+
time_unit (TimeUnit, optional): The time unit for the validity period. Defaults to TimeUnit.SECOND.
2478+
2479+
Returns:
2480+
Dict[str, str]: A dictionary containing the created API key information.
2481+
"""
2482+
warnings.warn(
2483+
"The creation of API keys is currently in alpha and its behavior may change in future releases.",
2484+
)
2485+
return ApiKey.create_api_key(
2486+
self, name, user, role, validity, time_unit
2487+
)
2488+
2489+
def get_api_keys(self, include_expired: bool = False) -> List[ApiKey]:
2490+
"""Retrieves all API keys accessible to the current user.
2491+
2492+
Args:
2493+
include_revoked (bool, optional): Whether to include revoked API keys. Defaults to True.
2494+
2495+
Returns:
2496+
List[ApiKey]: A list of ApiKey objects.
2497+
"""
2498+
return ApiKey.get_api_keys(self, include_expired)
2499+
2500+
def get_api_key(self, api_key_id: str) -> Optional[ApiKey]:
2501+
"""Retrieves a single API key by its ID.
2502+
2503+
Args:
2504+
api_key_id (str): The unique ID of the API key.
2505+
2506+
Returns:
2507+
Optional[ApiKey]: The corresponding ApiKey object if found, otherwise None.
2508+
"""
2509+
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)