|
| 1 | +from typing import Protocol |
| 2 | + |
| 3 | +from workos.types.api_keys import ApiKey |
| 4 | +from workos.typing.sync_or_async import SyncOrAsync |
| 5 | +from workos.utils.http_client import AsyncHTTPClient, SyncHTTPClient |
| 6 | +from workos.utils.request_helper import REQUEST_METHOD_POST |
| 7 | + |
| 8 | + |
| 9 | +class ApiKeyModule(Protocol): |
| 10 | + def validate_api_key(self) -> SyncOrAsync[ApiKey]: |
| 11 | + """Validates the configured API key. |
| 12 | +
|
| 13 | + Returns: |
| 14 | + ApiKey: The validated API key details containing |
| 15 | + information about the key's name and usage |
| 16 | +
|
| 17 | + Raises: |
| 18 | + AuthenticationException: If the API key is invalid or |
| 19 | + unauthorized (401) |
| 20 | + NotFoundException: If the API key is not found (404) |
| 21 | + ServerException: If the API server encounters an error |
| 22 | + (5xx) |
| 23 | + """ |
| 24 | + ... |
| 25 | + |
| 26 | + |
| 27 | +class ApiKey(ApiKeyModule): |
| 28 | + _http_client: SyncHTTPClient |
| 29 | + |
| 30 | + def __init__(self, http_client: SyncHTTPClient): |
| 31 | + self._http_client = http_client |
| 32 | + |
| 33 | + def validate_api_key(self) -> ApiKey: |
| 34 | + response = self._http_client.request( |
| 35 | + "api_keys/validate", |
| 36 | + method=REQUEST_METHOD_POST, |
| 37 | + ) |
| 38 | + |
| 39 | + return ApiKey.model_validate(response) |
| 40 | + |
| 41 | + |
| 42 | +class AsyncApiKey(ApiKeyModule): |
| 43 | + _http_client: AsyncHTTPClient |
| 44 | + |
| 45 | + def __init__(self, http_client: AsyncHTTPClient): |
| 46 | + self._http_client = http_client |
| 47 | + |
| 48 | + async def validate_api_key(self) -> ApiKey: |
| 49 | + response = await self._http_client.request( |
| 50 | + "api_keys/validate", |
| 51 | + method=REQUEST_METHOD_POST, |
| 52 | + ) |
| 53 | + |
| 54 | + return ApiKey.model_validate(response) |
0 commit comments