Skip to content

Commit

Permalink
feat(api-client): add support for slovene word and word meaning endpo…
Browse files Browse the repository at this point in the history
…ints, refactor macros
  • Loading branch information
simongoricar committed Oct 22, 2024
1 parent 10b2d1a commit c578287
Show file tree
Hide file tree
Showing 5 changed files with 759 additions and 29 deletions.
10 changes: 5 additions & 5 deletions kolomoni_api_client/src/api/dictionary/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
handle_internal_server_error,
handle_unexpected_status_code,
handlers,
unexpected_error_reason,
handle_unexpected_error_reason,
},
request::RequestBuilder,
AuthenticatedClient,
Expand Down Expand Up @@ -222,7 +222,7 @@ async fn update_category(

match category_error_reason {
CategoryErrorReason::CategoryNotFound => Err(CategoryUpdatingError::NotFound),
_ => unexpected_error_reason!(category_error_reason, response_status),
_ => handle_unexpected_error_reason!(category_error_reason, response_status),
}
} else if response_status == StatusCode::CONFLICT {
let category_error_reason = response.category_error_reason().await?;
Expand All @@ -234,7 +234,7 @@ async fn update_category(
CategoryErrorReason::EnglishNameAlreadyExists => {
Err(CategoryUpdatingError::EnglishNameAlreadyExists)
}
_ => unexpected_error_reason!(category_error_reason, response_status),
_ => handle_unexpected_error_reason!(category_error_reason, response_status),
}
} else if response_status == StatusCode::FORBIDDEN {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::MissingPermissions]);
Expand Down Expand Up @@ -275,7 +275,7 @@ async fn create_category(
CategoryErrorReason::SloveneNameAlreadyExists => {
Err(CategoryCreationError::SloveneNameAlreadyExists)
}
_ => unexpected_error_reason!(category_error_reason, response_status),
_ => handle_unexpected_error_reason!(category_error_reason, response_status),
}
} else if response_status == StatusCode::FORBIDDEN {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::MissingPermissions]);
Expand Down Expand Up @@ -307,7 +307,7 @@ async fn delete_category(

match category_error_response {
CategoryErrorReason::CategoryNotFound => Err(CategoryDeletionError::NotFound),
_ => unexpected_error_reason!(category_error_response, response_status),
_ => handle_unexpected_error_reason!(category_error_response, response_status),
}
} else if response_status == StatusCode::BAD_REQUEST {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::InvalidUuidFormat]);
Expand Down
46 changes: 25 additions & 21 deletions kolomoni_api_client/src/api/dictionary/english.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ use crate::{
errors::{ClientError, ClientResult},
macros::{
handle_error_reasons_or_catch_unexpected_status,
handle_internal_server_error,
handle_unexpected_error_reason,
handle_unexpected_status_code,
handlers,
unexpected_error_reason,
},
request::RequestBuilder,
AuthenticatedClient,
Expand Down Expand Up @@ -90,7 +89,7 @@ pub enum EnglishWordUpdatingError {
#[error("english word not found")]
NotFound,

#[error("english word with this lemma already exists")]
#[error("an english word with this lemma already exists")]
LemmaAlreadyExists,

#[error("there were no fields to update")]
Expand Down Expand Up @@ -183,7 +182,7 @@ where

match error_reason {
WordErrorReason::WordNotFound => Err(EnglishWordFetchingError::NotFound),
_ => unexpected_error_reason!(error_reason, response_status),
_ => handle_unexpected_error_reason!(error_reason, response_status),
}
} else if response_status == StatusCode::BAD_REQUEST {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::InvalidUuidFormat]);
Expand All @@ -195,13 +194,16 @@ where

async fn get_english_word_by_lemma<C>(
client: &C,
lemma: &str,
english_word_lemma: &str,
) -> ClientResult<EnglishWordWithMeanings, EnglishWordFetchingError>
where
C: HttpClient,
{
let response = RequestBuilder::get(client)
.endpoint_url(format!("/dictionary/english/by-lemma/{}", lemma))
.endpoint_url(format!(
"/dictionary/english/by-lemma/{}",
english_word_lemma
))
.send()
.await?;

Expand All @@ -217,7 +219,7 @@ where

match word_error_reason {
WordErrorReason::WordNotFound => Err(EnglishWordFetchingError::NotFound),
_ => unexpected_error_reason!(word_error_reason, response_status),
_ => handle_unexpected_error_reason!(word_error_reason, response_status),
}
} else if response_status == StatusCode::BAD_REQUEST {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::InvalidUuidFormat]);
Expand All @@ -231,11 +233,13 @@ where

