Skip to main content

open_logistics/shiprocket/apis/
auth_api.rs

1/*
2 * Open Logistics — Shiprocket
3 *
4 * OpenAPI description of the Shiprocket endpoints wrapped by `open-logistics`.  Source of truth for generated Python and Rust clients. Operation IDs and schema field names are intentionally snake_case so every language exposes the same names.  Two TypeScript methods (`checkServiceability` and `calculateRates`) call `GET /courier/serviceability`; OpenAPI permits one operation per path + method, so this spec exposes a single `courier_serviceability` operation. Language facades may alias it as `calculate_rates`. 
5 *
6 * The version of the OpenAPI document: 0.2.2
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::shiprocket::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`auth_login`]
19#[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    // add a prefix to parameters to efficiently prevent name collisions
29    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