Skip to content

Commit

Permalink
Merge pull request #6 from abhi3700/recipes
Browse files Browse the repository at this point in the history
Add Recipes module
  • Loading branch information
abhi3700 authored Nov 1, 2024
2 parents 0647a98 + e2cdc2a commit 17390c1
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 0 deletions.
51 changes: 51 additions & 0 deletions api-requests/recipes.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@host=https://dummyjson.com/recipes

###
# @name GetAllRecipes
GET {{host}}
Accept: application/json
Content-Type: application/json

###
# @name GetRecipeById
GET {{host}}/1
Accept: application/json
Content-Type: application/json

###
# @name SearchRecipes
GET {{host}}/search?q=pizza
Accept: application/json
Content-Type: application/json

###
# @name LimitAndSkipRecipes
GET {{host}}?limit=10&skip=10&select=name,image
Accept: application/json
Content-Type: application/json

###
# @name SortRecipes
GET {{host}}?sortBy=name&order=asc
Accept: application/json
Content-Type: application/json

###
# @name GetRecipesTags
GET {{host}}/tags
Accept: application/json
Content-Type: application/json

###
# @name GetRecipesByTags
@tags=biryani
GET {{host}}/tag/{{tags}}
Accept: application/json
Content-Type: application/json

###
# @name GetRecipesByMealType
@meal_type=snack
GET {{host}}/meal-type/{{meal_type}}
Accept: application/json
Content-Type: application/json
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down
144 changes: 144 additions & 0 deletions src/recipes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
use crate::{DummyJsonClient, API_BASE_URL};
use once_cell::sync::Lazy;
use serde::Deserialize;

static RECIPES_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/recipes", API_BASE_URL));

#[derive(Deserialize, Debug)]
pub struct GetAllRecipesResponse {
pub recipes: Vec<Recipe>,
pub total: u32,
pub skip: u32,
pub limit: u32,
}

#[derive(Deserialize, Debug)]
pub struct Recipe {
pub id: u32,
pub name: Option<String>,
pub ingredients: Option<Vec<String>>,
pub instructions: Option<Vec<String>>,
#[serde(rename = "prepTimeMinutes")]
pub prep_time_mins: Option<u32>,
#[serde(rename = "cookTimeMinutes")]
pub cook_time_mins: Option<u32>,
pub servings: Option<u32>,
// TODO: convert the Easy, Difficulty to enum
pub difficulty: Option<String>,
pub cuisine: Option<String>,
#[serde(rename = "caloriesPerServing")]
pub calories_per_serving: Option<u32>,
pub tags: Option<Vec<String>>,
#[serde(rename = "userId")]
pub user_id: Option<u32>,
pub image: Option<String>,
pub rating: Option<f32>,
#[serde(rename = "reviewCount")]
pub review_count: Option<u32>,
#[serde(rename = "mealType")]
pub meal_type: Option<Vec<String>>,
}

impl DummyJsonClient {
/// Get all recipes
pub async fn get_all_recipes(&self) -> Result<GetAllRecipesResponse, reqwest::Error> {
self.client
.get(RECIPES_BASE_URL.as_str())
.send()
.await?
.json::<GetAllRecipesResponse>()
.await
}

/// Get recipe by id
pub async fn get_recipe_by_id(&self, id: u32) -> Result<Recipe, reqwest::Error> {
self.client
.get(format!("{}/{}", &*RECIPES_BASE_URL, id))
.send()
.await?
.json::<Recipe>()
.await
}

/// Search recipes
pub async fn search_recipes(
&self,
query: &str,
) -> Result<GetAllRecipesResponse, reqwest::Error> {
self.client
.get(format!("{}/search?q={}", &*RECIPES_BASE_URL, query))
.send()
.await?
.json::<GetAllRecipesResponse>()
.await
}

/// Limit and skip recipes
pub async fn limit_and_skip_recipes(
&self,
limit: u32,
skip: u32,
selects: &str,
) -> Result<GetAllRecipesResponse, reqwest::Error> {
self.client
.get(format!(
"{}/?limit={}&skip={}&select={}",
&*RECIPES_BASE_URL, limit, skip, selects
))
.send()
.await?
.json::<GetAllRecipesResponse>()
.await
}

/// Sort recipes
pub async fn sort_recipes(
&self,
sort_by: &str,
// TODO: convert the asc, desc to enum
order: &str,
) -> Result<GetAllRecipesResponse, reqwest::Error> {
self.client
.get(format!("{}/?sortBy={}&order={}", &*RECIPES_BASE_URL, sort_by, order))
.send()
.await?
.json::<GetAllRecipesResponse>()
.await
}

/// Get recipes tags
pub async fn get_recipes_tags(&self) -> Result<Vec<String>, reqwest::Error> {
self.client
.get(format!("{}/tags", &*RECIPES_BASE_URL))
.send()
.await?
.json::<Vec<String>>()
.await
}

/// Get recipes by tags
pub async fn get_recipes_by_tags(
&self,
tags: &str,
) -> Result<GetAllRecipesResponse, reqwest::Error> {
self.client
.get(format!("{}/tag/{}", &*RECIPES_BASE_URL, tags))
.send()
.await?
.json::<GetAllRecipesResponse>()
.await
}

/// Get recipes by meal type
pub async fn get_recipes_by_meal_type(
&self,
meal_type: &str,
) -> Result<GetAllRecipesResponse, reqwest::Error> {
self.client
.get(format!("{}/meal-type/{}", &*RECIPES_BASE_URL, meal_type))
.send()
.await?
.json::<GetAllRecipesResponse>()
.await
}
}
67 changes: 67 additions & 0 deletions tests/recipes.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 17390c1

Please sign in to comment.