1use crate::Client;
2use crate::ClientResult;
3
4pub struct Users {
5 pub client: Client,
6}
7
8impl Users {
9 #[doc(hidden)]
10 pub fn new(client: Client) -> Self {
11 Users { client }
12 }
13
14 pub async fn get(&self, id: &str) -> ClientResult<crate::Response<crate::types::User>> {
26 let url = self.client.url(
27 &format!("/users/{}", crate::progenitor_support::encode_path(id),),
28 None,
29 );
30 self.client
31 .get(
32 &url,
33 crate::Message {
34 body: None,
35 content_type: None,
36 },
37 )
38 .await
39 }
40 pub async fn delete(&self, id: &str) -> ClientResult<crate::Response<()>> {
48 let url = self.client.url(
49 &format!("/users/{}", crate::progenitor_support::encode_path(id),),
50 None,
51 );
52 self.client
53 .delete(
54 &url,
55 crate::Message {
56 body: None,
57 content_type: None,
58 },
59 )
60 .await
61 }
62 pub async fn patch(
70 &self,
71 id: &str,
72 body: &crate::types::PatchUsersRequest,
73 ) -> ClientResult<crate::Response<()>> {
74 let url = self.client.url(
75 &format!("/users/{}", crate::progenitor_support::encode_path(id),),
76 None,
77 );
78 self.client
79 .patch(
80 &url,
81 crate::Message {
82 body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
83 content_type: Some("application/json".to_string()),
84 },
85 )
86 .await
87 }
88 pub async fn get_page(
104 &self,
105 start: &str,
106 page_size: f64,
107 department_id: &str,
108 location_id: &str,
109 ) -> ClientResult<crate::Response<Vec<crate::types::User>>> {
110 let mut query_args: Vec<(String, String)> = Default::default();
111 if !department_id.is_empty() {
112 query_args.push(("department_id".to_string(), department_id.to_string()));
113 }
114 if !location_id.is_empty() {
115 query_args.push(("location_id".to_string(), location_id.to_string()));
116 }
117 if !page_size.to_string().is_empty() {
118 query_args.push(("page_size".to_string(), page_size.to_string()));
119 }
120 if !start.is_empty() {
121 query_args.push(("start".to_string(), start.to_string()));
122 }
123 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
124 let url = self.client.url(&format!("/users?{}", query_), None);
125 let resp: crate::Response<crate::types::GetUsersResponse> = self
126 .client
127 .get(
128 &url,
129 crate::Message {
130 body: None,
131 content_type: None,
132 },
133 )
134 .await?;
135
136 Ok(crate::Response::new(
138 resp.status,
139 resp.headers,
140 resp.body.data.to_vec(),
141 ))
142 }
143 pub async fn get_all(
153 &self,
154 department_id: &str,
155 location_id: &str,
156 ) -> ClientResult<crate::Response<Vec<crate::types::User>>> {
157 let mut query_args: Vec<(String, String)> = Default::default();
158 if !department_id.is_empty() {
159 query_args.push(("department_id".to_string(), department_id.to_string()));
160 }
161 if !location_id.is_empty() {
162 query_args.push(("location_id".to_string(), location_id.to_string()));
163 }
164 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
165 let url = self.client.url(&format!("/users?{}", query_), None);
166 let crate::Response::<crate::types::GetUsersResponse> {
167 mut status,
168 mut headers,
169 body,
170 } = self
171 .client
172 .get(
173 &url,
174 crate::Message {
175 body: None,
176 content_type: None,
177 },
178 )
179 .await?;
180
181 let mut data = body.data;
182 let mut page = body.page.next.to_string();
183
184 while !page.is_empty() {
186 match self
187 .client
188 .get::<crate::types::GetUsersResponse>(
189 page.trim_start_matches(&self.client.host),
190 crate::Message {
191 body: None,
192 content_type: None,
193 },
194 )
195 .await
196 {
197 Ok(mut resp) => {
198 data.append(&mut resp.body.data);
199 status = resp.status;
200 headers = resp.headers;
201
202 page = if body.page.next != page {
203 body.page.next.to_string()
204 } else {
205 "".to_string()
206 };
207 }
208 Err(e) => {
209 if e.to_string().contains("404 Not Found") {
210 page = "".to_string();
211 } else {
212 return Err(e);
213 }
214 }
215 }
216 }
217
218 Ok(crate::Response::new(status, headers, data))
220 }
221 pub async fn post_deferred(
229 &self,
230 body: &crate::types::PostUsersDeferredRequest,
231 ) -> ClientResult<crate::Response<crate::types::User>> {
232 let url = self.client.url("/users/deferred", None);
233 self.client
234 .post(
235 &url,
236 crate::Message {
237 body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
238 content_type: Some("application/json".to_string()),
239 },
240 )
241 .await
242 }
243 pub async fn get_deferred_status(
251 &self,
252 id: &str,
253 ) -> ClientResult<crate::Response<crate::types::GetUsersDeferredStatusResponse>> {
254 let url = self.client.url(
255 &format!(
256 "/users/deferred/status/{}",
257 crate::progenitor_support::encode_path(id),
258 ),
259 None,
260 );
261 self.client
262 .get(
263 &url,
264 crate::Message {
265 body: None,
266 content_type: None,
267 },
268 )
269 .await
270 }
271}