front_api/
accounts.rs

1use anyhow::Result;
2
3use crate::Client;
4#[derive(Clone, Debug)]
5pub struct Accounts {
6    pub client: Client,
7}
8
9impl Accounts {
10    #[doc(hidden)]
11    pub fn new(client: Client) -> Self {
12        Self { client }
13    }
14
15    #[doc = "List Accounts\n\nList the accounts of the company.\n\n**Parameters:**\n\n- `limit: Option<i64>`: Max number of results per page\n- `page_token: Option<String>`: Token to use to request the next page\n- `sort_by: Option<String>`: Field used to sort the records\n- `sort_order: Option<crate::types::SortOrder>`: Order by which results should be sorted\n\n```rust,no_run\nasync fn example_accounts_list() -> anyhow::Result<()> {\n    let client = front_api::Client::new_from_env();\n    let result: front_api::types::ListAccountsResponse = client\n        .accounts()\n        .list(\n            Some(4 as i64),\n            Some(\"some-string\".to_string()),\n            Some(\"some-string\".to_string()),\n            Some(front_api::types::SortOrder::Asc),\n        )\n        .await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
16    #[tracing::instrument]
17    pub async fn list<'a>(
18        &'a self,
19        limit: Option<i64>,
20        page_token: Option<String>,
21        sort_by: Option<String>,
22        sort_order: Option<crate::types::SortOrder>,
23    ) -> Result<crate::types::ListAccountsResponse, crate::types::error::Error> {
24        let mut req = self.client.client.request(
25            http::Method::GET,
26            &format!("{}/{}", self.client.base_url, "accounts"),
27        );
28        req = req.bearer_auth(&self.client.token);
29        let mut query_params = Vec::new();
30        if let Some(p) = limit {
31            query_params.push(("limit", format!("{}", p)));
32        }
33
34        if let Some(p) = page_token {
35            query_params.push(("page_token", p));
36        }
37
38        if let Some(p) = sort_by {
39            query_params.push(("sort_by", p));
40        }
41
42        if let Some(p) = sort_order {
43            query_params.push(("sort_order", format!("{}", 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            Err(crate::types::error::Error::UnexpectedResponse(resp))
59        }
60    }
61
62    #[doc = "Create account\n\nCreate a new account.\n\n```rust,no_run\nasync fn example_accounts_create() -> anyhow::Result<()> {\n    let client = front_api::Client::new_from_env();\n    let result: front_api::types::AccountResponse = client\n        .accounts()\n        .create(&front_api::types::Account {\n            name: Some(\"some-string\".to_string()),\n            description: Some(\"some-string\".to_string()),\n            domains: Some(vec![\"some-string\".to_string()]),\n            external_id: Some(\"some-string\".to_string()),\n            custom_fields: Some(std::collections::HashMap::from([(\n                \"some-key\".to_string(),\n                \"some-string\".to_string(),\n            )])),\n        })\n        .await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
63    #[tracing::instrument]
64    pub async fn create<'a>(
65        &'a self,
66        body: &crate::types::Account,
67    ) -> Result<crate::types::AccountResponse, crate::types::error::Error> {
68        let mut req = self.client.client.request(
69            http::Method::POST,
70            &format!("{}/{}", self.client.base_url, "accounts"),
71        );
72        req = req.bearer_auth(&self.client.token);
73        req = req.json(body);
74        let resp = req.send().await?;
75        let status = resp.status();
76        if status.is_success() {
77            let text = resp.text().await.unwrap_or_default();
78            serde_json::from_str(&text).map_err(|err| {
79                crate::types::error::Error::from_serde_error(
80                    format_serde_error::SerdeError::new(text.to_string(), err),
81                    status,
82                )
83            })
84        } else {
85            Err(crate::types::error::Error::UnexpectedResponse(resp))
86        }
87    }
88
89    #[doc = "Fetch an account\n\nFetches an account\n\n**Parameters:**\n\n- `account_id: &'astr`: The Account ID (required)\n\n```rust,no_run\nasync fn example_accounts_fetch() -> anyhow::Result<()> {\n    let client = front_api::Client::new_from_env();\n    let result: front_api::types::AccountResponse = client.accounts().fetch(\"some-string\").await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
90    #[tracing::instrument]
91    pub async fn fetch<'a>(
92        &'a self,
93        account_id: &'a str,
94    ) -> Result<crate::types::AccountResponse, crate::types::error::Error> {
95        let mut req = self.client.client.request(
96            http::Method::GET,
97            &format!(
98                "{}/{}",
99                self.client.base_url,
100                "accounts/{account_id}".replace("{account_id}", account_id)
101            ),
102        );
103        req = req.bearer_auth(&self.client.token);
104        let resp = req.send().await?;
105        let status = resp.status();
106        if status.is_success() {
107            let text = resp.text().await.unwrap_or_default();
108            serde_json::from_str(&text).map_err(|err| {
109                crate::types::error::Error::from_serde_error(
110                    format_serde_error::SerdeError::new(text.to_string(), err),
111                    status,
112                )
113            })
114        } else {
115            Err(crate::types::error::Error::UnexpectedResponse(resp))
116        }
117    }
118
119    #[doc = "Delete an account\n\nDeletes an account\n\n**Parameters:**\n\n- `account_id: &'astr`: \
120             The Account ID (required)\n\n```rust,no_run\nasync fn example_accounts_delete() -> \
121             anyhow::Result<()> {\n    let client = front_api::Client::new_from_env();\n    \
122             client.accounts().delete(\"some-string\").await?;\n    Ok(())\n}\n```"]
123    #[tracing::instrument]
124    pub async fn delete<'a>(
125        &'a self,
126        account_id: &'a str,
127    ) -> Result<(), crate::types::error::Error> {
128        let mut req = self.client.client.request(
129            http::Method::DELETE,
130            &format!(
131                "{}/{}",
132                self.client.base_url,
133                "accounts/{account_id}".replace("{account_id}", account_id)
134            ),
135        );
136        req = req.bearer_auth(&self.client.token);
137        let resp = req.send().await?;
138        let status = resp.status();
139        if status.is_success() {
140            Ok(())
141        } else {
142            Err(crate::types::error::Error::UnexpectedResponse(resp))
143        }
144    }
145
146    #[doc = "Update account\n\nUpdates an account.\n\n**Parameters:**\n\n- `account_id: &'astr`: The Account ID (required)\n\n```rust,no_run\nasync fn example_accounts_update() -> anyhow::Result<()> {\n    let client = front_api::Client::new_from_env();\n    let result: front_api::types::AccountResponse = client\n        .accounts()\n        .update(\n            \"some-string\",\n            &front_api::types::Account {\n                name: Some(\"some-string\".to_string()),\n                description: Some(\"some-string\".to_string()),\n                domains: Some(vec![\"some-string\".to_string()]),\n                external_id: Some(\"some-string\".to_string()),\n                custom_fields: Some(std::collections::HashMap::from([(\n                    \"some-key\".to_string(),\n                    \"some-string\".to_string(),\n                )])),\n            },\n        )\n        .await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
147    #[tracing::instrument]
148    pub async fn update<'a>(
149        &'a self,
150        account_id: &'a str,
151        body: &crate::types::Account,
152    ) -> Result<crate::types::AccountResponse, crate::types::error::Error> {
153        let mut req = self.client.client.request(
154            http::Method::PATCH,
155            &format!(
156                "{}/{}",
157                self.client.base_url,
158                "accounts/{account_id}".replace("{account_id}", account_id)
159            ),
160        );
161        req = req.bearer_auth(&self.client.token);
162        req = req.json(body);
163        let resp = req.send().await?;
164        let status = resp.status();
165        if status.is_success() {
166            let text = resp.text().await.unwrap_or_default();
167            serde_json::from_str(&text).map_err(|err| {
168                crate::types::error::Error::from_serde_error(
169                    format_serde_error::SerdeError::new(text.to_string(), err),
170                    status,
171                )
172            })
173        } else {
174            Err(crate::types::error::Error::UnexpectedResponse(resp))
175        }
176    }
177
178    #[doc = "List account contacts\n\nLists the contacts associated to an Account\n\n**Parameters:**\n\n- `account_id: &'astr`: The Account ID (required)\n- `limit: Option<i64>`: Max number of results per page\n- `page_token: Option<String>`: Token to use to request the next page\n\n```rust,no_run\nasync fn example_accounts_list_contacts() -> anyhow::Result<()> {\n    let client = front_api::Client::new_from_env();\n    let result: front_api::types::ListAccountContactsResponse = client\n        .accounts()\n        .list_contacts(\n            \"some-string\",\n            Some(4 as i64),\n            Some(\"some-string\".to_string()),\n        )\n        .await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
179    #[tracing::instrument]
180    pub async fn list_contacts<'a>(
181        &'a self,
182        account_id: &'a str,
183        limit: Option<i64>,
184        page_token: Option<String>,
185    ) -> Result<crate::types::ListAccountContactsResponse, crate::types::error::Error> {
186        let mut req = self.client.client.request(
187            http::Method::GET,
188            &format!(
189                "{}/{}",
190                self.client.base_url,
191                "accounts/{account_id}/contacts".replace("{account_id}", account_id)
192            ),
193        );
194        req = req.bearer_auth(&self.client.token);
195        let mut query_params = Vec::new();
196        if let Some(p) = limit {
197            query_params.push(("limit", format!("{}", p)));
198        }
199
200        if let Some(p) = page_token {
201            query_params.push(("page_token", p));
202        }
203
204        req = req.query(&query_params);
205        let resp = req.send().await?;
206        let status = resp.status();
207        if status.is_success() {
208            let text = resp.text().await.unwrap_or_default();
209            serde_json::from_str(&text).map_err(|err| {
210                crate::types::error::Error::from_serde_error(
211                    format_serde_error::SerdeError::new(text.to_string(), err),
212                    status,
213                )
214            })
215        } else {
216            Err(crate::types::error::Error::UnexpectedResponse(resp))
217        }
218    }
219
220    #[doc = "Add contact to Account\n\nAdds a list of contacts to an \
221             Account\n\n**Parameters:**\n\n- `account_id: &'astr`: The Account ID \
222             (required)\n\n```rust,no_run\nasync fn example_accounts_add_contacts_to() -> \
223             anyhow::Result<()> {\n    let client = front_api::Client::new_from_env();\n    \
224             client\n        .accounts()\n        .add_contacts_to(\n            \
225             \"some-string\",\n            &front_api::types::ContactIds {\n                \
226             contact_ids: vec![\"some-string\".to_string()],\n            },\n        )\n        \
227             .await?;\n    Ok(())\n}\n```"]
228    #[tracing::instrument]
229    pub async fn add_contacts_to<'a>(
230        &'a self,
231        account_id: &'a str,
232        body: &crate::types::ContactIds,
233    ) -> Result<(), crate::types::error::Error> {
234        let mut req = self.client.client.request(
235            http::Method::POST,
236            &format!(
237                "{}/{}",
238                self.client.base_url,
239                "accounts/{account_id}/contacts".replace("{account_id}", account_id)
240            ),
241        );
242        req = req.bearer_auth(&self.client.token);
243        req = req.json(body);
244        let resp = req.send().await?;
245        let status = resp.status();
246        if status.is_success() {
247            Ok(())
248        } else {
249            Err(crate::types::error::Error::UnexpectedResponse(resp))
250        }
251    }
252
253    #[doc = "Remove contact from Account\n\nRemoves a list of contacts from an \
254             Account\n\n**Parameters:**\n\n- `account_id: &'astr`: The Account ID \
255             (required)\n\n```rust,no_run\nasync fn example_accounts_remove_contacts_from() -> \
256             anyhow::Result<()> {\n    let client = front_api::Client::new_from_env();\n    \
257             client\n        .accounts()\n        .remove_contacts_from(\n            \
258             \"some-string\",\n            &front_api::types::ContactIds {\n                \
259             contact_ids: vec![\"some-string\".to_string()],\n            },\n        )\n        \
260             .await?;\n    Ok(())\n}\n```"]
261    #[tracing::instrument]
262    pub async fn remove_contacts_from<'a>(
263        &'a self,
264        account_id: &'a str,
265        body: &crate::types::ContactIds,
266    ) -> Result<(), crate::types::error::Error> {
267        let mut req = self.client.client.request(
268            http::Method::DELETE,
269            &format!(
270                "{}/{}",
271                self.client.base_url,
272                "accounts/{account_id}/contacts".replace("{account_id}", account_id)
273            ),
274        );
275        req = req.bearer_auth(&self.client.token);
276        req = req.json(body);
277        let resp = req.send().await?;
278        let status = resp.status();
279        if status.is_success() {
280            Ok(())
281        } else {
282            Err(crate::types::error::Error::UnexpectedResponse(resp))
283        }
284    }
285}