clerk_fapi_rs/apis/
backup_codes_api.rs1use super::{configuration, ContentType, Error};
12use crate::{apis::ResponseContent, models};
13use reqwest;
14use serde::{de::Error as _, Deserialize, Serialize};
15
16#[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
26pub 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}