clerk_fapi_rs/apis/
client_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 [`delete_client_sessions`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum DeleteClientSessionsError {
20    Status404(models::ClerkErrors),
21    UnknownValue(serde_json::Value),
22}
23
24/// struct for typed errors of method [`get_client`]
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(untagged)]
27pub enum GetClientError {
28    Status400(models::ClerkErrors),
29    Status401(models::ClerkErrors),
30    UnknownValue(serde_json::Value),
31}
32
33/// struct for typed errors of method [`handshake_client`]
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(untagged)]
36pub enum HandshakeClientError {
37    Status400(models::ClerkErrors),
38    Status403(models::ClerkErrors),
39    Status404(models::ClerkErrors),
40    Status422(models::ClerkErrors),
41    Status500(models::ClerkErrors),
42    UnknownValue(serde_json::Value),
43}
44
45/// struct for typed errors of method [`post_client`]
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum PostClientError {
49    UnknownValue(serde_json::Value),
50}
51
52/// struct for typed errors of method [`put_client`]
53#[derive(Debug, Clone, Serialize, Deserialize)]
54#[serde(untagged)]
55pub enum PutClientError {
56    UnknownValue(serde_json::Value),
57}
58
59/// Ends all Sessions of the Current Client
60pub async fn delete_client_sessions(
61    configuration: &configuration::Configuration,
62) -> Result<models::ClientDeleteSession, Error<DeleteClientSessionsError>> {
63    let uri_str = format!("{}/v1/client", configuration.base_path);
64    let mut req_builder = configuration
65        .client
66        .request(reqwest::Method::DELETE, &uri_str);
67
68    if let Some(ref apikey) = configuration.api_key {
69        let key = apikey.key.clone();
70        let value = match apikey.prefix {
71            Some(ref prefix) => format!("{prefix} {key}"),
72            None => key,
73        };
74        req_builder = req_builder.query(&[("__dev_session", value)]);
75    }
76    if let Some(ref user_agent) = configuration.user_agent {
77        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
78    }
79    if let Some(ref apikey) = configuration.api_key {
80        let key = apikey.key.clone();
81        let value = match apikey.prefix {
82            Some(ref prefix) => format!("{prefix} {key}"),
83            None => key,
84        };
85        req_builder = req_builder.header("__session", value);
86    };
87
88    let req = req_builder.build()?;
89    let resp = configuration.client.execute(req).await?;
90
91    let status = resp.status();
92    let content_type = resp
93        .headers()
94        .get("content-type")
95        .and_then(|v| v.to_str().ok())
96        .unwrap_or("application/octet-stream");
97    let content_type = super::ContentType::from(content_type);
98
99    if !status.is_client_error() && !status.is_server_error() {
100        let content = resp.text().await?;
101        match content_type {
102            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
103            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientDeleteSession`"))),
104            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientDeleteSession`")))),
105        }
106    } else {
107        let content = resp.text().await?;
108        let entity: Option<DeleteClientSessionsError> = serde_json::from_str(&content).ok();
109        Err(Error::ResponseError(ResponseContent {
110            status,
111            content,
112            entity,
113        }))
114    }
115}
116
117/// Returns the current client that is present either in the browser cookies or authorization header.
118pub async fn get_client(
119    configuration: &configuration::Configuration,
120) -> Result<models::ClientClientWrappedClient, Error<GetClientError>> {
121    let uri_str = format!("{}/v1/client", configuration.base_path);
122    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
123
124    if let Some(ref apikey) = configuration.api_key {
125        let key = apikey.key.clone();
126        let value = match apikey.prefix {
127            Some(ref prefix) => format!("{prefix} {key}"),
128            None => key,
129        };
130        req_builder = req_builder.query(&[("__dev_session", value)]);
131    }
132    if let Some(ref user_agent) = configuration.user_agent {
133        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
134    }
135
136    let req = req_builder.build()?;
137    let resp = configuration.client.execute(req).await?;
138
139    let status = resp.status();
140    let content_type = resp
141        .headers()
142        .get("content-type")
143        .and_then(|v| v.to_str().ok())
144        .unwrap_or("application/octet-stream");
145    let content_type = super::ContentType::from(content_type);
146
147    if !status.is_client_error() && !status.is_server_error() {
148        let content = resp.text().await?;
149        match content_type {
150            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
151            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedClient`"))),
152            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedClient`")))),
153        }
154    } else {
155        let content = resp.text().await?;
156        let entity: Option<GetClientError> = serde_json::from_str(&content).ok();
157        Err(Error::ResponseError(ResponseContent {
158            status,
159            content,
160            entity,
161        }))
162    }
163}
164
165/// When the authentication status cannot be determined from the current session token, we initiate a handshake to refresh the token and send it back to the application. Called in server environments.
166pub async fn handshake_client(
167    configuration: &configuration::Configuration,
168    clerk_proxy_url: Option<&str>,
169    clerk_secret_key: Option<&str>,
170    redirect_url: Option<&str>,
171    format: Option<&str>,
172    organization_id: Option<&str>,
173    satellite_fapi: Option<&str>,
174) -> Result<(), Error<HandshakeClientError>> {
175    // add a prefix to parameters to efficiently prevent name collisions
176    let p_header_clerk_proxy_url = clerk_proxy_url;
177    let p_header_clerk_secret_key = clerk_secret_key;
178    let p_query_redirect_url = redirect_url;
179    let p_query_format = format;
180    let p_query_organization_id = organization_id;
181    let p_query_satellite_fapi = satellite_fapi;
182
183    let uri_str = format!("{}/v1/client/handshake", configuration.base_path);
184    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
185
186    if let Some(ref param_value) = p_query_redirect_url {
187        req_builder = req_builder.query(&[("redirect_url", &param_value.to_string())]);
188    }
189    if let Some(ref param_value) = p_query_format {
190        req_builder = req_builder.query(&[("format", &param_value.to_string())]);
191    }
192    if let Some(ref param_value) = p_query_organization_id {
193        req_builder = req_builder.query(&[("organization_id", &param_value.to_string())]);
194    }
195    if let Some(ref param_value) = p_query_satellite_fapi {
196        req_builder = req_builder.query(&[("satellite_fapi", &param_value.to_string())]);
197    }
198    if let Some(ref apikey) = configuration.api_key {
199        let key = apikey.key.clone();
200        let value = match apikey.prefix {
201            Some(ref prefix) => format!("{prefix} {key}"),
202            None => key,
203        };
204        req_builder = req_builder.query(&[("__dev_session", value)]);
205    }
206    if let Some(ref user_agent) = configuration.user_agent {
207        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
208    }
209    if let Some(param_value) = p_header_clerk_proxy_url {
210        req_builder = req_builder.header("Clerk-Proxy-Url", param_value.to_string());
211    }
212    if let Some(param_value) = p_header_clerk_secret_key {
213        req_builder = req_builder.header("Clerk-Secret-Key", param_value.to_string());
214    }
215    if let Some(ref apikey) = configuration.api_key {
216        let key = apikey.key.clone();
217        let value = match apikey.prefix {
218            Some(ref prefix) => format!("{prefix} {key}"),
219            None => key,
220        };
221        req_builder = req_builder.header("__session", value);
222    };
223
224    let req = req_builder.build()?;
225    let resp = configuration.client.execute(req).await?;
226
227    let status = resp.status();
228
229    if !status.is_client_error() && !status.is_server_error() {
230        Ok(())
231    } else {
232        let content = resp.text().await?;
233        let entity: Option<HandshakeClientError> = serde_json::from_str(&content).ok();
234        Err(Error::ResponseError(ResponseContent {
235            status,
236            content,
237            entity,
238        }))
239    }
240}
241
242/// Creates a new Client and sets it either in the response cookies or the response authorization header.
243pub async fn post_client(
244    configuration: &configuration::Configuration,
245) -> Result<models::ClientClientWrappedClient, Error<PostClientError>> {
246    let uri_str = format!("{}/v1/client", configuration.base_path);
247    let mut req_builder = configuration
248        .client
249        .request(reqwest::Method::POST, &uri_str);
250
251    if let Some(ref apikey) = configuration.api_key {
252        let key = apikey.key.clone();
253        let value = match apikey.prefix {
254            Some(ref prefix) => format!("{prefix} {key}"),
255            None => key,
256        };
257        req_builder = req_builder.query(&[("__dev_session", value)]);
258    }
259    if let Some(ref user_agent) = configuration.user_agent {
260        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
261    }
262
263    let req = req_builder.build()?;
264    let resp = configuration.client.execute(req).await?;
265
266    let status = resp.status();
267    let content_type = resp
268        .headers()
269        .get("content-type")
270        .and_then(|v| v.to_str().ok())
271        .unwrap_or("application/octet-stream");
272    let content_type = super::ContentType::from(content_type);
273
274    if !status.is_client_error() && !status.is_server_error() {
275        let content = resp.text().await?;
276        match content_type {
277            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
278            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedClient`"))),
279            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedClient`")))),
280        }
281    } else {
282        let content = resp.text().await?;
283        let entity: Option<PostClientError> = serde_json::from_str(&content).ok();
284        Err(Error::ResponseError(ResponseContent {
285            status,
286            content,
287            entity,
288        }))
289    }
290}
291
292/// Creates a new Client and sets it either in the response cookies or the response authorization header.
293pub async fn put_client(
294    configuration: &configuration::Configuration,
295) -> Result<models::ClientClientWrappedClient, Error<PutClientError>> {
296    let uri_str = format!("{}/v1/client", configuration.base_path);
297    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
298
299    if let Some(ref apikey) = configuration.api_key {
300        let key = apikey.key.clone();
301        let value = match apikey.prefix {
302            Some(ref prefix) => format!("{prefix} {key}"),
303            None => key,
304        };
305        req_builder = req_builder.query(&[("__dev_session", value)]);
306    }
307    if let Some(ref user_agent) = configuration.user_agent {
308        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
309    }
310
311    let req = req_builder.build()?;
312    let resp = configuration.client.execute(req).await?;
313
314    let status = resp.status();
315    let content_type = resp
316        .headers()
317        .get("content-type")
318        .and_then(|v| v.to_str().ok())
319        .unwrap_or("application/octet-stream");
320    let content_type = super::ContentType::from(content_type);
321
322    if !status.is_client_error() && !status.is_server_error() {
323        let content = resp.text().await?;
324        match content_type {
325            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
326            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedClient`"))),
327            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedClient`")))),
328        }
329    } else {
330        let content = resp.text().await?;
331        let entity: Option<PutClientError> = serde_json::from_str(&content).ok();
332        Err(Error::ResponseError(ResponseContent {
333            status,
334            content,
335            entity,
336        }))
337    }
338}