twilio_rust_openapi/apis/
api20100401_account_api.rs

1/*
2 * Twilio - Api
3 *
4 * This is the public Twilio REST API.
5 *
6 * The version of the OpenAPI document: 1.0.0
7 * Contact: support@twilio.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration};
16
17/// struct for passing parameters to the method [`create_account`]
18#[derive(Clone, Debug)]
19pub struct CreateAccountParams {
20    /// A human readable description of the account to create, defaults to `SubAccount Created at {YYYY-MM-DD HH:MM meridian}`
21    pub friendly_name: Option<String>
22}
23
24/// struct for passing parameters to the method [`fetch_account`]
25#[derive(Clone, Debug)]
26pub struct FetchAccountParams {
27    /// The Account Sid that uniquely identifies the account to fetch
28    pub sid: String
29}
30
31/// struct for passing parameters to the method [`list_account`]
32#[derive(Clone, Debug)]
33pub struct ListAccountParams {
34    /// Only return the Account resources with friendly names that exactly match this name.
35    pub friendly_name: Option<String>,
36    /// Only return Account resources with the given status. Can be `closed`, `suspended` or `active`.
37    pub status: Option<String>,
38    /// How many resources to return in each list page. The default is 50, and the maximum is 1000.
39    pub page_size: Option<i32>,
40    /// The page index. This value is simply for client state.
41    pub page: Option<i32>,
42    /// The page token. This is provided by the API.
43    pub page_token: Option<String>
44}
45
46/// struct for passing parameters to the method [`update_account`]
47#[derive(Clone, Debug)]
48pub struct UpdateAccountParams {
49    /// The Account Sid that uniquely identifies the account to update
50    pub sid: String,
51    /// Update the human-readable description of this Account
52    pub friendly_name: Option<String>,
53    pub status: Option<String>
54}
55
56
57/// struct for typed errors of method [`create_account`]
58#[derive(Debug, Clone, Serialize, Deserialize)]
59#[serde(untagged)]
60pub enum CreateAccountError {
61    UnknownValue(serde_json::Value),
62}
63
64/// struct for typed errors of method [`fetch_account`]
65#[derive(Debug, Clone, Serialize, Deserialize)]
66#[serde(untagged)]
67pub enum FetchAccountError {
68    UnknownValue(serde_json::Value),
69}
70
71/// struct for typed errors of method [`list_account`]
72#[derive(Debug, Clone, Serialize, Deserialize)]
73#[serde(untagged)]
74pub enum ListAccountError {
75    UnknownValue(serde_json::Value),
76}
77
78/// struct for typed errors of method [`update_account`]
79#[derive(Debug, Clone, Serialize, Deserialize)]
80#[serde(untagged)]
81pub enum UpdateAccountError {
82    UnknownValue(serde_json::Value),
83}
84
85
86/// Create a new Twilio Subaccount from the account making the request
87pub async fn create_account(configuration: &configuration::Configuration, params: CreateAccountParams) -> Result<models::ApiPeriodV2010PeriodAccount, Error<CreateAccountError>> {
88    let local_var_configuration = configuration;
89
90    // unbox the parameters
91    let friendly_name = params.friendly_name;
92
93
94    let local_var_client = &local_var_configuration.client;
95
96    let local_var_uri_str = format!("{}/2010-04-01/Accounts.json", local_var_configuration.base_path);
97    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
98
99    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
100        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
101    }
102    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
103        local_var_req_builder = local_var_req_builder.basic_auth(local_var_auth_conf.0.to_owned(), local_var_auth_conf.1.to_owned());
104    };
105    let mut local_var_form_params = std::collections::HashMap::new();
106    if let Some(local_var_param_value) = friendly_name {
107        local_var_form_params.insert("FriendlyName", local_var_param_value.to_string());
108    }
109    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
110
111    let local_var_req = local_var_req_builder.build()?;
112    let local_var_resp = local_var_client.execute(local_var_req).await?;
113
114    let local_var_status = local_var_resp.status();
115    let local_var_content = local_var_resp.text().await?;
116
117    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
118        serde_json::from_str(&local_var_content).map_err(Error::from)
119    } else {
120        let local_var_entity: Option<CreateAccountError> = serde_json::from_str(&local_var_content).ok();
121        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
122        Err(Error::ResponseError(local_var_error))
123    }
124}
125
126/// Fetch the account specified by the provided Account Sid
127pub async fn fetch_account(configuration: &configuration::Configuration, params: FetchAccountParams) -> Result<models::ApiPeriodV2010PeriodAccount, Error<FetchAccountError>> {
128    let local_var_configuration = configuration;
129
130    // unbox the parameters
131    let sid = params.sid;
132
133
134    let local_var_client = &local_var_configuration.client;
135
136    let local_var_uri_str = format!("{}/2010-04-01/Accounts/{Sid}.json", local_var_configuration.base_path, Sid=crate::apis::urlencode(sid));
137    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
138
139    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
140        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
141    }
142    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
143        local_var_req_builder = local_var_req_builder.basic_auth(local_var_auth_conf.0.to_owned(), local_var_auth_conf.1.to_owned());
144    };
145
146    let local_var_req = local_var_req_builder.build()?;
147    let local_var_resp = local_var_client.execute(local_var_req).await?;
148
149    let local_var_status = local_var_resp.status();
150    let local_var_content = local_var_resp.text().await?;
151
152    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
153        serde_json::from_str(&local_var_content).map_err(Error::from)
154    } else {
155        let local_var_entity: Option<FetchAccountError> = serde_json::from_str(&local_var_content).ok();
156        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
157        Err(Error::ResponseError(local_var_error))
158    }
159}
160
161/// Retrieves a collection of Accounts belonging to the account used to make the request
162pub async fn list_account(configuration: &configuration::Configuration, params: ListAccountParams) -> Result<models::ListAccountResponse, Error<ListAccountError>> {
163    let local_var_configuration = configuration;
164
165    // unbox the parameters
166    let friendly_name = params.friendly_name;
167    let status = params.status;
168    let page_size = params.page_size;
169    let page = params.page;
170    let page_token = params.page_token;
171
172
173    let local_var_client = &local_var_configuration.client;
174
175    let local_var_uri_str = format!("{}/2010-04-01/Accounts.json", local_var_configuration.base_path);
176    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
177
178    if let Some(ref local_var_str) = friendly_name {
179        local_var_req_builder = local_var_req_builder.query(&[("FriendlyName", &local_var_str.to_string())]);
180    }
181    if let Some(ref local_var_str) = status {
182        local_var_req_builder = local_var_req_builder.query(&[("Status", &local_var_str.to_string())]);
183    }
184    if let Some(ref local_var_str) = page_size {
185        local_var_req_builder = local_var_req_builder.query(&[("PageSize", &local_var_str.to_string())]);
186    }
187    if let Some(ref local_var_str) = page {
188        local_var_req_builder = local_var_req_builder.query(&[("Page", &local_var_str.to_string())]);
189    }
190    if let Some(ref local_var_str) = page_token {
191        local_var_req_builder = local_var_req_builder.query(&[("PageToken", &local_var_str.to_string())]);
192    }
193    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
194        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
195    }
196    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
197        local_var_req_builder = local_var_req_builder.basic_auth(local_var_auth_conf.0.to_owned(), local_var_auth_conf.1.to_owned());
198    };
199
200    let local_var_req = local_var_req_builder.build()?;
201    let local_var_resp = local_var_client.execute(local_var_req).await?;
202
203    let local_var_status = local_var_resp.status();
204    let local_var_content = local_var_resp.text().await?;
205
206    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
207        serde_json::from_str(&local_var_content).map_err(Error::from)
208    } else {
209        let local_var_entity: Option<ListAccountError> = serde_json::from_str(&local_var_content).ok();
210        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
211        Err(Error::ResponseError(local_var_error))
212    }
213}
214
215/// Modify the properties of a given Account
216pub async fn update_account(configuration: &configuration::Configuration, params: UpdateAccountParams) -> Result<models::ApiPeriodV2010PeriodAccount, Error<UpdateAccountError>> {
217    let local_var_configuration = configuration;
218
219    // unbox the parameters
220    let sid = params.sid;
221    let friendly_name = params.friendly_name;
222    let status = params.status;
223
224
225    let local_var_client = &local_var_configuration.client;
226
227    let local_var_uri_str = format!("{}/2010-04-01/Accounts/{Sid}.json", local_var_configuration.base_path, Sid=crate::apis::urlencode(sid));
228    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
229
230    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
231        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
232    }
233    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
234        local_var_req_builder = local_var_req_builder.basic_auth(local_var_auth_conf.0.to_owned(), local_var_auth_conf.1.to_owned());
235    };
236    let mut local_var_form_params = std::collections::HashMap::new();
237    if let Some(local_var_param_value) = friendly_name {
238        local_var_form_params.insert("FriendlyName", local_var_param_value.to_string());
239    }
240    if let Some(local_var_param_value) = status {
241        local_var_form_params.insert("Status", local_var_param_value.to_string());
242    }
243    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
244
245    let local_var_req = local_var_req_builder.build()?;
246    let local_var_resp = local_var_client.execute(local_var_req).await?;
247
248    let local_var_status = local_var_resp.status();
249    let local_var_content = local_var_resp.text().await?;
250
251    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
252        serde_json::from_str(&local_var_content).map_err(Error::from)
253    } else {
254        let local_var_entity: Option<UpdateAccountError> = serde_json::from_str(&local_var_content).ok();
255        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
256        Err(Error::ResponseError(local_var_error))
257    }
258}
259