Fix missing_swap_indexes error code and handling of expires_at param...

of create api key route
This commit is contained in:
Loïc Lecrenier 2023-01-12 16:42:50 +01:00
parent 766dd830ae
commit 49ddaaef49
2 changed files with 15 additions and 11 deletions

View File

@ -15,7 +15,8 @@ use serde_cs::vec::CS;
use crate::star_or::StarOr;
use self::deserr_codes::{
MissingApiKeyActions, MissingApiKeyExpiresAt, MissingApiKeyIndexes, MissingIndexUid, InvalidSwapIndexes, MissingSwapIndexesIndexes,
InvalidSwapIndexes, MissingApiKeyActions, MissingApiKeyExpiresAt, MissingApiKeyIndexes,
MissingIndexUid, MissingSwapIndexes,
};
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
@ -280,7 +281,7 @@ MissingDocumentId , invalid , BAD_REQUEST ;
MissingIndexUid , invalid , BAD_REQUEST ;
MissingMasterKey , authentication, UNAUTHORIZED ;
MissingPayload , invalid , BAD_REQUEST ;
MissingSwapIndexesIndexes , invalid , BAD_REQUEST ;
MissingSwapIndexes , invalid , BAD_REQUEST ;
MissingTaskFilters , invalid , BAD_REQUEST ;
NoSpaceLeftOnDevice , system , UNPROCESSABLE_ENTITY;
PayloadTooLarge , invalid , PAYLOAD_TOO_LARGE ;
@ -516,7 +517,7 @@ impl DeserrJsonError<InvalidSwapIndexes> {
deserr::ErrorKind::MissingField { field },
location,
));
Self { msg: x.msg, code: MissingSwapIndexesIndexes.error_code(), _phantom: PhantomData }
Self { msg: x.msg, code: MissingSwapIndexes.error_code(), _phantom: PhantomData }
}
}

View File

@ -48,7 +48,7 @@ pub struct CreateApiKey {
pub actions: Vec<Action>,
#[deserr(error = DeserrJsonError<InvalidApiKeyIndexes>, missing_field_error = DeserrJsonError::missing_api_key_indexes)]
pub indexes: Vec<StarOr<IndexUid>>,
#[deserr(error = DeserrJsonError<InvalidApiKeyExpiresAt>, from(&String) = parse_expiration_date -> TakeErrorMessage<ParseOffsetDateTimeError>, missing_field_error = DeserrJsonError::missing_api_key_expires_at)]
#[deserr(error = DeserrJsonError<InvalidApiKeyExpiresAt>, from(Option<String>) = parse_expiration_date -> TakeErrorMessage<ParseOffsetDateTimeError>, missing_field_error = DeserrJsonError::missing_api_key_expires_at)]
pub expires_at: Option<OffsetDateTime>,
}
impl CreateApiKey {
@ -159,36 +159,39 @@ impl Display for ParseOffsetDateTimeError {
impl std::error::Error for ParseOffsetDateTimeError {}
fn parse_expiration_date(
string: &str,
string: Option<String>,
) -> std::result::Result<Option<OffsetDateTime>, TakeErrorMessage<ParseOffsetDateTimeError>> {
let datetime = if let Ok(datetime) = OffsetDateTime::parse(string, &Rfc3339) {
let Some(string) = string else {
return Ok(None)
};
let datetime = if let Ok(datetime) = OffsetDateTime::parse(&string, &Rfc3339) {
datetime
} else if let Ok(primitive_datetime) = PrimitiveDateTime::parse(
string,
&string,
format_description!(
"[year repr:full base:calendar]-[month repr:numerical]-[day]T[hour]:[minute]:[second]"
),
) {
primitive_datetime.assume_utc()
} else if let Ok(primitive_datetime) = PrimitiveDateTime::parse(
string,
&string,
format_description!(
"[year repr:full base:calendar]-[month repr:numerical]-[day] [hour]:[minute]:[second]"
),
) {
primitive_datetime.assume_utc()
} else if let Ok(date) = Date::parse(
string,
&string,
format_description!("[year repr:full base:calendar]-[month repr:numerical]-[day]"),
) {
PrimitiveDateTime::new(date, time!(00:00)).assume_utc()
} else {
return Err(TakeErrorMessage(ParseOffsetDateTimeError(string.to_owned())));
return Err(TakeErrorMessage(ParseOffsetDateTimeError(string)));
};
if datetime > OffsetDateTime::now_utc() {
Ok(Some(datetime))
} else {
Err(TakeErrorMessage(ParseOffsetDateTimeError(string.to_owned())))
Err(TakeErrorMessage(ParseOffsetDateTimeError(string)))
}
}