artifacts/apis/
token_api.rs

1use super::{configuration, Error};
2use crate::{apis::ResponseContent, models};
3use reqwest::StatusCode;
4use serde::{Deserialize, Serialize};
5
6/// struct for typed errors of method [`generate_token`]
7#[derive(Debug, Clone, Serialize, Deserialize)]
8#[serde(untagged)]
9pub enum GenerateTokenError {
10    /// Failed to generate token.
11    Status455,
12}
13
14impl TryFrom<StatusCode> for GenerateTokenError {
15    type Error = &'static str;
16    #[allow(clippy::match_single_binding)]
17    fn try_from(status: StatusCode) -> Result<Self, Self::Error> {
18        match status.as_u16() {
19            455 => Ok(Self::Status455),
20            _ => Err("status code not in spec"),
21        }
22    }
23}
24
25/// Use your account as HTTPBasic Auth to generate your token to use the API. You can also generate your token directly on the website.
26pub async fn generate_token(
27    configuration: &configuration::Configuration,
28) -> Result<models::TokenResponseSchema, Error<GenerateTokenError>> {
29    let local_var_configuration = configuration;
30
31    let local_var_client = &local_var_configuration.client;
32
33    let local_var_uri_str = format!("{}/token", local_var_configuration.base_path);
34    let mut local_var_req_builder =
35        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
36
37    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
38        local_var_req_builder =
39            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
40    }
41    if let Some(ref local_var_auth_conf) = local_var_configuration.basic_auth {
42        local_var_req_builder = local_var_req_builder.basic_auth(
43            local_var_auth_conf.0.to_owned(),
44            local_var_auth_conf.1.to_owned(),
45        );
46    };
47
48    let local_var_req = local_var_req_builder.build()?;
49    let local_var_resp = local_var_client.execute(local_var_req).await?;
50
51    let local_var_status = local_var_resp.status();
52    let local_var_content = local_var_resp.text().await?;
53
54    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
55        serde_json::from_str(&local_var_content).map_err(Error::from)
56    } else {
57        let local_var_entity: Option<GenerateTokenError> = local_var_status.try_into().ok();
58        let local_var_error = ResponseContent {
59            status: local_var_status,
60            content: local_var_content,
61            entity: local_var_entity,
62        };
63        Err(Error::ResponseError(local_var_error))
64    }
65}