1use crate::{
2 AddUserPayload, AllTodos, DummyJsonClient, GetAllCartsResponse, GetAllPosts, LoginRequest,
3 LoginResponse, User, API_BASE_URL,
4};
5use once_cell::sync::Lazy;
6use serde::Deserialize;
7
8static USERS_BASE_URL: Lazy<String> = Lazy::new(|| format!("{}/users", API_BASE_URL));
9
10#[derive(Deserialize, Debug)]
11pub struct GetAllUsersResponse {
12 pub users: Vec<User>,
13 pub total: u32,
14 pub skip: u32,
15 pub limit: u32,
16}
17
18#[derive(Deserialize, Debug)]
19pub struct DeleteUserResponse {
20 #[serde(flatten)]
21 pub user: User,
22 #[serde(rename = "isDeleted")]
23 pub is_deleted: bool,
24 #[serde(rename = "deletedOn")]
25 pub deleted_on: String,
26}
27
28impl DummyJsonClient {
29 pub async fn get_all_users(&self) -> Result<GetAllUsersResponse, reqwest::Error> {
31 self.client
32 .get(USERS_BASE_URL.as_str())
33 .send()
34 .await?
35 .json::<GetAllUsersResponse>()
36 .await
37 }
38
39 pub async fn login_user(
41 &self,
42 username: &str,
43 password: &str,
44 expires_in_mins: Option<u32>,
45 ) -> Result<LoginResponse, reqwest::Error> {
46 let payload: LoginRequest = LoginRequest {
47 username: username.to_string(),
48 password: password.to_string(),
49 expires_in_mins,
50 };
51 self.client
52 .post(format!("{}/login", USERS_BASE_URL.as_str()))
53 .json(&payload)
54 .send()
55 .await?
56 .json::<LoginResponse>()
57 .await
58 }
59
60 pub async fn get_current_authenticated_user(
62 &self,
63 access_token: &str,
64 ) -> Result<User, reqwest::Error> {
65 self.client
66 .get(format!("{}/me", USERS_BASE_URL.as_str()))
67 .header("Authorization", format!("Bearer {}", access_token))
68 .send()
69 .await?
70 .json::<User>()
71 .await
72 }
73
74 pub async fn get_user_by_id(&self, id: u32) -> Result<User, reqwest::Error> {
76 self.client
77 .get(format!("{}/{}", USERS_BASE_URL.as_str(), id))
78 .send()
79 .await?
80 .json::<User>()
81 .await
82 }
83
84 pub async fn search_users(&self, query: &str) -> Result<GetAllUsersResponse, reqwest::Error> {
86 self.client
87 .get(format!("{}/search?q={}", USERS_BASE_URL.as_str(), query))
88 .send()
89 .await?
90 .json::<GetAllUsersResponse>()
91 .await
92 }
93
94 pub async fn filter_users(
96 &self,
97 key: &str,
98 value: &str,
99 ) -> Result<GetAllUsersResponse, reqwest::Error> {
100 self.client
101 .get(format!("{}/filter?key={}&value={}", USERS_BASE_URL.as_str(), key, value))
102 .send()
103 .await?
104 .json::<GetAllUsersResponse>()
105 .await
106 }
107
108 pub async fn limit_skip_select_users(
110 &self,
111 limit: u32,
112 skip: u32,
113 selects: &str,
114 ) -> Result<GetAllUsersResponse, reqwest::Error> {
115 self.client
116 .get(format!(
117 "{}/?limit={}&skip={}&selects={}",
118 USERS_BASE_URL.as_str(),
119 limit,
120 skip,
121 selects
122 ))
123 .send()
124 .await?
125 .json::<GetAllUsersResponse>()
126 .await
127 }
128
129 pub async fn sort_users(
131 &self,
132 key: &str,
133 order: &str,
134 ) -> Result<GetAllUsersResponse, reqwest::Error> {
135 self.client
136 .get(format!("{}/?sortBy={}&order={}", USERS_BASE_URL.as_str(), key, order))
137 .send()
138 .await?
139 .json::<GetAllUsersResponse>()
140 .await
141 }
142
143 pub async fn get_user_carts_by_user_id(
145 &self,
146 user_id: u32,
147 ) -> Result<GetAllCartsResponse, reqwest::Error> {
148 self.client
149 .get(format!("{}/{}/carts", USERS_BASE_URL.as_str(), user_id))
150 .send()
151 .await?
152 .json::<GetAllCartsResponse>()
153 .await
154 }
155
156 pub async fn get_user_posts_by_user_id(
158 &self,
159 user_id: u32,
160 ) -> Result<GetAllPosts, reqwest::Error> {
161 self.client
162 .get(format!("{}/{}/posts", USERS_BASE_URL.as_str(), user_id))
163 .send()
164 .await?
165 .json::<GetAllPosts>()
166 .await
167 }
168
169 pub async fn get_user_todos_by_user_id(
171 &self,
172 user_id: u32,
173 ) -> Result<AllTodos, reqwest::Error> {
174 self.client
175 .get(format!("{}/{}/todos", USERS_BASE_URL.as_str(), user_id))
176 .send()
177 .await?
178 .json::<AllTodos>()
179 .await
180 }
181
182 pub async fn add_user(&self, user: &AddUserPayload) -> Result<User, reqwest::Error> {
184 self.client
185 .post(format!("{}/add", USERS_BASE_URL.as_str()))
186 .json(&user)
187 .send()
188 .await?
189 .json::<User>()
190 .await
191 }
192
193 pub async fn update_user(
195 &self,
196 id: u32,
197 user: &AddUserPayload,
198 ) -> Result<User, reqwest::Error> {
199 self.client
200 .put(format!("{}/{}", USERS_BASE_URL.as_str(), id))
201 .json(&user)
202 .send()
203 .await?
204 .json::<User>()
205 .await
206 }
207
208 pub async fn delete_user(&self, id: u32) -> Result<DeleteUserResponse, reqwest::Error> {
210 self.client
211 .delete(format!("{}/{}", USERS_BASE_URL.as_str(), id))
212 .send()
213 .await?
214 .json::<DeleteUserResponse>()
215 .await
216 }
217}