From 9b7b9945300320250721a86c51a87ccf86eab62a Mon Sep 17 00:00:00 2001 From: Abhijit Roy Date: Sat, 2 Nov 2024 02:59:26 +0530 Subject: [PATCH] Add quotes with tests --- api-requests/quotes.http | 32 +++++++++++++++++ src/comments.rs | 2 +- src/lib.rs | 2 ++ src/quotes.rs | 76 ++++++++++++++++++++++++++++++++++++++++ tests/quotes.rs | 43 +++++++++++++++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 api-requests/quotes.http create mode 100644 src/quotes.rs create mode 100644 tests/quotes.rs diff --git a/api-requests/quotes.http b/api-requests/quotes.http new file mode 100644 index 0000000..835e8b4 --- /dev/null +++ b/api-requests/quotes.http @@ -0,0 +1,32 @@ +@host=https://dummyjson.com/quotes + +### +# @name GetAllQuotes +GET {{host}} +Accept: application/json +Content-Type: application/json + + +### +# @name GetQuoteById +GET {{host}}/1 +Accept: application/json +Content-Type: application/json + +### +# @name GetRandomQuote +GET {{host}}/random +Accept: application/json +Content-Type: application/json + +### +# @name GetRandomQuotes +GET {{host}}/random/3 +Accept: application/json +Content-Type: application/json + +### +# @name LimitSkipQuotes +GET {{host}}?limit=3&skip=10 +Accept: application/json +Content-Type: application/json diff --git a/src/comments.rs b/src/comments.rs index 1534d9f..47313b3 100644 --- a/src/comments.rs +++ b/src/comments.rs @@ -35,7 +35,7 @@ pub struct GetAllComments { #[derive(Deserialize, Debug)] pub struct DeleteCommentResponse { #[serde(flatten)] - other_fields: Comment, + pub other_fields: Comment, #[serde(rename = "isDeleted")] pub is_deleted: bool, #[serde(rename = "deletedOn")] diff --git a/src/lib.rs b/src/lib.rs index 5f53cba..04e3edd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ mod carts; mod comments; mod posts; mod products; +mod quotes; mod recipes; mod todos; @@ -11,6 +12,7 @@ pub use carts::*; pub use comments::*; pub use posts::*; pub use products::*; +pub use quotes::*; pub use recipes::*; use reqwest::Client; pub use todos::*; diff --git a/src/quotes.rs b/src/quotes.rs new file mode 100644 index 0000000..47b69bd --- /dev/null +++ b/src/quotes.rs @@ -0,0 +1,76 @@ +use crate::{DummyJsonClient, API_BASE_URL}; +use once_cell::sync::Lazy; +use serde::{Deserialize, Serialize}; + +static QUOTES_BASE_URL: Lazy = Lazy::new(|| format!("{}/quotes", API_BASE_URL)); + +#[derive(Serialize, Deserialize, Debug)] +pub struct Quote { + pub id: u32, + pub quote: String, + pub author: String, +} + +#[derive(Deserialize, Debug)] +pub struct GetAllQuotes { + pub quotes: Vec, + pub total: u32, + pub skip: u32, + pub limit: u32, +} + +impl DummyJsonClient { + /// Get all quotes + pub async fn get_all_quotes(&self) -> Result { + self.client + .get(QUOTES_BASE_URL.as_str()) + .send() + .await? + .json::() + .await + } + + /// Get quote by id + pub async fn get_quote_by_id(&self, id: u32) -> Result { + self.client + .get(format!("{}/{}", QUOTES_BASE_URL.as_str(), id)) + .send() + .await? + .json::() + .await + } + + /// Get random quote + pub async fn get_random_quote(&self) -> Result { + self.client + .get(format!("{}/random", QUOTES_BASE_URL.as_str())) + .send() + .await? + .json::() + .await + } + + /// Get random quotes + pub async fn get_random_quotes(&self, count: u32) -> Result, reqwest::Error> { + self.client + .get(format!("{}/random/{}", QUOTES_BASE_URL.as_str(), count)) + .send() + .await? + .json::>() + .await + } + + /// Limit and skip quotes + pub async fn limit_skip_quotes( + &self, + limit: u32, + skip: u32, + ) -> Result { + self.client + .get(format!("{}/?limit={}&skip={}", QUOTES_BASE_URL.as_str(), limit, skip)) + .send() + .await? + .json::() + .await + } +} diff --git a/tests/quotes.rs b/tests/quotes.rs new file mode 100644 index 0000000..f8ba665 --- /dev/null +++ b/tests/quotes.rs @@ -0,0 +1,43 @@ +mod quotes { + use dummy_json_rs::DummyJsonClient; + + #[tokio::test] + async fn get_all_quotes() { + let client = DummyJsonClient::default(); + let response = client.get_all_quotes().await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn get_quote_by_id() { + let client = DummyJsonClient::default(); + let response = client.get_quote_by_id(1).await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn get_random_quote() { + let client = DummyJsonClient::default(); + let response = client.get_random_quote().await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn get_random_quotes() { + let client = DummyJsonClient::default(); + let response = client.get_random_quotes(3).await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } + + #[tokio::test] + async fn limit_skip_quotes() { + let client = DummyJsonClient::default(); + let response = client.limit_skip_quotes(3, 10).await; + assert!(response.is_ok()); + println!("{:#?}", response.unwrap()); + } +}