cortex_client/apis/
authentication_api.rs1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum LoginError {
22 Status400(models::Error),
23 Status401(models::Error),
24 Status520(models::Error),
25 UnknownValue(serde_json::Value),
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
30#[serde(untagged)]
31pub enum LogoutError {
32 UnknownValue(serde_json::Value),
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
37#[serde(untagged)]
38pub enum SsoLoginError {
39 Status401(models::Error),
40 Status520(),
41 UnknownValue(serde_json::Value),
42}
43
44
45pub async fn login(configuration: &configuration::Configuration, login_request: models::LoginRequest) -> Result<models::AuthContext, Error<LoginError>> {
46 let p_login_request = login_request;
48
49 let uri_str = format!("{}/login", configuration.base_path);
50 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
51
52 if let Some(ref user_agent) = configuration.user_agent {
53 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
54 }
55 if let Some(ref token) = configuration.bearer_access_token {
56 req_builder = req_builder.bearer_auth(token.to_owned());
57 };
58 req_builder = req_builder.json(&p_login_request);
59
60 let req = req_builder.build()?;
61 let resp = configuration.client.execute(req).await?;
62
63 let status = resp.status();
64 let content_type = resp
65 .headers()
66 .get("content-type")
67 .and_then(|v| v.to_str().ok())
68 .unwrap_or("application/octet-stream");
69 let content_type = super::ContentType::from(content_type);
70
71 if !status.is_client_error() && !status.is_server_error() {
72 let content = resp.text().await?;
73 match content_type {
74 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
75 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AuthContext`"))),
76 ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AuthContext`")))),
77 }
78 } else {
79 let content = resp.text().await?;
80 let entity: Option<LoginError> = serde_json::from_str(&content).ok();
81 Err(Error::ResponseError(ResponseContent { status, content, entity }))
82 }
83}
84
85pub async fn logout(configuration: &configuration::Configuration, ) -> Result<(), Error<LogoutError>> {
86
87 let uri_str = format!("{}/logout", configuration.base_path);
88 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
89
90 if let Some(ref user_agent) = configuration.user_agent {
91 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
92 }
93 if let Some(ref token) = configuration.bearer_access_token {
94 req_builder = req_builder.bearer_auth(token.to_owned());
95 };
96
97 let req = req_builder.build()?;
98 let resp = configuration.client.execute(req).await?;
99
100 let status = resp.status();
101
102 if !status.is_client_error() && !status.is_server_error() {
103 Ok(())
104 } else {
105 let content = resp.text().await?;
106 let entity: Option<LogoutError> = serde_json::from_str(&content).ok();
107 Err(Error::ResponseError(ResponseContent { status, content, entity }))
108 }
109}
110
111pub async fn sso_login(configuration: &configuration::Configuration, ) -> Result<models::AuthContext, Error<SsoLoginError>> {
112
113 let uri_str = format!("{}/ssoLogin", configuration.base_path);
114 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
115
116 if let Some(ref user_agent) = configuration.user_agent {
117 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
118 }
119 if let Some(ref token) = configuration.bearer_access_token {
120 req_builder = req_builder.bearer_auth(token.to_owned());
121 };
122
123 let req = req_builder.build()?;
124 let resp = configuration.client.execute(req).await?;
125
126 let status = resp.status();
127 let content_type = resp
128 .headers()
129 .get("content-type")
130 .and_then(|v| v.to_str().ok())
131 .unwrap_or("application/octet-stream");
132 let content_type = super::ContentType::from(content_type);
133
134 if !status.is_client_error() && !status.is_server_error() {
135 let content = resp.text().await?;
136 match content_type {
137 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
138 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AuthContext`"))),
139 ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AuthContext`")))),
140 }
141 } else {
142 let content = resp.text().await?;
143 let entity: Option<SsoLoginError> = serde_json::from_str(&content).ok();
144 Err(Error::ResponseError(ResponseContent { status, content, entity }))
145 }
146}
147