ramp_api/
users.rs

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    /**
15     * Get User Info by User ID.
16     *
17     * This function performs a `GET` to the `/users/{id}` endpoint.
18     *
19     * Retrieve the information of the user with the matching user ID.
20     *
21     * **Parameters:**
22     *
23     * * `authorization: &str` -- The OAuth2 token header.
24     */
25    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    /**
41     * Suspend a user.
42     *
43     * This function performs a `DELETE` to the `/users/{id}` endpoint.
44     *
45     * Suspends a user. Does not delete the user's cards. Currently this action is not reversible.
46     */
47    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    /**
63     * Modify Existing User.
64     *
65     * This function performs a `PATCH` to the `/users/{id}` endpoint.
66     *
67     * Modify information about a user.
68     */
69    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    /**
89     * List users.
90     *
91     * This function performs a `GET` to the `/users` endpoint.
92     *
93     * Retrieve all users of the business.
94     *
95     * **Parameters:**
96     *
97     * * `authorization: &str` -- The OAuth2 token header.
98     * * `start: &str` -- The ID of the last entity of the previous page, used for pagination to get the next page.
99     * * `page_size: f64` -- The number of results to be returned in each page. The value must be between 2 and 10,000. If not specified, the default will be 1,000.
100     * * `department_id: &str` -- The OAuth2 token header.
101     * * `location_id: &str` -- The OAuth2 token header.
102     */
103    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        // Return our response data.
137        Ok(crate::Response::new(
138            resp.status,
139            resp.headers,
140            resp.body.data.to_vec(),
141        ))
142    }
143    /**
144     * List users.
145     *
146     * This function performs a `GET` to the `/users` endpoint.
147     *
148     * As opposed to `get`, this function returns all the pages of the request at once.
149     *
150     * Retrieve all users of the business.
151     */
152    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        // Paginate if we should.
185        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        // Return our response data.
219        Ok(crate::Response::new(status, headers, data))
220    }
221    /**
222     * Invite a new user.
223     *
224     * This function performs a `POST` to the `/users/deferred` endpoint.
225     *
226     * Creates an invite for the user to accept. Also optionally sets department, location, and manager.
227     */
228    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    /**
244     * Get status of a deferred user task.
245     *
246     * This function performs a `GET` to the `/users/deferred/status/{id}` endpoint.
247     *
248     * Gets status of a deferred task for users
249     */
250    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}