Skip to main content

ramp_api/
locations.rs

1use crate::Client;
2use crate::ClientResult;
3
4pub struct Locations {
5    pub client: Client,
6}
7
8impl Locations {
9    #[doc(hidden)]
10    pub fn new(client: Client) -> Self {
11        Locations { client }
12    }
13
14    /**
15     * List locations.
16     *
17     * This function performs a `GET` to the `/locations` endpoint.
18     *
19     * Retrieves all locations for your business.
20     *
21     * **Parameters:**
22     *
23     * * `authorization: &str` -- The OAuth2 token header.
24     * * `start: &str` -- The ID of the last entity of the previous page, used for pagination to get the next page.
25     * * `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.
26     */
27    pub async fn get_page(
28        &self,
29        start: &str,
30        page_size: f64,
31    ) -> ClientResult<crate::Response<Vec<crate::types::Location>>> {
32        let mut query_args: Vec<(String, String)> = Default::default();
33        if !page_size.to_string().is_empty() {
34            query_args.push(("page_size".to_string(), page_size.to_string()));
35        }
36        if !start.is_empty() {
37            query_args.push(("start".to_string(), start.to_string()));
38        }
39        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
40        let url = self.client.url(&format!("/locations?{}", query_), None);
41        let resp: crate::Response<crate::types::GetLocationResponse> = self
42            .client
43            .get(
44                &url,
45                crate::Message {
46                    body: None,
47                    content_type: None,
48                },
49            )
50            .await?;
51
52        // Return our response data.
53        Ok(crate::Response::new(
54            resp.status,
55            resp.headers,
56            resp.body.data.to_vec(),
57        ))
58    }
59    /**
60     * List locations.
61     *
62     * This function performs a `GET` to the `/locations` endpoint.
63     *
64     * As opposed to `get`, this function returns all the pages of the request at once.
65     *
66     * Retrieves all locations for your business.
67     */
68    pub async fn get_all(&self) -> ClientResult<crate::Response<Vec<crate::types::Location>>> {
69        let url = self.client.url("/locations", None);
70        let crate::Response::<crate::types::GetLocationResponse> {
71            mut status,
72            mut headers,
73            body,
74        } = self
75            .client
76            .get(
77                &url,
78                crate::Message {
79                    body: None,
80                    content_type: None,
81                },
82            )
83            .await?;
84
85        let mut data = body.data;
86        let mut page = body.page.next.to_string();
87
88        // Paginate if we should.
89        while !page.is_empty() {
90            match self
91                .client
92                .get::<crate::types::GetLocationResponse>(
93                    page.trim_start_matches(&self.client.host),
94                    crate::Message {
95                        body: None,
96                        content_type: None,
97                    },
98                )
99                .await
100            {
101                Ok(mut resp) => {
102                    data.append(&mut resp.body.data);
103                    status = resp.status;
104                    headers = resp.headers;
105
106                    page = if body.page.next != page {
107                        body.page.next.to_string()
108                    } else {
109                        "".to_string()
110                    };
111                }
112                Err(e) => {
113                    if e.to_string().contains("404 Not Found") {
114                        page = "".to_string();
115                    } else {
116                        return Err(e);
117                    }
118                }
119            }
120        }
121
122        // Return our response data.
123        Ok(crate::Response::new(status, headers, data))
124    }
125    /**
126     * Create new location.
127     *
128     * This function performs a `POST` to the `/locations` endpoint.
129     *
130     * Creates a new location for the business.
131     *
132     * **Parameters:**
133     *
134     * * `authorization: &str` -- The OAuth2 token header.
135     */
136    pub async fn post(
137        &self,
138        body: &crate::types::PostLocationRequest,
139    ) -> ClientResult<crate::Response<crate::types::Location>> {
140        let url = self.client.url("/locations", None);
141        self.client
142            .post(
143                &url,
144                crate::Message {
145                    body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
146                    content_type: Some("application/json".to_string()),
147                },
148            )
149            .await
150    }
151    /**
152     * GET a location.
153     *
154     * This function performs a `GET` to the `/locations/{id}` endpoint.
155     *
156     * Retrieve a specific location.
157     *
158     * **Parameters:**
159     *
160     * * `authorization: &str` -- The OAuth2 token header.
161     */
162    pub async fn get(&self, id: &str) -> ClientResult<crate::Response<crate::types::Location>> {
163        let url = self.client.url(
164            &format!("/locations/{}", crate::progenitor_support::encode_path(id),),
165            None,
166        );
167        self.client
168            .get(
169                &url,
170                crate::Message {
171                    body: None,
172                    content_type: None,
173                },
174            )
175            .await
176    }
177    /**
178     * Update location.
179     *
180     * This function performs a `PATCH` to the `/locations/{id}` endpoint.
181     *
182     * Modifies a specific location.
183     */
184    pub async fn patch(
185        &self,
186        id: &str,
187        body: &crate::types::PostLocationRequest,
188    ) -> ClientResult<crate::Response<crate::types::Location>> {
189        let url = self.client.url(
190            &format!("/locations/{}", crate::progenitor_support::encode_path(id),),
191            None,
192        );
193        self.client
194            .patch(
195                &url,
196                crate::Message {
197                    body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
198                    content_type: Some("application/json".to_string()),
199                },
200            )
201            .await
202    }
203}