diff --git a/api-requests/recipes.http b/api-requests/recipes.http index db0deed..74e6490 100644 --- a/api-requests/recipes.http +++ b/api-requests/recipes.http @@ -38,7 +38,7 @@ Content-Type: application/json ### # @name GetRecipesByTags -@tags=pakistani +@tags=biryani GET {{host}}/tag/{{tags}} Accept: application/json Content-Type: application/json diff --git a/src/lib.rs b/src/lib.rs index 0c62989..d100838 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,13 @@ mod auth; mod carts; mod products; +mod recipes; mod todos; pub use auth::*; pub use carts::*; pub use products::*; +pub use recipes::*; use reqwest::Client; pub use todos::*; diff --git a/src/recipes.rs b/src/recipes.rs new file mode 100644 index 0000000..7e51290 --- /dev/null +++ b/src/recipes.rs @@ -0,0 +1,144 @@ +use crate::{DummyJsonClient, API_BASE_URL}; +use once_cell::sync::Lazy; +use serde::Deserialize; + +static RECIPES_BASE_URL: Lazy = Lazy::new(|| format!("{}/recipes", API_BASE_URL)); + +#[derive(Deserialize, Debug)] +pub struct GetAllRecipesResponse { + pub recipes: Vec, + pub total: u32, + pub skip: u32, + pub limit: u32, +} + +#[derive(Deserialize, Debug)] +pub struct Recipe { + pub id: u32, + pub name: Option, + pub ingredients: Option>, + pub instructions: Option>, + #[serde(rename = "prepTimeMinutes")] + pub prep_time_mins: Option, + #[serde(rename = "cookTimeMinutes")] + pub cook_time_mins: Option, + pub servings: Option, + // TODO: convert the Easy, Difficulty to enum + pub difficulty: Option, + pub cuisine: Option, + #[serde(rename = "caloriesPerServing")] + pub calories_per_serving: Option, + pub tags: Option>, + #[serde(rename = "userId")] + pub user_id: Option, + pub image: Option, + pub rating: Option, + #[serde(rename = "reviewCount")] + pub review_count: Option, + #[serde(rename = "mealType")] + pub meal_type: Option>, +} + +impl DummyJsonClient { + /// Get all recipes + pub async fn get_all_recipes(&self) -> Result { + self.client + .get(RECIPES_BASE_URL.as_str()) + .send() + .await? + .json::() + .await + } + + /// Get recipe by id + pub async fn get_recipe_by_id(&self, id: u32) -> Result { + self.client + .get(format!("{}/{}", &*RECIPES_BASE_URL, id)) + .send() + .await? + .json::() + .await + } + + /// Search recipes + pub async fn search_recipes( + &self, + query: &str, + ) -> Result { + self.client + .get(format!("{}/search?q={}", &*RECIPES_BASE_URL, query)) + .send() + .await? + .json::() + .await + } + + /// Limit and skip recipes + pub async fn limit_and_skip_recipes( + &self, + limit: u32, + skip: u32, + selects: &str, + ) -> Result { + self.client + .get(format!( + "{}/?limit={}&skip={}&select={}", + &*RECIPES_BASE_URL, limit, skip, selects + )) + .send() + .await? + .json::() + .await + } + + /// Sort recipes + pub async fn sort_recipes( + &self, + sort_by: &str, + // TODO: convert the asc, desc to enum + order: &str, + ) -> Result { + self.client + .get(format!("{}/?sortBy={}&order={}", &*RECIPES_BASE_URL, sort_by, order)) + .send() + .await? + .json::() + .await + } + + /// Get recipes tags + pub async fn get_recipes_tags(&self) -> Result, reqwest::Error> { + self.client + .get(format!("{}/tags", &*RECIPES_BASE_URL)) + .send() + .await? + .json::>() + .await + } + + /// Get recipes by tags + pub async fn get_recipes_by_tags( + &self, + tags: &str, + ) -> Result { + self.client + .get(format!("{}/tag/{}", &*RECIPES_BASE_URL, tags)) + .send() + .await? + .json::() + .await + } + + /// Get recipes by meal type + pub async fn get_recipes_by_meal_type( + &self, + meal_type: &str, + ) -> Result { + self.client + .get(format!("{}/meal-type/{}", &*RECIPES_BASE_URL, meal_type)) + .send() + .await? + .json::() + .await + } +} diff --git a/tests/recipes.rs b/tests/recipes.rs new file mode 100644 index 0000000..8a7a843 --- /dev/null +++ b/tests/recipes.rs @@ -0,0 +1,67 @@ +mod recipes { + use dummy_json_rs::DummyJsonClient; + + #[tokio::test] + async fn get_all_recipes() { + let client = DummyJsonClient::default(); + let response = client.get_all_recipes().await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn get_recipe_by_id() { + let client = DummyJsonClient::default(); + let response = client.get_recipe_by_id(1).await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn search_recipes() { + let client = DummyJsonClient::default(); + let response = client.search_recipes("pizza").await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn limit_and_skip_recipes() { + let client = DummyJsonClient::default(); + let response = client.limit_and_skip_recipes(10, 10, "name,image").await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn sort_recipes() { + let client = DummyJsonClient::default(); + let response = client.sort_recipes("name", "asc").await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn get_recipes_tags() { + let client = DummyJsonClient::default(); + let response = client.get_recipes_tags().await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn get_recipes_by_tags() { + let client = DummyJsonClient::default(); + let response = client.get_recipes_by_tags("biryani").await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn get_recipes_by_meal_type() { + let client = DummyJsonClient::default(); + let response = client.get_recipes_by_meal_type("snack").await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } +}