dummy_json_rs/
products.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::{DummyJsonClient, API_BASE_URL};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};

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

#[derive(Deserialize, Debug)]
pub struct Product {
	pub id: u32,
	#[serde(flatten)]
	pub other_fields: AddProduct,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct AddProduct {
	pub title: String,
	pub description: Option<String>,
	pub price: Option<f32>,
	#[serde(rename = "discountPercentage")]
	pub discount_percentage: Option<f32>,
	pub rating: Option<f32>,
	pub stock: Option<u32>,
	pub tags: Option<Vec<String>>,
	// FIXME: Not sure, why the actual API response missing 'brand' field
	// resulting in error.
	// pub brand: Option<String>,
	pub sku: Option<String>,
	pub weight: Option<u16>,
	pub dimensions: Option<Dimension>,
	#[serde(rename = "warrantyInformation")]
	pub warranty_info: Option<String>,
	#[serde(rename = "shippingInformation")]
	pub shipping_info: Option<String>,
	#[serde(rename = "availabilityStatus")]
	pub availability_status: Option<String>,
	pub reviews: Option<Vec<Review>>,
	#[serde(rename = "returnPolicy")]
	pub return_policy: Option<String>,
	#[serde(rename = "minimumOrderQuantity")]
	pub min_order_qty: Option<u16>,
	pub meta: Option<Meta>,
	pub images: Option<Vec<String>>,
	pub thumbnail: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Dimension {
	pub width: f32,
	pub height: f32,
	pub depth: f32,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Review {
	pub rating: u8,
	pub comment: String,
	pub date: String,
	#[serde(rename = "reviewerName")]
	pub reviewer_name: String,
	#[serde(rename = "reviewerEmail")]
	pub reviewer_email: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Meta {
	#[serde(rename = "createdAt")]
	pub created_at: String,
	#[serde(rename = "updatedAt")]
	pub updated_at: String,
	pub barcode: String,
	#[serde(rename = "qrCode")]
	pub qr_code: String,
}

#[derive(Deserialize, Debug)]
pub struct GetAllProductsResponse {
	pub products: Vec<Product>,
	pub total: u32,
	pub skip: u32,
	pub limit: u32,
}

#[derive(Deserialize, Debug)]
pub struct ProductCategory {
	pub slug: String,
	pub name: String,
	pub url: String,
}

#[derive(Deserialize, Debug)]
pub struct DeleteProductResponse {
	#[serde(flatten)]
	pub other_fields: Product,
	#[serde(rename = "isDeleted")]
	pub is_deleted: bool,
	#[serde(rename = "deletedOn")]
	pub deleted_on: String,
}

impl DummyJsonClient {
	/// Get all products
	pub async fn get_all_products(&self) -> Result<GetAllProductsResponse, reqwest::Error> {
		let response = self.client.get(PRODUCTS_BASE_URL.as_str()).send().await?;
		response.json::<GetAllProductsResponse>().await
	}

	/// Get product by id
	pub async fn get_product_by_id(&self, id: u32) -> Result<Product, reqwest::Error> {
		let url = &format!("{}/{}", PRODUCTS_BASE_URL.as_str(), id);
		let response = self.client.get(url).send().await?;
		response.json::<Product>().await
	}

	/// Search products
	pub async fn search_products(
		&self,
		query: &str,
	) -> Result<GetAllProductsResponse, reqwest::Error> {
		let url = &format!("{}/search?q={}", PRODUCTS_BASE_URL.as_str(), query);
		let response = self.client.get(url).send().await?;
		response.json::<GetAllProductsResponse>().await
	}

	/// Limit and skip products
	pub async fn limit_and_skip_products(
		&self,
		limit: u32,
		skip: u32,
		selects: &str,
	) -> Result<GetAllProductsResponse, reqwest::Error> {
		let url = &format!(
			"{}/?limit={}&skip={}&select={}",
			PRODUCTS_BASE_URL.as_str(),
			limit,
			skip,
			selects
		);
		let response = self.client.get(url).send().await?;
		response.json::<GetAllProductsResponse>().await
	}

	/// Sort products by field
	pub async fn sort_products_by(
		&self,
		field: &str,
		order: &str,
	) -> Result<GetAllProductsResponse, reqwest::Error> {
		let url = &format!("{}/?sortBy={}&order={}", PRODUCTS_BASE_URL.as_str(), field, order);
		let response = self.client.get(url).send().await?;
		response.json::<GetAllProductsResponse>().await
	}

	/// Get product categories
	pub async fn get_product_categories(&self) -> Result<Vec<ProductCategory>, reqwest::Error> {
		let url = &format!("{}/categories", PRODUCTS_BASE_URL.as_str());
		let response = self.client.get(url).send().await?;
		response.json::<Vec<ProductCategory>>().await
	}

	/// Get product categories list
	pub async fn get_product_categories_list(&self) -> Result<Vec<String>, reqwest::Error> {
		let url = &format!("{}/category-list", PRODUCTS_BASE_URL.as_str());
		let response = self.client.get(url).send().await?;
		response.json::<Vec<String>>().await
	}

	/// Get products by category
	pub async fn get_products_by_category(
		&self,
		category: &str,
	) -> Result<GetAllProductsResponse, reqwest::Error> {
		let url = &format!("{}/category/{}", PRODUCTS_BASE_URL.as_str(), category);
		let response = self.client.get(url).send().await?;
		response.json::<GetAllProductsResponse>().await
	}

	/// Add product
	pub async fn add_product(&self, product: &AddProduct) -> Result<Product, reqwest::Error> {
		let url = &format!("{}/add", PRODUCTS_BASE_URL.as_str());
		let response = self.client.post(url).json(product).send().await?;
		response.json::<Product>().await
	}

	/// Update product
	pub async fn update_product(
		&self,
		id: u32,
		product: &AddProduct,
	) -> Result<Product, reqwest::Error> {
		let url = &format!("{}/{}", PRODUCTS_BASE_URL.as_str(), id);
		let response = self.client.put(url).json(product).send().await?;
		response.json::<Product>().await
	}

	/// Delete product
	pub async fn delete_product(&self, id: u32) -> Result<DeleteProductResponse, reqwest::Error> {
		let url = &format!("{}/{}", PRODUCTS_BASE_URL.as_str(), id);
		let response = self.client.delete(url).send().await?;
		response.json::<DeleteProductResponse>().await
	}
}