rippling_api/
work_locations.rs1use anyhow::Result;
2
3use crate::Client;
4#[derive(Clone, Debug)]
5pub struct WorkLocations {
6 pub client: Client,
7}
8
9impl WorkLocations {
10 #[doc(hidden)]
11 pub fn new(client: Client) -> Self {
12 Self { client }
13 }
14
15 #[doc = "List work locations\n\nA List of work locations\n- Requires: `API Tier 1`\n- Sortable \
16 fields: `id`, `created_at`, `updated_at`\n\n**Parameters:**\n\n- `cursor: \
17 Option<String>`\n- `order_by: Option<String>`\n\n```rust,no_run\nuse \
18 futures_util::TryStreamExt;\nasync fn example_work_locations_list_stream() -> \
19 anyhow::Result<()> {\n let client = rippling_api::Client::new_from_env();\n let \
20 mut work_locations = client.work_locations();\n let mut stream = \
21 work_locations.list_stream(Some(\"some-string\".to_string()));\n loop {\n \
22 match stream.try_next().await {\n Ok(Some(item)) => {\n \
23 println!(\"{:?}\", item);\n }\n Ok(None) => {\n \
24 break;\n }\n Err(err) => {\n return \
25 Err(err.into());\n }\n }\n }\n\n Ok(())\n}\n```"]
26 #[tracing::instrument]
27 pub async fn list<'a>(
28 &'a self,
29 cursor: Option<String>,
30 order_by: Option<String>,
31 ) -> Result<crate::types::ListWorkLocationsResponse, crate::types::error::Error> {
32 let mut req = self.client.client.request(
33 http::Method::GET,
34 format!("{}/{}", self.client.base_url, "work-locations"),
35 );
36 req = req.bearer_auth(&self.client.token);
37 let mut query_params = vec![];
38 if let Some(p) = cursor {
39 query_params.push(("cursor", p));
40 }
41
42 if let Some(p) = order_by {
43 query_params.push(("order_by", p));
44 }
45
46 req = req.query(&query_params);
47 let resp = req.send().await?;
48 let status = resp.status();
49 if status.is_success() {
50 let text = resp.text().await.unwrap_or_default();
51 serde_json::from_str(&text).map_err(|err| {
52 crate::types::error::Error::from_serde_error(
53 format_serde_error::SerdeError::new(text.to_string(), err),
54 status,
55 )
56 })
57 } else {
58 let text = resp.text().await.unwrap_or_default();
59 Err(crate::types::error::Error::Server {
60 body: text.to_string(),
61 status,
62 })
63 }
64 }
65
66 #[doc = "List work locations\n\nA List of work locations\n- Requires: `API Tier 1`\n- Sortable \
67 fields: `id`, `created_at`, `updated_at`\n\n**Parameters:**\n\n- `cursor: \
68 Option<String>`\n- `order_by: Option<String>`\n\n```rust,no_run\nuse \
69 futures_util::TryStreamExt;\nasync fn example_work_locations_list_stream() -> \
70 anyhow::Result<()> {\n let client = rippling_api::Client::new_from_env();\n let \
71 mut work_locations = client.work_locations();\n let mut stream = \
72 work_locations.list_stream(Some(\"some-string\".to_string()));\n loop {\n \
73 match stream.try_next().await {\n Ok(Some(item)) => {\n \
74 println!(\"{:?}\", item);\n }\n Ok(None) => {\n \
75 break;\n }\n Err(err) => {\n return \
76 Err(err.into());\n }\n }\n }\n\n Ok(())\n}\n```"]
77 #[tracing::instrument]
78 #[cfg(not(feature = "js"))]
79 pub fn list_stream<'a>(
80 &'a self,
81 order_by: Option<String>,
82 ) -> impl futures::Stream<Item = Result<crate::types::WorkLocation, crate::types::error::Error>>
83 + Unpin
84 + '_ {
85 use futures::{StreamExt, TryFutureExt, TryStreamExt};
86
87 use crate::types::paginate::Pagination;
88 self.list(None, order_by)
89 .map_ok(move |result| {
90 let items = futures::stream::iter(result.items().into_iter().map(Ok));
91 let next_pages = futures::stream::try_unfold(
92 (None, result),
93 move |(prev_page_token, new_result)| async move {
94 if new_result.has_more_pages()
95 && !new_result.items().is_empty()
96 && prev_page_token != new_result.next_page_token()
97 {
98 async {
99 let mut req = self.client.client.request(
100 http::Method::GET,
101 format!("{}/{}", self.client.base_url, "work-locations"),
102 );
103 req = req.bearer_auth(&self.client.token);
104 let mut request = req.build()?;
105 request = new_result.next_page(request)?;
106 let resp = self.client.client.execute(request).await?;
107 let status = resp.status();
108 if status.is_success() {
109 let text = resp.text().await.unwrap_or_default();
110 serde_json::from_str(&text).map_err(|err| {
111 crate::types::error::Error::from_serde_error(
112 format_serde_error::SerdeError::new(
113 text.to_string(),
114 err,
115 ),
116 status,
117 )
118 })
119 } else {
120 let text = resp.text().await.unwrap_or_default();
121 Err(crate::types::error::Error::Server {
122 body: text.to_string(),
123 status,
124 })
125 }
126 }
127 .map_ok(|result: crate::types::ListWorkLocationsResponse| {
128 Some((
129 futures::stream::iter(result.items().into_iter().map(Ok)),
130 (new_result.next_page_token(), result),
131 ))
132 })
133 .await
134 } else {
135 Ok(None)
136 }
137 },
138 )
139 .try_flatten();
140 items.chain(next_pages)
141 })
142 .try_flatten_stream()
143 .boxed()
144 }
145
146 #[doc = "Retrieve a specific work location\n\nRetrieve a specific work \
147 location\n\n**Parameters:**\n\n- `id: &'astr`: ID of the resource to return \
148 (required)\n\n```rust,no_run\nasync fn example_work_locations_get() -> \
149 anyhow::Result<()> {\n let client = rippling_api::Client::new_from_env();\n let \
150 result: rippling_api::types::GetWorkLocationsResponse = client\n \
151 .work_locations()\n .get(\"d9797f8d-9ad6-4e08-90d7-2ec17e13471c\")\n \
152 .await?;\n println!(\"{:?}\", result);\n Ok(())\n}\n```"]
153 #[tracing::instrument]
154 pub async fn get<'a>(
155 &'a self,
156 id: &'a str,
157 ) -> Result<crate::types::GetWorkLocationsResponse, crate::types::error::Error> {
158 let mut req = self.client.client.request(
159 http::Method::GET,
160 format!(
161 "{}/{}",
162 self.client.base_url,
163 "work-locations/{id}".replace("{id}", id)
164 ),
165 );
166 req = req.bearer_auth(&self.client.token);
167 let resp = req.send().await?;
168 let status = resp.status();
169 if status.is_success() {
170 let text = resp.text().await.unwrap_or_default();
171 serde_json::from_str(&text).map_err(|err| {
172 crate::types::error::Error::from_serde_error(
173 format_serde_error::SerdeError::new(text.to_string(), err),
174 status,
175 )
176 })
177 } else {
178 let text = resp.text().await.unwrap_or_default();
179 Err(crate::types::error::Error::Server {
180 body: text.to_string(),
181 status,
182 })
183 }
184 }
185}