1- use crate :: apis:: api_key_service_api:: { ApiKeyError , ApiKeyQueryParams , CreateApiKeyParams , UpdateApiKeyParams , ValidateApiKeyParams } ;
1+ use crate :: apis:: api_key_service_api:: { ApiKeyError , ApiKeyQueryParams , ApiKeyValidationErrorResponse , CreateApiKeyParams , UpdateApiKeyParams , ValidateApiKeyParams } ;
22use crate :: apis:: configuration:: Configuration ;
33use crate :: models:: { CreateApiKeyResponse , FetchApiKeyResponse , FetchApiKeysPagedResponse , ValidateApiKeyResponse } ;
44use crate :: models:: validate_api_key_response:: { ValidateOrgApiKeyResponse , ValidatePersonalApiKeyResponse } ;
@@ -62,7 +62,7 @@ impl ApiKeyService<'_> {
6262 ApiKeyError :: UnexpectedExceptionWithSDK ,
6363 |status_code, _| match status_code. as_u16 ( ) {
6464 401 => ApiKeyError :: InvalidIntegrationAPIKey ,
65- 404 => ApiKeyError :: InvalidAPIKey ,
65+ 404 => ApiKeyError :: NotFound ,
6666 _ => ApiKeyError :: UnknownError ,
6767 } ,
6868 )
@@ -94,7 +94,7 @@ impl ApiKeyService<'_> {
9494 ApiKeyError :: UnexpectedExceptionWithSDK ,
9595 |status_code, _| match status_code. as_u16 ( ) {
9696 401 => ApiKeyError :: InvalidIntegrationAPIKey ,
97- 404 => ApiKeyError :: InvalidAPIKey ,
97+ 404 => ApiKeyError :: NotFound ,
9898 _ => ApiKeyError :: UnknownError ,
9999 } ,
100100 )
@@ -112,7 +112,7 @@ impl ApiKeyService<'_> {
112112 ApiKeyError :: UnexpectedExceptionWithSDK ,
113113 |status_code, _| match status_code. as_u16 ( ) {
114114 401 => ApiKeyError :: InvalidIntegrationAPIKey ,
115- 404 => ApiKeyError :: InvalidAPIKey ,
115+ 404 => ApiKeyError :: NotFound ,
116116 _ => ApiKeyError :: UnknownError ,
117117 } ,
118118 )
@@ -121,17 +121,41 @@ impl ApiKeyService<'_> {
121121 Ok ( ( ) )
122122 }
123123
124- pub async fn validate_api_key ( & self , params : ValidateApiKeyParams ) -> Result < ValidateApiKeyResponse , ApiKeyError > {
124+ pub async fn validate_api_key (
125+ & self ,
126+ params : ValidateApiKeyParams ,
127+ ) -> Result < ValidateApiKeyResponse , ApiKeyError > {
128+ if hex:: decode ( & params. api_key_token ) . is_err ( ) {
129+ return Err ( ApiKeyError :: InvalidAPIKey {
130+ message : "Invalid API key format." . to_string ( )
131+ } ) ;
132+ }
133+
125134 crate :: apis:: api_key_service_api:: validate_api_key ( & self . config , params)
126135 . await
127136 . map_err ( |err| {
128137 map_autogenerated_error (
129138 err,
130139 ApiKeyError :: UnexpectedExceptionWithSDK ,
131- |status_code, _| match status_code. as_u16 ( ) {
132- 401 => ApiKeyError :: InvalidIntegrationAPIKey ,
133- 404 => ApiKeyError :: NotFound ,
134- _ => ApiKeyError :: UnknownError ,
140+ |status_code, error_response_body| match error_response_body {
141+ Some ( ApiKeyValidationErrorResponse :: InvalidEndUserApiKey {
142+ api_key_token,
143+ } ) => ApiKeyError :: InvalidAPIKey {
144+ message : api_key_token,
145+ } ,
146+ Some ( ApiKeyValidationErrorResponse :: EndUserApiKeyRateLimited {
147+ wait_seconds,
148+ user_facing_error,
149+ ..
150+ } ) => ApiKeyError :: RateLimited {
151+ wait_seconds,
152+ user_facing_error,
153+ } ,
154+ None => match status_code. as_u16 ( ) {
155+ 401 => ApiKeyError :: InvalidIntegrationAPIKey ,
156+ 404 => ApiKeyError :: NotFound ,
157+ _ => ApiKeyError :: UnknownError ,
158+ } ,
135159 } ,
136160 )
137161 } )
0 commit comments