|
80 | 80 | from labelbox.schema.task import DataUpsertTask, Task
|
81 | 81 | from labelbox.schema.user import User
|
82 | 82 | from labelbox.schema.taskstatus import TaskStatus
|
| 83 | +from labelbox.schema.api_key import ApiKey |
| 84 | +from labelbox.schema.timeunit import TimeUnit |
83 | 85 |
|
84 | 86 | logger = logging.getLogger(__name__)
|
85 | 87 |
|
@@ -2456,3 +2458,61 @@ def cancel_task(self, task_id: str) -> bool:
|
2456 | 2458 | """
|
2457 | 2459 | res = self.execute(mutation_str, {"id": task_id})
|
2458 | 2460 | 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) |
0 commit comments