clerk_fapi_rs/apis/
sessions_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 [`attempt_session_reverification_first_factor`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum AttemptSessionReverificationFirstFactorError {
20    Status400(models::ClerkErrors),
21    Status403(models::ClerkErrors),
22    Status422(models::ClerkErrors),
23    UnknownValue(serde_json::Value),
24}
25
26/// struct for typed errors of method [`attempt_session_reverification_second_factor`]
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum AttemptSessionReverificationSecondFactorError {
30    Status400(models::ClerkErrors),
31    Status403(models::ClerkErrors),
32    Status422(models::ClerkErrors),
33    UnknownValue(serde_json::Value),
34}
35
36/// struct for typed errors of method [`create_session_token`]
37#[derive(Debug, Clone, Serialize, Deserialize)]
38#[serde(untagged)]
39pub enum CreateSessionTokenError {
40    Status401(models::ClerkErrors),
41    Status404(models::ClerkErrors),
42    UnknownValue(serde_json::Value),
43}
44
45/// struct for typed errors of method [`create_session_token_with_template`]
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum CreateSessionTokenWithTemplateError {
49    Status401(models::ClerkErrors),
50    Status404(models::ClerkErrors),
51    UnknownValue(serde_json::Value),
52}
53
54/// struct for typed errors of method [`end_session`]
55#[derive(Debug, Clone, Serialize, Deserialize)]
56#[serde(untagged)]
57pub enum EndSessionError {
58    Status400(models::ClerkErrors),
59    Status404(models::ClerkErrors),
60    UnknownValue(serde_json::Value),
61}
62
63/// struct for typed errors of method [`get_session`]
64#[derive(Debug, Clone, Serialize, Deserialize)]
65#[serde(untagged)]
66pub enum GetSessionError {
67    Status401(models::ClerkErrors),
68    Status404(models::ClerkErrors),
69    UnknownValue(serde_json::Value),
70}
71
72/// struct for typed errors of method [`prepare_session_reverification_first_factor`]
73#[derive(Debug, Clone, Serialize, Deserialize)]
74#[serde(untagged)]
75pub enum PrepareSessionReverificationFirstFactorError {
76    Status400(models::ClerkErrors),
77    Status403(models::ClerkErrors),
78    Status422(models::ClerkErrors),
79    UnknownValue(serde_json::Value),
80}
81
82/// struct for typed errors of method [`prepare_session_reverification_second_factor`]
83#[derive(Debug, Clone, Serialize, Deserialize)]
84#[serde(untagged)]
85pub enum PrepareSessionReverificationSecondFactorError {
86    Status400(models::ClerkErrors),
87    Status403(models::ClerkErrors),
88    Status422(models::ClerkErrors),
89    UnknownValue(serde_json::Value),
90}
91
92/// struct for typed errors of method [`remove_client_sessions_and_retain_cookie`]
93#[derive(Debug, Clone, Serialize, Deserialize)]
94#[serde(untagged)]
95pub enum RemoveClientSessionsAndRetainCookieError {
96    Status401(models::ClerkErrors),
97    Status404(models::ClerkErrors),
98    UnknownValue(serde_json::Value),
99}
100
101/// struct for typed errors of method [`remove_session`]
102#[derive(Debug, Clone, Serialize, Deserialize)]
103#[serde(untagged)]
104pub enum RemoveSessionError {
105    Status400(models::ClerkErrors),
106    Status404(models::ClerkErrors),
107    UnknownValue(serde_json::Value),
108}
109
110/// struct for typed errors of method [`start_session_reverification`]
111#[derive(Debug, Clone, Serialize, Deserialize)]
112#[serde(untagged)]
113pub enum StartSessionReverificationError {
114    Status401(models::ClerkErrors),
115    Status422(models::ClerkErrors),
116    UnknownValue(serde_json::Value),
117}
118
119/// struct for typed errors of method [`touch_session`]
120#[derive(Debug, Clone, Serialize, Deserialize)]
121#[serde(untagged)]
122pub enum TouchSessionError {
123    Status400(models::ClerkErrors),
124    Status401(models::ClerkErrors),
125    Status403(models::ClerkErrors),
126    Status404(models::ClerkErrors),
127    Status422(models::ClerkErrors),
128    UnknownValue(serde_json::Value),
129}
130
131/// Attempt the first factor verification. Requires the first factor verification to be prepared, unless you're using a password.  Parameter rules: If the strategy equals `email_code` then a code is required. If the strategy equals `password` then a password is required.
132pub async fn attempt_session_reverification_first_factor(
133    configuration: &configuration::Configuration,
134    session_id: &str,
135    strategy: &str,
136    origin: Option<&str>,
137    code: Option<&str>,
138    password: Option<&str>,
139    public_key_credential: Option<&str>,
140) -> Result<
141    models::ClientClientWrappedSessionReverification,
142    Error<AttemptSessionReverificationFirstFactorError>,
143> {
144    // add a prefix to parameters to efficiently prevent name collisions
145    let p_path_session_id = session_id;
146    let p_form_strategy = strategy;
147    let p_header_origin = origin;
148    let p_form_code = code;
149    let p_form_password = password;
150    let p_form_public_key_credential = public_key_credential;
151
152    let uri_str = format!(
153        "{}/v1/client/sessions/{session_id}/verify/attempt_first_factor",
154        configuration.base_path,
155        session_id = crate::apis::urlencode(p_path_session_id)
156    );
157    let mut req_builder = configuration
158        .client
159        .request(reqwest::Method::POST, &uri_str);
160
161    if let Some(ref apikey) = configuration.api_key {
162        let key = apikey.key.clone();
163        let value = match apikey.prefix {
164            Some(ref prefix) => format!("{prefix} {key}"),
165            None => key,
166        };
167        req_builder = req_builder.query(&[("__dev_session", value)]);
168    }
169    if let Some(ref user_agent) = configuration.user_agent {
170        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
171    }
172    if let Some(param_value) = p_header_origin {
173        req_builder = req_builder.header("Origin", param_value.to_string());
174    }
175    if let Some(ref apikey) = configuration.api_key {
176        let key = apikey.key.clone();
177        let value = match apikey.prefix {
178            Some(ref prefix) => format!("{prefix} {key}"),
179            None => key,
180        };
181        req_builder = req_builder.header("__session", value);
182    };
183    let mut multipart_form_params = std::collections::HashMap::new();
184    multipart_form_params.insert("strategy", p_form_strategy.to_string());
185    if let Some(param_value) = p_form_code {
186        multipart_form_params.insert("code", param_value.to_string());
187    }
188    if let Some(param_value) = p_form_password {
189        multipart_form_params.insert("password", param_value.to_string());
190    }
191    if let Some(param_value) = p_form_public_key_credential {
192        multipart_form_params.insert("public_key_credential", param_value.to_string());
193    }
194    req_builder = req_builder.form(&multipart_form_params);
195
196    let req = req_builder.build()?;
197    let resp = configuration.client.execute(req).await?;
198
199    let status = resp.status();
200    let content_type = resp
201        .headers()
202        .get("content-type")
203        .and_then(|v| v.to_str().ok())
204        .unwrap_or("application/octet-stream");
205    let content_type = super::ContentType::from(content_type);
206
207    if !status.is_client_error() && !status.is_server_error() {
208        let content = resp.text().await?;
209        match content_type {
210            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
211            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedSessionReverification`"))),
212            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedSessionReverification`")))),
213        }
214    } else {
215        let content = resp.text().await?;
216        let entity: Option<AttemptSessionReverificationFirstFactorError> =
217            serde_json::from_str(&content).ok();
218        Err(Error::ResponseError(ResponseContent {
219            status,
220            content,
221            entity,
222        }))
223    }
224}
225
226/// Attempt the second factor verification. Requires the `status` to be equal to `needs_second_factor` and for the preparation step to have been called.
227pub async fn attempt_session_reverification_second_factor(
228    configuration: &configuration::Configuration,
229    session_id: &str,
230    strategy: Option<&str>,
231    code: Option<&str>,
232) -> Result<
233    models::ClientClientWrappedSessionReverification,
234    Error<AttemptSessionReverificationSecondFactorError>,
235> {
236    // add a prefix to parameters to efficiently prevent name collisions
237    let p_path_session_id = session_id;
238    let p_form_strategy = strategy;
239    let p_form_code = code;
240
241    let uri_str = format!(
242        "{}/v1/client/sessions/{session_id}/verify/attempt_second_factor",
243        configuration.base_path,
244        session_id = crate::apis::urlencode(p_path_session_id)
245    );
246    let mut req_builder = configuration
247        .client
248        .request(reqwest::Method::POST, &uri_str);
249
250    if let Some(ref apikey) = configuration.api_key {
251        let key = apikey.key.clone();
252        let value = match apikey.prefix {
253            Some(ref prefix) => format!("{prefix} {key}"),
254            None => key,
255        };
256        req_builder = req_builder.query(&[("__dev_session", value)]);
257    }
258    if let Some(ref user_agent) = configuration.user_agent {
259        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
260    }
261    if let Some(ref apikey) = configuration.api_key {
262        let key = apikey.key.clone();
263        let value = match apikey.prefix {
264            Some(ref prefix) => format!("{prefix} {key}"),
265            None => key,
266        };
267        req_builder = req_builder.header("__session", value);
268    };
269    let mut multipart_form_params = std::collections::HashMap::new();
270    if let Some(param_value) = p_form_strategy {
271        multipart_form_params.insert("strategy", param_value.to_string());
272    }
273    if let Some(param_value) = p_form_code {
274        multipart_form_params.insert("code", param_value.to_string());
275    }
276    req_builder = req_builder.form(&multipart_form_params);
277
278    let req = req_builder.build()?;
279    let resp = configuration.client.execute(req).await?;
280
281    let status = resp.status();
282    let content_type = resp
283        .headers()
284        .get("content-type")
285        .and_then(|v| v.to_str().ok())
286        .unwrap_or("application/octet-stream");
287    let content_type = super::ContentType::from(content_type);
288
289    if !status.is_client_error() && !status.is_server_error() {
290        let content = resp.text().await?;
291        match content_type {
292            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
293            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedSessionReverification`"))),
294            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedSessionReverification`")))),
295        }
296    } else {
297        let content = resp.text().await?;
298        let entity: Option<AttemptSessionReverificationSecondFactorError> =
299            serde_json::from_str(&content).ok();
300        Err(Error::ResponseError(ResponseContent {
301            status,
302            content,
303            entity,
304        }))
305    }
306}
307
308/// Create a session jwt for the authenticated requested user.  When force organization selection is enabled and `organization_id` is sent as null or empty string, the token will be created with the previous active organization and will not attempt to switch to a personal account.
309pub async fn create_session_token(
310    configuration: &configuration::Configuration,
311    session_id: &str,
312    organization_id: Option<&str>,
313) -> Result<models::CreateSessionToken200Response, Error<CreateSessionTokenError>> {
314    // add a prefix to parameters to efficiently prevent name collisions
315    let p_path_session_id = session_id;
316    let p_form_organization_id = organization_id;
317
318    let uri_str = format!(
319        "{}/v1/client/sessions/{session_id}/tokens",
320        configuration.base_path,
321        session_id = crate::apis::urlencode(p_path_session_id)
322    );
323    let mut req_builder = configuration
324        .client
325        .request(reqwest::Method::POST, &uri_str);
326
327    if let Some(ref apikey) = configuration.api_key {
328        let key = apikey.key.clone();
329        let value = match apikey.prefix {
330            Some(ref prefix) => format!("{prefix} {key}"),
331            None => key,
332        };
333        req_builder = req_builder.query(&[("__dev_session", value)]);
334    }
335    if let Some(ref user_agent) = configuration.user_agent {
336        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
337    }
338    if let Some(ref apikey) = configuration.api_key {
339        let key = apikey.key.clone();
340        let value = match apikey.prefix {
341            Some(ref prefix) => format!("{prefix} {key}"),
342            None => key,
343        };
344        req_builder = req_builder.header("__session", value);
345    };
346    let mut multipart_form_params = std::collections::HashMap::new();
347    if let Some(param_value) = p_form_organization_id {
348        multipart_form_params.insert("organization_id", param_value.to_string());
349    }
350    req_builder = req_builder.form(&multipart_form_params);
351
352    let req = req_builder.build()?;
353    let resp = configuration.client.execute(req).await?;
354
355    let status = resp.status();
356    let content_type = resp
357        .headers()
358        .get("content-type")
359        .and_then(|v| v.to_str().ok())
360        .unwrap_or("application/octet-stream");
361    let content_type = super::ContentType::from(content_type);
362
363    if !status.is_client_error() && !status.is_server_error() {
364        let content = resp.text().await?;
365        match content_type {
366            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
367            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateSessionToken200Response`"))),
368            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateSessionToken200Response`")))),
369        }
370    } else {
371        let content = resp.text().await?;
372        let entity: Option<CreateSessionTokenError> = serde_json::from_str(&content).ok();
373        Err(Error::ResponseError(ResponseContent {
374            status,
375            content,
376            entity,
377        }))
378    }
379}
380
381/// Create a session jwt for the authenticated requested user.
382pub async fn create_session_token_with_template(
383    configuration: &configuration::Configuration,
384    session_id: &str,
385    template_name: &str,
386) -> Result<models::CreateSessionToken200Response, Error<CreateSessionTokenWithTemplateError>> {
387    // add a prefix to parameters to efficiently prevent name collisions
388    let p_path_session_id = session_id;
389    let p_path_template_name = template_name;
390
391    let uri_str = format!(
392        "{}/v1/client/sessions/{session_id}/tokens/{template_name}",
393        configuration.base_path,
394        session_id = crate::apis::urlencode(p_path_session_id),
395        template_name = crate::apis::urlencode(p_path_template_name)
396    );
397    let mut req_builder = configuration
398        .client
399        .request(reqwest::Method::POST, &uri_str);
400
401    if let Some(ref apikey) = configuration.api_key {
402        let key = apikey.key.clone();
403        let value = match apikey.prefix {
404            Some(ref prefix) => format!("{prefix} {key}"),
405            None => key,
406        };
407        req_builder = req_builder.query(&[("__dev_session", value)]);
408    }
409    if let Some(ref user_agent) = configuration.user_agent {
410        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
411    }
412    if let Some(ref apikey) = configuration.api_key {
413        let key = apikey.key.clone();
414        let value = match apikey.prefix {
415            Some(ref prefix) => format!("{prefix} {key}"),
416            None => key,
417        };
418        req_builder = req_builder.header("__session", value);
419    };
420
421    let req = req_builder.build()?;
422    let resp = configuration.client.execute(req).await?;
423
424    let status = resp.status();
425    let content_type = resp
426        .headers()
427        .get("content-type")
428        .and_then(|v| v.to_str().ok())
429        .unwrap_or("application/octet-stream");
430    let content_type = super::ContentType::from(content_type);
431
432    if !status.is_client_error() && !status.is_server_error() {
433        let content = resp.text().await?;
434        match content_type {
435            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
436            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateSessionToken200Response`"))),
437            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateSessionToken200Response`")))),
438        }
439    } else {
440        let content = resp.text().await?;
441        let entity: Option<CreateSessionTokenWithTemplateError> =
442            serde_json::from_str(&content).ok();
443        Err(Error::ResponseError(ResponseContent {
444            status,
445            content,
446            entity,
447        }))
448    }
449}
450
451/// Marks the given session as ended.
452pub async fn end_session(
453    configuration: &configuration::Configuration,
454    session_id: &str,
455) -> Result<models::ClientClientWrappedSession, Error<EndSessionError>> {
456    // add a prefix to parameters to efficiently prevent name collisions
457    let p_path_session_id = session_id;
458
459    let uri_str = format!(
460        "{}/v1/client/sessions/{session_id}/end",
461        configuration.base_path,
462        session_id = crate::apis::urlencode(p_path_session_id)
463    );
464    let mut req_builder = configuration
465        .client
466        .request(reqwest::Method::POST, &uri_str);
467
468    if let Some(ref apikey) = configuration.api_key {
469        let key = apikey.key.clone();
470        let value = match apikey.prefix {
471            Some(ref prefix) => format!("{prefix} {key}"),
472            None => key,
473        };
474        req_builder = req_builder.query(&[("__dev_session", value)]);
475    }
476    if let Some(ref user_agent) = configuration.user_agent {
477        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
478    }
479    if let Some(ref apikey) = configuration.api_key {
480        let key = apikey.key.clone();
481        let value = match apikey.prefix {
482            Some(ref prefix) => format!("{prefix} {key}"),
483            None => key,
484        };
485        req_builder = req_builder.header("__session", value);
486    };
487
488    let req = req_builder.build()?;
489    let resp = configuration.client.execute(req).await?;
490
491    let status = resp.status();
492    let content_type = resp
493        .headers()
494        .get("content-type")
495        .and_then(|v| v.to_str().ok())
496        .unwrap_or("application/octet-stream");
497    let content_type = super::ContentType::from(content_type);
498
499    if !status.is_client_error() && !status.is_server_error() {
500        let content = resp.text().await?;
501        match content_type {
502            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
503            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedSession`"))),
504            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedSession`")))),
505        }
506    } else {
507        let content = resp.text().await?;
508        let entity: Option<EndSessionError> = serde_json::from_str(&content).ok();
509        Err(Error::ResponseError(ResponseContent {
510            status,
511            content,
512            entity,
513        }))
514    }
515}
516
517/// Returns the session with the given id
518pub async fn get_session(
519    configuration: &configuration::Configuration,
520    session_id: &str,
521) -> Result<models::ClientClientWrappedSession, Error<GetSessionError>> {
522    // add a prefix to parameters to efficiently prevent name collisions
523    let p_path_session_id = session_id;
524
525    let uri_str = format!(
526        "{}/v1/client/sessions/{session_id}",
527        configuration.base_path,
528        session_id = crate::apis::urlencode(p_path_session_id)
529    );
530    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
531
532    if let Some(ref apikey) = configuration.api_key {
533        let key = apikey.key.clone();
534        let value = match apikey.prefix {
535            Some(ref prefix) => format!("{prefix} {key}"),
536            None => key,
537        };
538        req_builder = req_builder.query(&[("__dev_session", value)]);
539    }
540    if let Some(ref user_agent) = configuration.user_agent {
541        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
542    }
543    if let Some(ref apikey) = configuration.api_key {
544        let key = apikey.key.clone();
545        let value = match apikey.prefix {
546            Some(ref prefix) => format!("{prefix} {key}"),
547            None => key,
548        };
549        req_builder = req_builder.header("__session", value);
550    };
551
552    let req = req_builder.build()?;
553    let resp = configuration.client.execute(req).await?;
554
555    let status = resp.status();
556    let content_type = resp
557        .headers()
558        .get("content-type")
559        .and_then(|v| v.to_str().ok())
560        .unwrap_or("application/octet-stream");
561    let content_type = super::ContentType::from(content_type);
562
563    if !status.is_client_error() && !status.is_server_error() {
564        let content = resp.text().await?;
565        match content_type {
566            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
567            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedSession`"))),
568            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedSession`")))),
569        }
570    } else {
571        let content = resp.text().await?;
572        let entity: Option<GetSessionError> = serde_json::from_str(&content).ok();
573        Err(Error::ResponseError(ResponseContent {
574            status,
575            content,
576            entity,
577        }))
578    }
579}
580
581/// Prepare the first factor verification. Depending on the strategy, this request will do something different.  Parameter actions: If the strategy equals email_code then this request will send an email with an OTP code. If the strategy equals phone_code then this request will send an SMS with an OTP code.
582pub async fn prepare_session_reverification_first_factor(
583    configuration: &configuration::Configuration,
584    session_id: &str,
585    origin: Option<&str>,
586    strategy: Option<&str>,
587    email_address_id: Option<&str>,
588    phone_number_id: Option<&str>,
589) -> Result<
590    models::ClientClientWrappedSessionReverification,
591    Error<PrepareSessionReverificationFirstFactorError>,
592> {
593    // add a prefix to parameters to efficiently prevent name collisions
594    let p_path_session_id = session_id;
595    let p_header_origin = origin;
596    let p_form_strategy = strategy;
597    let p_form_email_address_id = email_address_id;
598    let p_form_phone_number_id = phone_number_id;
599
600    let uri_str = format!(
601        "{}/v1/client/sessions/{session_id}/verify/prepare_first_factor",
602        configuration.base_path,
603        session_id = crate::apis::urlencode(p_path_session_id)
604    );
605    let mut req_builder = configuration
606        .client
607        .request(reqwest::Method::POST, &uri_str);
608
609    if let Some(ref apikey) = configuration.api_key {
610        let key = apikey.key.clone();
611        let value = match apikey.prefix {
612            Some(ref prefix) => format!("{prefix} {key}"),
613            None => key,
614        };
615        req_builder = req_builder.query(&[("__dev_session", value)]);
616    }
617    if let Some(ref user_agent) = configuration.user_agent {
618        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
619    }
620    if let Some(param_value) = p_header_origin {
621        req_builder = req_builder.header("Origin", param_value.to_string());
622    }
623    if let Some(ref apikey) = configuration.api_key {
624        let key = apikey.key.clone();
625        let value = match apikey.prefix {
626            Some(ref prefix) => format!("{prefix} {key}"),
627            None => key,
628        };
629        req_builder = req_builder.header("__session", value);
630    };
631    let mut multipart_form_params = std::collections::HashMap::new();
632    if let Some(param_value) = p_form_strategy {
633        multipart_form_params.insert("strategy", param_value.to_string());
634    }
635    if let Some(param_value) = p_form_email_address_id {
636        multipart_form_params.insert("email_address_id", param_value.to_string());
637    }
638    if let Some(param_value) = p_form_phone_number_id {
639        multipart_form_params.insert("phone_number_id", param_value.to_string());
640    }
641    req_builder = req_builder.form(&multipart_form_params);
642
643    let req = req_builder.build()?;
644    let resp = configuration.client.execute(req).await?;
645
646    let status = resp.status();
647    let content_type = resp
648        .headers()
649        .get("content-type")
650        .and_then(|v| v.to_str().ok())
651        .unwrap_or("application/octet-stream");
652    let content_type = super::ContentType::from(content_type);
653
654    if !status.is_client_error() && !status.is_server_error() {
655        let content = resp.text().await?;
656        match content_type {
657            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
658            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedSessionReverification`"))),
659            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedSessionReverification`")))),
660        }
661    } else {
662        let content = resp.text().await?;
663        let entity: Option<PrepareSessionReverificationFirstFactorError> =
664            serde_json::from_str(&content).ok();
665        Err(Error::ResponseError(ResponseContent {
666            status,
667            content,
668            entity,
669        }))
670    }
671}
672
673/// Prepare the second factor verification. Requires the `status` to be equal to `needs_second_factor`.
674pub async fn prepare_session_reverification_second_factor(
675    configuration: &configuration::Configuration,
676    session_id: &str,
677    strategy: Option<&str>,
678    phone_number_id: Option<&str>,
679) -> Result<
680    models::ClientClientWrappedSessionReverification,
681    Error<PrepareSessionReverificationSecondFactorError>,
682> {
683    // add a prefix to parameters to efficiently prevent name collisions
684    let p_path_session_id = session_id;
685    let p_form_strategy = strategy;
686    let p_form_phone_number_id = phone_number_id;
687
688    let uri_str = format!(
689        "{}/v1/client/sessions/{session_id}/verify/prepare_second_factor",
690        configuration.base_path,
691        session_id = crate::apis::urlencode(p_path_session_id)
692    );
693    let mut req_builder = configuration
694        .client
695        .request(reqwest::Method::POST, &uri_str);
696
697    if let Some(ref apikey) = configuration.api_key {
698        let key = apikey.key.clone();
699        let value = match apikey.prefix {
700            Some(ref prefix) => format!("{prefix} {key}"),
701            None => key,
702        };
703        req_builder = req_builder.query(&[("__dev_session", value)]);
704    }
705    if let Some(ref user_agent) = configuration.user_agent {
706        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
707    }
708    if let Some(ref apikey) = configuration.api_key {
709        let key = apikey.key.clone();
710        let value = match apikey.prefix {
711            Some(ref prefix) => format!("{prefix} {key}"),
712            None => key,
713        };
714        req_builder = req_builder.header("__session", value);
715    };
716    let mut multipart_form_params = std::collections::HashMap::new();
717    if let Some(param_value) = p_form_strategy {
718        multipart_form_params.insert("strategy", param_value.to_string());
719    }
720    if let Some(param_value) = p_form_phone_number_id {
721        multipart_form_params.insert("phone_number_id", param_value.to_string());
722    }
723    req_builder = req_builder.form(&multipart_form_params);
724
725    let req = req_builder.build()?;
726    let resp = configuration.client.execute(req).await?;
727
728    let status = resp.status();
729    let content_type = resp
730        .headers()
731        .get("content-type")
732        .and_then(|v| v.to_str().ok())
733        .unwrap_or("application/octet-stream");
734    let content_type = super::ContentType::from(content_type);
735
736    if !status.is_client_error() && !status.is_server_error() {
737        let content = resp.text().await?;
738        match content_type {
739            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
740            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedSessionReverification`"))),
741            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedSessionReverification`")))),
742        }
743    } else {
744        let content = resp.text().await?;
745        let entity: Option<PrepareSessionReverificationSecondFactorError> =
746            serde_json::from_str(&content).ok();
747        Err(Error::ResponseError(ResponseContent {
748            status,
749            content,
750            entity,
751        }))
752    }
753}
754
755/// Removes all the sessions of the current client without removing the __client cookie
756pub async fn remove_client_sessions_and_retain_cookie(
757    configuration: &configuration::Configuration,
758) -> Result<models::ClientDeleteSession, Error<RemoveClientSessionsAndRetainCookieError>> {
759    let uri_str = format!("{}/v1/client/sessions", configuration.base_path);
760    let mut req_builder = configuration
761        .client
762        .request(reqwest::Method::DELETE, &uri_str);
763
764    if let Some(ref apikey) = configuration.api_key {
765        let key = apikey.key.clone();
766        let value = match apikey.prefix {
767            Some(ref prefix) => format!("{prefix} {key}"),
768            None => key,
769        };
770        req_builder = req_builder.query(&[("__dev_session", value)]);
771    }
772    if let Some(ref user_agent) = configuration.user_agent {
773        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
774    }
775    if let Some(ref apikey) = configuration.api_key {
776        let key = apikey.key.clone();
777        let value = match apikey.prefix {
778            Some(ref prefix) => format!("{prefix} {key}"),
779            None => key,
780        };
781        req_builder = req_builder.header("__session", value);
782    };
783
784    let req = req_builder.build()?;
785    let resp = configuration.client.execute(req).await?;
786
787    let status = resp.status();
788    let content_type = resp
789        .headers()
790        .get("content-type")
791        .and_then(|v| v.to_str().ok())
792        .unwrap_or("application/octet-stream");
793    let content_type = super::ContentType::from(content_type);
794
795    if !status.is_client_error() && !status.is_server_error() {
796        let content = resp.text().await?;
797        match content_type {
798            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
799            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientDeleteSession`"))),
800            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`")))),
801        }
802    } else {
803        let content = resp.text().await?;
804        let entity: Option<RemoveClientSessionsAndRetainCookieError> =
805            serde_json::from_str(&content).ok();
806        Err(Error::ResponseError(ResponseContent {
807            status,
808            content,
809            entity,
810        }))
811    }
812}
813
814/// Delete the given session.
815pub async fn remove_session(
816    configuration: &configuration::Configuration,
817    session_id: &str,
818) -> Result<models::ClientClientWrappedSession, Error<RemoveSessionError>> {
819    // add a prefix to parameters to efficiently prevent name collisions
820    let p_path_session_id = session_id;
821
822    let uri_str = format!(
823        "{}/v1/client/sessions/{session_id}/remove",
824        configuration.base_path,
825        session_id = crate::apis::urlencode(p_path_session_id)
826    );
827    let mut req_builder = configuration
828        .client
829        .request(reqwest::Method::POST, &uri_str);
830
831    if let Some(ref apikey) = configuration.api_key {
832        let key = apikey.key.clone();
833        let value = match apikey.prefix {
834            Some(ref prefix) => format!("{prefix} {key}"),
835            None => key,
836        };
837        req_builder = req_builder.query(&[("__dev_session", value)]);
838    }
839    if let Some(ref user_agent) = configuration.user_agent {
840        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
841    }
842    if let Some(ref apikey) = configuration.api_key {
843        let key = apikey.key.clone();
844        let value = match apikey.prefix {
845            Some(ref prefix) => format!("{prefix} {key}"),
846            None => key,
847        };
848        req_builder = req_builder.header("__session", value);
849    };
850
851    let req = req_builder.build()?;
852    let resp = configuration.client.execute(req).await?;
853
854    let status = resp.status();
855    let content_type = resp
856        .headers()
857        .get("content-type")
858        .and_then(|v| v.to_str().ok())
859        .unwrap_or("application/octet-stream");
860    let content_type = super::ContentType::from(content_type);
861
862    if !status.is_client_error() && !status.is_server_error() {
863        let content = resp.text().await?;
864        match content_type {
865            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
866            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedSession`"))),
867            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedSession`")))),
868        }
869    } else {
870        let content = resp.text().await?;
871        let entity: Option<RemoveSessionError> = serde_json::from_str(&content).ok();
872        Err(Error::ResponseError(ResponseContent {
873            status,
874            content,
875            entity,
876        }))
877    }
878}
879
880/// Start a new session reverification flow by providing a verification level.  If the requested level equals 'secondFactor' or 'multiFactor' and the associated user doesn't have any available second factor, then we fall back to 'firstFactor'
881pub async fn start_session_reverification(
882    configuration: &configuration::Configuration,
883    session_id: &str,
884    level: &str,
885) -> Result<models::ClientClientWrappedSessionReverification, Error<StartSessionReverificationError>>
886{
887    // add a prefix to parameters to efficiently prevent name collisions
888    let p_path_session_id = session_id;
889    let p_form_level = level;
890
891    let uri_str = format!(
892        "{}/v1/client/sessions/{session_id}/verify",
893        configuration.base_path,
894        session_id = crate::apis::urlencode(p_path_session_id)
895    );
896    let mut req_builder = configuration
897        .client
898        .request(reqwest::Method::POST, &uri_str);
899
900    if let Some(ref apikey) = configuration.api_key {
901        let key = apikey.key.clone();
902        let value = match apikey.prefix {
903            Some(ref prefix) => format!("{prefix} {key}"),
904            None => key,
905        };
906        req_builder = req_builder.query(&[("__dev_session", value)]);
907    }
908    if let Some(ref user_agent) = configuration.user_agent {
909        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
910    }
911    if let Some(ref apikey) = configuration.api_key {
912        let key = apikey.key.clone();
913        let value = match apikey.prefix {
914            Some(ref prefix) => format!("{prefix} {key}"),
915            None => key,
916        };
917        req_builder = req_builder.header("__session", value);
918    };
919    let mut multipart_form_params = std::collections::HashMap::new();
920    multipart_form_params.insert("level", p_form_level.to_string());
921    req_builder = req_builder.form(&multipart_form_params);
922
923    let req = req_builder.build()?;
924    let resp = configuration.client.execute(req).await?;
925
926    let status = resp.status();
927    let content_type = resp
928        .headers()
929        .get("content-type")
930        .and_then(|v| v.to_str().ok())
931        .unwrap_or("application/octet-stream");
932    let content_type = super::ContentType::from(content_type);
933
934    if !status.is_client_error() && !status.is_server_error() {
935        let content = resp.text().await?;
936        match content_type {
937            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
938            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedSessionReverification`"))),
939            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedSessionReverification`")))),
940        }
941    } else {
942        let content = resp.text().await?;
943        let entity: Option<StartSessionReverificationError> = serde_json::from_str(&content).ok();
944        Err(Error::ResponseError(ResponseContent {
945            status,
946            content,
947            entity,
948        }))
949    }
950}
951
952/// Specify the active session for the client.  When force organization selection is enabled and `active_organization_id` is sent as null or empty string, the session will keep the previous active organization and will not attempt to switch to a personal account.
953pub async fn touch_session(
954    configuration: &configuration::Configuration,
955    session_id: &str,
956    active_organization_id: Option<&str>,
957) -> Result<models::ClientClientWrappedSession, Error<TouchSessionError>> {
958    // add a prefix to parameters to efficiently prevent name collisions
959    let p_path_session_id = session_id;
960    let p_form_active_organization_id = active_organization_id;
961
962    let uri_str = format!(
963        "{}/v1/client/sessions/{session_id}/touch",
964        configuration.base_path,
965        session_id = crate::apis::urlencode(p_path_session_id)
966    );
967    let mut req_builder = configuration
968        .client
969        .request(reqwest::Method::POST, &uri_str);
970
971    if let Some(ref apikey) = configuration.api_key {
972        let key = apikey.key.clone();
973        let value = match apikey.prefix {
974            Some(ref prefix) => format!("{prefix} {key}"),
975            None => key,
976        };
977        req_builder = req_builder.query(&[("__dev_session", value)]);
978    }
979    if let Some(ref user_agent) = configuration.user_agent {
980        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
981    }
982    if let Some(ref apikey) = configuration.api_key {
983        let key = apikey.key.clone();
984        let value = match apikey.prefix {
985            Some(ref prefix) => format!("{prefix} {key}"),
986            None => key,
987        };
988        req_builder = req_builder.header("__session", value);
989    };
990    let mut multipart_form_params = std::collections::HashMap::new();
991    if let Some(param_value) = p_form_active_organization_id {
992        multipart_form_params.insert("active_organization_id", param_value.to_string());
993    }
994    req_builder = req_builder.form(&multipart_form_params);
995
996    let req = req_builder.build()?;
997    let resp = configuration.client.execute(req).await?;
998
999    let status = resp.status();
1000    let content_type = resp
1001        .headers()
1002        .get("content-type")
1003        .and_then(|v| v.to_str().ok())
1004        .unwrap_or("application/octet-stream");
1005    let content_type = super::ContentType::from(content_type);
1006
1007    if !status.is_client_error() && !status.is_server_error() {
1008        let content = resp.text().await?;
1009        match content_type {
1010            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
1011            ContentType::Text => Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClientClientWrappedSession`"))),
1012            ContentType::Unsupported(unknown_type) => Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ClientClientWrappedSession`")))),
1013        }
1014    } else {
1015        let content = resp.text().await?;
1016        let entity: Option<TouchSessionError> = serde_json::from_str(&content).ok();
1017        Err(Error::ResponseError(ResponseContent {
1018            status,
1019            content,
1020            entity,
1021        }))
1022    }
1023}