1- from typing import Protocol
1+ from typing import Optional , Protocol
22
33from workos .types .api_keys import ApiKey
44from workos .typing .sync_or_async import SyncOrAsync
55from workos .utils .http_client import AsyncHTTPClient , SyncHTTPClient
66from workos .utils .request_helper import REQUEST_METHOD_POST
77
88API_KEY_VALIDATION_PATH = "api_keys/validations"
9+ RESOURCE_OBJECT_PATH = "api_key"
910
1011
1112class ApiKeysModule (Protocol ):
12- def validate_api_key (self , * , value : str ) -> SyncOrAsync [ApiKey ]:
13- """Validates the configured API key.
13+ def validate_api_key (self , * , value : str ) -> SyncOrAsync [Optional [ApiKey ]]:
14+ """Validate an API key.
15+
16+ Kwargs:
17+ value (str): API key value
1418
1519 Returns:
16- ApiKey: The validated API key details containing
17- information about the key's name and usage
18-
19- Raises:
20- AuthenticationException: If the API key is invalid or
21- unauthorized (401)
22- NotFoundException: If the API key is not found (404)
23- ServerException: If the API server encounters an error
24- (5xx)
20+ Optional[ApiKey]: Returns ApiKey resource object
21+ if supplied value was valid, None if it was not
2522 """
2623 ...
2724
@@ -32,12 +29,13 @@ class ApiKeys(ApiKeysModule):
3229 def __init__ (self , http_client : SyncHTTPClient ):
3330 self ._http_client = http_client
3431
35- def validate_api_key (self , * , value : str ) -> ApiKey :
32+ def validate_api_key (self , * , value : str ) -> Optional [ ApiKey ] :
3633 response = self ._http_client .request (
37- API_KEY_VALIDATION_PATH , method = REQUEST_METHOD_POST , json = {
38- "value" : value }
34+ API_KEY_VALIDATION_PATH , method = REQUEST_METHOD_POST , json = {"value" : value }
3935 )
40- return ApiKey .model_validate (response ["api_key" ])
36+ if response .get (RESOURCE_OBJECT_PATH ) is None :
37+ return None
38+ return ApiKey .model_validate (response [RESOURCE_OBJECT_PATH ])
4139
4240
4341class AsyncApiKeys (ApiKeysModule ):
@@ -46,9 +44,10 @@ class AsyncApiKeys(ApiKeysModule):
4644 def __init__ (self , http_client : AsyncHTTPClient ):
4745 self ._http_client = http_client
4846
49- async def validate_api_key (self , * , value : str ) -> ApiKey :
47+ async def validate_api_key (self , * , value : str ) -> Optional [ ApiKey ] :
5048 response = await self ._http_client .request (
51- API_KEY_VALIDATION_PATH , method = REQUEST_METHOD_POST , json = {
52- "value" : value }
49+ API_KEY_VALIDATION_PATH , method = REQUEST_METHOD_POST , json = {"value" : value }
5350 )
54- return ApiKey .model_validate (response ["api_key" ])
51+ if response .get (RESOURCE_OBJECT_PATH ) is None :
52+ return None
53+ return ApiKey .model_validate (response [RESOURCE_OBJECT_PATH ])
0 commit comments