Skip to main content

open_logistics/delhivery/apis/
tracking_api.rs

1/*
2 * Open Logistics — Delhivery
3 *
4 * OpenAPI description of the Delhivery B2C 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.  Delhivery overloads `POST /api/p/edit` for update and cancel, and `GET /api/v1/packages/json/` for single, order, and bulk tracking. OpenAPI permits one operation per path + method, so this spec exposes `orders_edit` and `tracking_fetch`; language facades may alias them as `update`, `cancel`, `by_waybill`, `by_order_id`, and `by_waybills`. 
5 *
6 * The version of the OpenAPI document: 0.3.0
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::delhivery::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`tracking_fetch`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum TrackingFetchError {
22    DefaultResponse(models::DelhiveryApiErrorBody),
23    UnknownValue(serde_json::Value),
24}
25
26
27pub async fn tracking_fetch(configuration: &configuration::Configuration, waybill: Option<&str>, ref_nos: Option<&str>) -> Result<models::DelhiveryTrackingResponse, Error<TrackingFetchError>> {
28    // add a prefix to parameters to efficiently prevent name collisions
29    let p_query_waybill = waybill;
30    let p_query_ref_nos = ref_nos;
31
32    let uri_str = format!("{}/api/v1/packages/json/", configuration.base_path);
33    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
34
35    if let Some(ref param_value) = p_query_waybill {
36        req_builder = req_builder.query(&[("waybill", &param_value.to_string())]);
37    }
38    if let Some(ref param_value) = p_query_ref_nos {
39        req_builder = req_builder.query(&[("ref_nos", &param_value.to_string())]);
40    }
41    if let Some(ref user_agent) = configuration.user_agent {
42        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
43    }
44    if let Some(ref apikey) = configuration.api_key {
45        let key = apikey.key.clone();
46        let value = match apikey.prefix {
47            Some(ref prefix) => format!("{} {}", prefix, key),
48            None => key,
49        };
50        req_builder = req_builder.header("Authorization", value);
51    };
52
53    let req = req_builder.build()?;
54    let resp = configuration.client.execute(req).await?;
55
56    let status = resp.status();
57    let content_type = resp
58        .headers()
59        .get("content-type")
60        .and_then(|v| v.to_str().ok())
61        .unwrap_or("application/octet-stream");
62    let content_type = super::ContentType::from(content_type);
63
64    if !status.is_client_error() && !status.is_server_error() {
65        let content = resp.text().await?;
66        match content_type {
67            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
68            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::DelhiveryTrackingResponse`"))),
69            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::DelhiveryTrackingResponse`")))),
70        }
71    } else {
72        let content = resp.text().await?;
73        let entity: Option<TrackingFetchError> = serde_json::from_str(&content).ok();
74        Err(Error::ResponseError(ResponseContent { status, content, entity }))
75    }
76}
77