dummy_json_rs/
quotes.rs

1use crate::{DummyJsonClient, API_BASE_URL};
2use once_cell::sync::Lazy;
3use serde::{Deserialize, Serialize};
4
5static QUOTES_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/quotes", API_BASE_URL));
6
7#[derive(Serialize, Deserialize, Debug)]
8pub struct Quote {
9	pub id: u32,
10	pub quote: String,
11	pub author: String,
12}
13
14#[derive(Deserialize, Debug)]
15pub struct GetAllQuotes {
16	pub quotes: Vec<Quote>,
17	pub total: u32,
18	pub skip: u32,
19	pub limit: u32,
20}
21
22impl DummyJsonClient {
23	/// Get all quotes
24	pub async fn get_all_quotes(&self) -> Result<GetAllQuotes, reqwest::Error> {
25		self.client
26			.get(QUOTES_BASE_URL.as_str())
27			.send()
28			.await?
29			.json::<GetAllQuotes>()
30			.await
31	}
32
33	/// Get quote by id
34	pub async fn get_quote_by_id(&self, id: u32) -> Result<Quote, reqwest::Error> {
35		self.client
36			.get(format!("{}/{}", QUOTES_BASE_URL.as_str(), id))
37			.send()
38			.await?
39			.json::<Quote>()
40			.await
41	}
42
43	/// Get random quote
44	pub async fn get_random_quote(&self) -> Result<Quote, reqwest::Error> {
45		self.client
46			.get(format!("{}/random", QUOTES_BASE_URL.as_str()))
47			.send()
48			.await?
49			.json::<Quote>()
50			.await
51	}
52
53	/// Get random quotes
54	pub async fn get_random_quotes(&self, count: u32) -> Result<Vec<Quote>, reqwest::Error> {
55		self.client
56			.get(format!("{}/random/{}", QUOTES_BASE_URL.as_str(), count))
57			.send()
58			.await?
59			.json::<Vec<Quote>>()
60			.await
61	}
62
63	/// Limit and skip quotes
64	pub async fn limit_skip_quotes(
65		&self,
66		limit: u32,
67		skip: u32,
68	) -> Result<GetAllQuotes, reqwest::Error> {
69		self.client
70			.get(format!("{}/?limit={}&skip={}", QUOTES_BASE_URL.as_str(), limit, skip))
71			.send()
72			.await?
73			.json::<GetAllQuotes>()
74			.await
75	}
76}