clerk_fapi_rs/apis/
environment_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 [`get_environment`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum GetEnvironmentError {
20    Status400(models::ClerkErrors),
21    Status401(models::ClerkErrors),
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`update_environment`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum UpdateEnvironmentError {
29    Status400(models::ClerkErrors),
30    UnknownValue(serde_json::Value),
31}
32
33/// Get the current environment. The environment contains information about the settings and features enabled for the current instance.
34pub async fn get_environment(
35    configuration: &configuration::Configuration,
36) -> Result<models::ClientEnvironment, Error<GetEnvironmentError>> {
37    let uri_str = format!("{}/v1/environment", configuration.base_path);
38    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
39
40    if let Some(ref apikey) = configuration.api_key {
41        let key = apikey.key.clone();
42        let value = match apikey.prefix {
43            Some(ref prefix) => format!("{prefix} {key}"),
44            None => key,
45        };
46        req_builder = req_builder.query(&[("__dev_session", value)]);
47    }
48    if let Some(ref user_agent) = configuration.user_agent {
49        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
50    }
51
52    let req = req_builder.build()?;
53    let resp = configuration.client.execute(req).await?;
54
55    let status = resp.status();
56    let content_type = resp
57        .headers()
58        .get("content-type")
59        .and_then(|v| v.to_str().ok())
60        .unwrap_or("application/octet-stream");
61    let content_type = super::ContentType::from(content_type);
62
63    if !status.is_client_error() && !status.is_server_error() {
64        let content = resp.text().await?;
65        match content_type {
66            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
67            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientEnvironment`"))),
68            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientEnvironment`")))),
69        }
70    } else {
71        let content = resp.text().await?;
72        let entity: Option<GetEnvironmentError> = serde_json::from_str(&content).ok();
73        Err(Error::ResponseError(ResponseContent {
74            status,
75            content,
76            entity,
77        }))
78    }
79}
80
81/// Update environment using request origin
82pub async fn update_environment(
83    configuration: &configuration::Configuration,
84    origin: &str,
85) -> Result<models::ClientEnvironment, Error<UpdateEnvironmentError>> {
86    // add a prefix to parameters to efficiently prevent name collisions
87    let p_header_origin = origin;
88
89    let uri_str = format!("{}/v1/environment", configuration.base_path);
90    let mut req_builder = configuration
91        .client
92        .request(reqwest::Method::PATCH, &uri_str);
93
94    if let Some(ref apikey) = configuration.api_key {
95        let key = apikey.key.clone();
96        let value = match apikey.prefix {
97            Some(ref prefix) => format!("{prefix} {key}"),
98            None => key,
99        };
100        req_builder = req_builder.query(&[("__dev_session", value)]);
101    }
102    if let Some(ref user_agent) = configuration.user_agent {
103        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
104    }
105    req_builder = req_builder.header("Origin", p_header_origin.to_string());
106
107    let req = req_builder.build()?;
108    let resp = configuration.client.execute(req).await?;
109
110    let status = resp.status();
111    let content_type = resp
112        .headers()
113        .get("content-type")
114        .and_then(|v| v.to_str().ok())
115        .unwrap_or("application/octet-stream");
116    let content_type = super::ContentType::from(content_type);
117
118    if !status.is_client_error() && !status.is_server_error() {
119        let content = resp.text().await?;
120        match content_type {
121            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
122            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientEnvironment`"))),
123            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientEnvironment`")))),
124        }
125    } else {
126        let content = resp.text().await?;
127        let entity: Option<UpdateEnvironmentError> = serde_json::from_str(&content).ok();
128        Err(Error::ResponseError(ResponseContent {
129            status,
130            content,
131            entity,
132        }))
133    }
134}