clerk_fapi_rs/apis/
backup_codes_api.rs

1/*
2 * Clerk Frontend API
3 *
4 * The Clerk REST Frontend API, meant to be accessed from a browser or native environment.  This is a Form Based API and all the data must be sent and formatted according to the `application/x-www-form-urlencoded` content type.  ### Versions  When the API changes in a way that isn't compatible with older versions, a new version is released. Each version is identified by its release date, e.g. `2021-02-05`. For more information, please see [Clerk API Versions](https://clerk.com/docs/backend-requests/versioning/overview).  ### Using the Try It Console  The `Try It` feature of the docs only works for **Development Instances** when using the `DevBrowser` security scheme. To use it, first generate a dev instance token from the `/v1/dev_browser` endpoint.  Please see https://clerk.com/docs for more information.
5 *
6 * The version of the OpenAPI document: v1
7 * Contact: support@clerk.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11use super::{configuration, ContentType, Error};
12use crate::{apis::ResponseContent, models};
13use reqwest;
14use serde::{de::Error as _, Deserialize, Serialize};
15
16/// struct for typed errors of method [`create_backup_codes`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum CreateBackupCodesError {
20    Status400(models::ClerkErrors),
21    Status403(models::ClerkErrors),
22    Status500(models::ClerkErrors),
23    UnknownValue(serde_json::Value),
24}
25
26/// Create two factor authentication backup codes for the current user. A two factor authentication method must be enabled for the environment. Otherwise the endpoint will return an error.
27pub async fn create_backup_codes(
28    configuration: &configuration::Configuration,
29) -> Result<models::ClientClientWrappedBackupCodes, Error<CreateBackupCodesError>> {
30    let uri_str = format!("{}/v1/me/backup_codes", configuration.base_path);
31    let mut req_builder = configuration
32        .client
33        .request(reqwest::Method::POST, &uri_str);
34
35    if let Some(ref apikey) = configuration.api_key {
36        let key = apikey.key.clone();
37        let value = match apikey.prefix {
38            Some(ref prefix) => format!("{prefix} {key}"),
39            None => key,
40        };
41        req_builder = req_builder.query(&[("__dev_session", value)]);
42    }
43    if let Some(ref user_agent) = configuration.user_agent {
44        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
45    }
46    if let Some(ref apikey) = configuration.api_key {
47        let key = apikey.key.clone();
48        let value = match apikey.prefix {
49            Some(ref prefix) => format!("{prefix} {key}"),
50            None => key,
51        };
52        req_builder = req_builder.header("__session", value);
53    };
54
55    let req = req_builder.build()?;
56    let resp = configuration.client.execute(req).await?;
57
58    let status = resp.status();
59    let content_type = resp
60        .headers()
61        .get("content-type")
62        .and_then(|v| v.to_str().ok())
63        .unwrap_or("application/octet-stream");
64    let content_type = super::ContentType::from(content_type);
65
66    if !status.is_client_error() && !status.is_server_error() {
67        let content = resp.text().await?;
68        match content_type {
69            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
70            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedBackupCodes`"))),
71            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedBackupCodes`")))),
72        }
73    } else {
74        let content = resp.text().await?;
75        let entity: Option<CreateBackupCodesError> = serde_json::from_str(&content).ok();
76        Err(Error::ResponseError(ResponseContent {
77            status,
78            content,
79            entity,
80        }))
81    }
82}