twilio_rust_openapi/apis/
api20100401_token_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_token`]
18#[derive(Clone, Debug)]
19pub struct CreateTokenParams {
20    /// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that will create the resource.
21    pub account_sid: String,
22    /// The duration in seconds for which the generated credentials are valid. The default value is 86400 (24 hours).
23    pub ttl: Option<i32>
24}
25
26
27/// struct for typed errors of method [`create_token`]
28#[derive(Debug, Clone, Serialize, Deserialize)]
29#[serde(untagged)]
30pub enum CreateTokenError {
31    UnknownValue(serde_json::Value),
32}
33
34
35/// Create a new token for ICE servers
36pub async fn create_token(configuration: &configuration::Configuration, params: CreateTokenParams) -> Result<models::ApiPeriodV2010PeriodAccountPeriodToken, Error<CreateTokenError>> {
37    let local_var_configuration = configuration;
38
39    // unbox the parameters
40    let account_sid = params.account_sid;
41    let ttl = params.ttl;
42
43
44    let local_var_client = &local_var_configuration.client;
45
46    let local_var_uri_str = format!("{}/2010-04-01/Accounts/{AccountSid}/Tokens.json", local_var_configuration.base_path, AccountSid=crate::apis::urlencode(account_sid));
47    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
48
49    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
50        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
51    }
52    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
53        local_var_req_builder = local_var_req_builder.basic_auth(local_var_auth_conf.0.to_owned(), local_var_auth_conf.1.to_owned());
54    };
55    let mut local_var_form_params = std::collections::HashMap::new();
56    if let Some(local_var_param_value) = ttl {
57        local_var_form_params.insert("Ttl", local_var_param_value.to_string());
58    }
59    local_var_req_builder = local_var_req_builder.form(&local_var_form_params);
60
61    let local_var_req = local_var_req_builder.build()?;
62    let local_var_resp = local_var_client.execute(local_var_req).await?;
63
64    let local_var_status = local_var_resp.status();
65    let local_var_content = local_var_resp.text().await?;
66
67    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
68        serde_json::from_str(&local_var_content).map_err(Error::from)
69    } else {
70        let local_var_entity: Option<CreateTokenError> = serde_json::from_str(&local_var_content).ok();
71        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
72        Err(Error::ResponseError(local_var_error))
73    }
74}
75