open_logistics/shiprocket/apis/
tracking_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 TrackingByAwbError {
22 DefaultResponse(models::ShiprocketError),
23 UnknownValue(serde_json::Value),
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum TrackingByOrderError {
30 DefaultResponse(models::ShiprocketError),
31 UnknownValue(serde_json::Value),
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
36#[serde(untagged)]
37pub enum TrackingByShipmentError {
38 DefaultResponse(models::ShiprocketError),
39 UnknownValue(serde_json::Value),
40}
41
42
43pub async fn tracking_by_awb(configuration: &configuration::Configuration, awb: &str) -> Result<models::TrackByAwbResponse, Error<TrackingByAwbError>> {
44 let p_path_awb = awb;
46
47 let uri_str = format!("{}/courier/track/awb/{awb}", configuration.base_path, awb=crate::shiprocket::apis::urlencode(p_path_awb));
48 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
49
50 if let Some(ref user_agent) = configuration.user_agent {
51 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
52 }
53 if let Some(ref token) = configuration.bearer_access_token {
54 req_builder = req_builder.bearer_auth(token.to_owned());
55 };
56
57 let req = req_builder.build()?;
58 let resp = configuration.client.execute(req).await?;
59
60 let status = resp.status();
61 let content_type = resp
62 .headers()
63 .get("content-type")
64 .and_then(|v| v.to_str().ok())
65 .unwrap_or("application/octet-stream");
66 let content_type = super::ContentType::from(content_type);
67
68 if !status.is_client_error() && !status.is_server_error() {
69 let content = resp.text().await?;
70 match content_type {
71 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
72 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TrackByAwbResponse`"))),
73 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::TrackByAwbResponse`")))),
74 }
75 } else {
76 let content = resp.text().await?;
77 let entity: Option<TrackingByAwbError> = serde_json::from_str(&content).ok();
78 Err(Error::ResponseError(ResponseContent { status, content, entity }))
79 }
80}
81
82pub async fn tracking_by_order(configuration: &configuration::Configuration, order_id: &str) -> Result<models::TrackByAwbResponse, Error<TrackingByOrderError>> {
83 let p_path_order_id = order_id;
85
86 let uri_str = format!("{}/orders/track/{orderId}", configuration.base_path, orderId=crate::shiprocket::apis::urlencode(p_path_order_id));
87 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
88
89 if let Some(ref user_agent) = configuration.user_agent {
90 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
91 }
92 if let Some(ref token) = configuration.bearer_access_token {
93 req_builder = req_builder.bearer_auth(token.to_owned());
94 };
95
96 let req = req_builder.build()?;
97 let resp = configuration.client.execute(req).await?;
98
99 let status = resp.status();
100 let content_type = resp
101 .headers()
102 .get("content-type")
103 .and_then(|v| v.to_str().ok())
104 .unwrap_or("application/octet-stream");
105 let content_type = super::ContentType::from(content_type);
106
107 if !status.is_client_error() && !status.is_server_error() {
108 let content = resp.text().await?;
109 match content_type {
110 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
111 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TrackByAwbResponse`"))),
112 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::TrackByAwbResponse`")))),
113 }
114 } else {
115 let content = resp.text().await?;
116 let entity: Option<TrackingByOrderError> = serde_json::from_str(&content).ok();
117 Err(Error::ResponseError(ResponseContent { status, content, entity }))
118 }
119}
120
121pub async fn tracking_by_shipment(configuration: &configuration::Configuration, shipment_id: i32) -> Result<models::TrackByAwbResponse, Error<TrackingByShipmentError>> {
122 let p_path_shipment_id = shipment_id;
124
125 let uri_str = format!("{}/courier/track/shipment/{shipmentId}", configuration.base_path, shipmentId=p_path_shipment_id);
126 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
127
128 if let Some(ref user_agent) = configuration.user_agent {
129 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
130 }
131 if let Some(ref token) = configuration.bearer_access_token {
132 req_builder = req_builder.bearer_auth(token.to_owned());
133 };
134
135 let req = req_builder.build()?;
136 let resp = configuration.client.execute(req).await?;
137
138 let status = resp.status();
139 let content_type = resp
140 .headers()
141 .get("content-type")
142 .and_then(|v| v.to_str().ok())
143 .unwrap_or("application/octet-stream");
144 let content_type = super::ContentType::from(content_type);
145
146 if !status.is_client_error() && !status.is_server_error() {
147 let content = resp.text().await?;
148 match content_type {
149 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
150 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TrackByAwbResponse`"))),
151 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::TrackByAwbResponse`")))),
152 }
153 } else {
154 let content = resp.text().await?;
155 let entity: Option<TrackingByShipmentError> = serde_json::from_str(&content).ok();
156 Err(Error::ResponseError(ResponseContent { status, content, entity }))
157 }
158}
159