Skip to content

Commit

Permalink
catch rate-limiting errors and fix other error-parsing for api key va…
Browse files Browse the repository at this point in the history
…lidation
  • Loading branch information
mrmauer committed Jan 15, 2025
1 parent d9276c4 commit 51c30b8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
25 changes: 22 additions & 3 deletions src/apis/api_key_service_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ pub struct ValidateApiKeyParams {
pub enum ApiKeyError {
BadRequest(crate::models::BadUpdateOrgRequest),
InvalidIntegrationAPIKey,
InvalidAPIKey,
InvalidAPIKey {
message: String,
},
RateLimited {
wait_seconds: f64,
user_facing_error: String,
},
InvalidPersonalAPIKey,
InvalidOrgAPIKey,
NotFound,
Expand All @@ -58,6 +64,19 @@ pub enum ApiKeyError {
UnexpectedExceptionWithSDK,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum ApiKeyValidationErrorResponse {
InvalidEndUserApiKey {
api_key_id: String,
},
EndUserApiKeyRateLimited {
wait_seconds: f64,
error_code: String,
user_facing_error: String,
},
}

pub async fn fetch_current_api_keys(
configuration: &configuration::Configuration,
params: ApiKeyQueryParams,
Expand Down Expand Up @@ -340,7 +359,7 @@ pub async fn delete_api_key(
pub async fn validate_api_key(
configuration: &configuration::Configuration,
params: ValidateApiKeyParams,
) -> Result<crate::models::ValidateApiKeyResponse, Error<ApiKeyError>> {
) -> Result<crate::models::ValidateApiKeyResponse, Error<ApiKeyValidationErrorResponse>> {
if hex::decode(&params.api_key_token).is_err() {
return Err(Error::Params("Invalid API key ID format".to_string()));
}
Expand Down Expand Up @@ -371,7 +390,7 @@ pub async fn validate_api_key(
if !status.is_client_error() && !status.is_server_error() {
serde_json::from_str(&content).map_err(Error::from)
} else {
let entity: Option<ApiKeyError> = serde_json::from_str(&content).ok();
let entity: Option<ApiKeyValidationErrorResponse> = serde_json::from_str(&content).ok();
let error = ResponseContent {
status,
content,
Expand Down
35 changes: 26 additions & 9 deletions src/propelauth/api_key.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::apis::api_key_service_api::{ApiKeyError, ApiKeyQueryParams, CreateApiKeyParams, UpdateApiKeyParams, ValidateApiKeyParams};
use crate::apis::api_key_service_api::{ApiKeyError, ApiKeyQueryParams, ApiKeyValidationErrorResponse, CreateApiKeyParams, UpdateApiKeyParams, ValidateApiKeyParams};
use crate::apis::configuration::Configuration;
use crate::models::{CreateApiKeyResponse, FetchApiKeyResponse, FetchApiKeysPagedResponse, ValidateApiKeyResponse};
use crate::models::validate_api_key_response::{ValidateOrgApiKeyResponse, ValidatePersonalApiKeyResponse};
Expand Down Expand Up @@ -62,7 +62,7 @@ impl ApiKeyService<'_> {
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::InvalidAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
},
)
Expand Down Expand Up @@ -94,7 +94,7 @@ impl ApiKeyService<'_> {
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::InvalidAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
},
)
Expand All @@ -112,7 +112,7 @@ impl ApiKeyService<'_> {
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::InvalidAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
},
)
Expand All @@ -121,17 +121,34 @@ impl ApiKeyService<'_> {
Ok(())
}

pub async fn validate_api_key(&self, params: ValidateApiKeyParams) -> Result<ValidateApiKeyResponse, ApiKeyError> {
pub async fn validate_api_key(
&self,
params: ValidateApiKeyParams,
) -> Result<ValidateApiKeyResponse, ApiKeyError> {
crate::apis::api_key_service_api::validate_api_key(&self.config, params)
.await
.map_err(|err| {
map_autogenerated_error(
err,
ApiKeyError::UnexpectedExceptionWithSDK,
|status_code, _| match status_code.as_u16() {
401 => ApiKeyError::InvalidIntegrationAPIKey,
404 => ApiKeyError::NotFound,
_ => ApiKeyError::UnknownError,
|status_code, error_response_body| match error_response_body {
Some(ApiKeyValidationErrorResponse::InvalidEndUserApiKey {
api_key_id,
}) => ApiKeyError::InvalidAPIKey {
message: api_key_id,
},
Some(ApiKeyValidationErrorResponse::EndUserApiKeyRateLimited {
wait_seconds,
user_facing_error,
..
}) => ApiKeyError::RateLimited {
wait_seconds,
user_facing_error,
},
None => match status_code.as_u16() {
401 => ApiKeyError::InvalidIntegrationAPIKey,
_ => ApiKeyError::UnknownError,
},
},
)
})
Expand Down

0 comments on commit 51c30b8

Please sign in to comment.