open_logistics/delhivery/apis/
serviceability_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 ServiceabilityCheckError {
22 DefaultResponse(models::DelhiveryApiErrorBody),
23 UnknownValue(serde_json::Value),
24}
25
26
27pub async fn serviceability_check(configuration: &configuration::Configuration, filter_codes: Option<&str>, dt: Option<&str>, st: Option<&str>) -> Result<models::DelhiveryServiceabilityResponse, Error<ServiceabilityCheckError>> {
28 let p_query_filter_codes = filter_codes;
30 let p_query_dt = dt;
31 let p_query_st = st;
32
33 let uri_str = format!("{}/c/api/pin-codes/json/", configuration.base_path);
34 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
35
36 if let Some(ref param_value) = p_query_filter_codes {
37 req_builder = req_builder.query(&[("filter_codes", ¶m_value.to_string())]);
38 }
39 if let Some(ref param_value) = p_query_dt {
40 req_builder = req_builder.query(&[("dt", ¶m_value.to_string())]);
41 }
42 if let Some(ref param_value) = p_query_st {
43 req_builder = req_builder.query(&[("st", ¶m_value.to_string())]);
44 }
45 if let Some(ref user_agent) = configuration.user_agent {
46 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
47 }
48 if let Some(ref apikey) = configuration.api_key {
49 let key = apikey.key.clone();
50 let value = match apikey.prefix {
51 Some(ref prefix) => format!("{} {}", prefix, key),
52 None => key,
53 };
54 req_builder = req_builder.header("Authorization", value);
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::DelhiveryServiceabilityResponse`"))),
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::DelhiveryServiceabilityResponse`")))),
74 }
75 } else {
76 let content = resp.text().await?;
77 let entity: Option<ServiceabilityCheckError> = serde_json::from_str(&content).ok();
78 Err(Error::ResponseError(ResponseContent { status, content, entity }))
79 }
80}
81