open_logistics/delhivery/apis/
tracking_api.rs1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::delhivery::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18#[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 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", ¶m_value.to_string())]);
37 }
38 if let Some(ref param_value) = p_query_ref_nos {
39 req_builder = req_builder.query(&[("ref_nos", ¶m_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