artifacts/apis/
token_api.rs

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