open_logistics/shiprocket/apis/
auth_api.rs1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::shiprocket::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum AuthLoginError {
22 DefaultResponse(models::ShiprocketError),
23 UnknownValue(serde_json::Value),
24}
25
26
27pub async fn auth_login(configuration: &configuration::Configuration, auth_credentials: models::AuthCredentials) -> Result<models::AuthResponse, Error<AuthLoginError>> {
28 let p_body_auth_credentials = auth_credentials;
30
31 let uri_str = format!("{}/auth/login", configuration.base_path);
32 let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
33
34 if let Some(ref user_agent) = configuration.user_agent {
35 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
36 }
37 req_builder = req_builder.json(&p_body_auth_credentials);
38
39 let req = req_builder.build()?;
40 let resp = configuration.client.execute(req).await?;
41
42 let status = resp.status();
43 let content_type = resp
44 .headers()
45 .get("content-type")
46 .and_then(|v| v.to_str().ok())
47 .unwrap_or("application/octet-stream");
48 let content_type = super::ContentType::from(content_type);
49
50 if !status.is_client_error() && !status.is_server_error() {
51 let content = resp.text().await?;
52 match content_type {
53 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
54 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AuthResponse`"))),
55 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::AuthResponse`")))),
56 }
57 } else {
58 let content = resp.text().await?;
59 let entity: Option<AuthLoginError> = serde_json::from_str(&content).ok();
60 Err(Error::ResponseError(ResponseContent { status, content, entity }))
61 }
62}
63