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 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 Ok(crate::Response::new(
54 resp.status,
55 resp.headers,
56 resp.body.data.to_vec(),
57 ))
58 }
59 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 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 Ok(crate::Response::new(status, headers, data))
124 }
125 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 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 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}