rippling_api/
teams.rs

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