dummy_json_rs/
quotes.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::{DummyJsonClient, API_BASE_URL};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};

static QUOTES_BASE_URL: Lazy<String> = 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<Quote>,
	pub total: u32,
	pub skip: u32,
	pub limit: u32,
}

impl DummyJsonClient {
	/// Get all quotes
	pub async fn get_all_quotes(&self) -> Result<GetAllQuotes, reqwest::Error> {
		self.client
			.get(QUOTES_BASE_URL.as_str())
			.send()
			.await?
			.json::<GetAllQuotes>()
			.await
	}

	/// Get quote by id
	pub async fn get_quote_by_id(&self, id: u32) -> Result<Quote, reqwest::Error> {
		self.client
			.get(format!("{}/{}", QUOTES_BASE_URL.as_str(), id))
			.send()
			.await?
			.json::<Quote>()
			.await
	}

	/// Get random quote
	pub async fn get_random_quote(&self) -> Result<Quote, reqwest::Error> {
		self.client
			.get(format!("{}/random", QUOTES_BASE_URL.as_str()))
			.send()
			.await?
			.json::<Quote>()
			.await
	}

	/// Get random quotes
	pub async fn get_random_quotes(&self, count: u32) -> Result<Vec<Quote>, reqwest::Error> {
		self.client
			.get(format!("{}/random/{}", QUOTES_BASE_URL.as_str(), count))
			.send()
			.await?
			.json::<Vec<Quote>>()
			.await
	}

	/// Limit and skip quotes
	pub async fn limit_skip_quotes(
		&self,
		limit: u32,
		skip: u32,
	) -> Result<GetAllQuotes, reqwest::Error> {
		self.client
			.get(format!("{}/?limit={}&skip={}", QUOTES_BASE_URL.as_str(), limit, skip))
			.send()
			.await?
			.json::<GetAllQuotes>()
			.await
	}
}