async fn create_english_word(
client: &AuthenticatedClient,
word: EnglishWordToCreate,
word_to_create: EnglishWordToCreate,
) -> ClientResult<EnglishWordWithMeanings, EnglishWordCreationError> {
let response = RequestBuilder::post(client)
.endpoint_url("/dictionary/english")
.json(&EnglishWordCreationRequest { lemma: word.lemma })
.json(&EnglishWordCreationRequest {
lemma: word_to_create.lemma,
})
.send()
.await?;

Expand All @@ -253,7 +257,7 @@ async fn create_english_word(
WordErrorReason::WordWithGivenLemmaAlreadyExists => {
Err(EnglishWordCreationError::LemmaAlreadyExists)
}
_ => unexpected_error_reason!(word_error_reason, response_status),
_ => handle_unexpected_error_reason!(word_error_reason, response_status),
}
} else if response_status == StatusCode::FORBIDDEN {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::MissingPermissions]);
Expand Down Expand Up @@ -293,7 +297,7 @@ async fn update_english_word(

match word_error_reason {
WordErrorReason::WordNotFound => Err(EnglishWordUpdatingError::NotFound),
_ => unexpected_error_reason!(word_error_reason, response_status),
_ => handle_unexpected_error_reason!(word_error_reason, response_status),
}
} else if response_status == StatusCode::CONFLICT {
let word_error_reason = response.word_error_reason().await?;
Expand All @@ -302,7 +306,7 @@ async fn update_english_word(
WordErrorReason::WordWithGivenLemmaAlreadyExists => {
Err(EnglishWordUpdatingError::LemmaAlreadyExists)
}
_ => unexpected_error_reason!(word_error_reason, response_status),
_ => handle_unexpected_error_reason!(word_error_reason, response_status),
}
} else {
handle_unexpected_status_code!(response_status);
Expand All @@ -328,7 +332,7 @@ async fn delete_english_word(

match word_error_reason {
WordErrorReason::WordNotFound => Err(EnglishWordDeletionError::NotFound),
_ => unexpected_error_reason!(word_error_reason, response_status),
_ => handle_unexpected_error_reason!(word_error_reason, response_status),
}
} else if response_status == StatusCode::BAD_REQUEST {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::InvalidUuidFormat]);
Expand Down Expand Up @@ -463,7 +467,7 @@ where

match word_error_response {
WordErrorReason::WordNotFound => Err(EnglishWordMeaningsFetchingError::WordNotFound),
_ => unexpected_error_reason!(word_error_response, response_status),
_ => handle_unexpected_error_reason!(word_error_response, response_status),
}
} else if response_status == StatusCode::FORBIDDEN {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::MissingPermissions]);
Expand Down Expand Up @@ -507,7 +511,7 @@ async fn create_english_word_meaning(
WordErrorReason::IdenticalWordMeaningAlreadyExists => {
Err(EnglishWordMeaningCreationError::IdenticalWordMeaningAlreadyExists)
}
_ => unexpected_error_reason!(word_error_reason, response_status),
_ => handle_unexpected_error_reason!(word_error_reason, response_status),
}
} else if response_status == StatusCode::FORBIDDEN {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::MissingPermissions]);
Expand Down Expand Up @@ -556,7 +560,7 @@ async fn update_english_word_meaning(
WordErrorReason::WordMeaningNotFound => {
Err(EnglishWordMeaningUpdatingError::WordMeaningNotFound)
}
_ => unexpected_error_reason!(word_error_reason, response_status),
_ => handle_unexpected_error_reason!(word_error_reason, response_status),
}
} else if response_status == StatusCode::FORBIDDEN {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::MissingPermissions]);
Expand Down Expand Up @@ -592,7 +596,7 @@ async fn delete_english_word_meaning(
WordErrorReason::WordMeaningNotFound => {
Err(EnglishWordMeaningDeletionError::WordMeaningNotFound)
}
_ => unexpected_error_reason!(word_error_reason, response_status),
_ => handle_unexpected_error_reason!(word_error_reason, response_status),
}
} else if response_status == StatusCode::FORBIDDEN {
handle_error_reasons_or_catch_unexpected_status!(response, [handlers::MissingPermissions]);
Expand All @@ -603,11 +607,11 @@ async fn delete_english_word_meaning(



pub struct EnglishApi<'c> {
pub struct EnglishDictionaryApi<'c> {
client: &'c Client,
}

impl<'c> EnglishApi<'c> {
impl<'c> EnglishDictionaryApi<'c> {
/*
* Word-related (word meanings are in the next section)
*/
Expand Down Expand Up @@ -649,11 +653,11 @@ impl<'c> EnglishApi<'c> {



pub struct EnglishAuthenticatedApi<'c> {
pub struct EnglishDictionaryAuthenticatedApi<'c> {
client: &'c AuthenticatedClient,
}

impl<'c> EnglishAuthenticatedApi<'c> {
impl<'c> EnglishDictionaryAuthenticatedApi<'c> {
/*
* Word-related (word meanings are in the next section)
*/
Expand Down
1 change: 1 addition & 0 deletions kolomoni_api_client/src/api/dictionary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod categories;
pub mod english;
pub mod slovene;
Loading

0 comments on commit c578287

Please sign in to comment.