motis_openapi_sdk/apis/
geocode_api.rs

1/*
2 * MOTIS API
3 *
4 * This is the MOTIS routing API.
5 *
6 * The version of the OpenAPI document: v1
7 * Contact: felix@triptix.tech
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration};
16
17
18/// struct for typed errors of method [`geocode`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GeocodeError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`one_to_many`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum OneToManyError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`reverse_geocode`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum ReverseGeocodeError {
36    UnknownValue(serde_json::Value),
37}
38
39
40pub async fn geocode(configuration: &configuration::Configuration, text: &str, language: Option<&str>) -> Result<Vec<models::Match>, Error<GeocodeError>> {
41    let local_var_configuration = configuration;
42
43    let local_var_client = &local_var_configuration.client;
44
45    let local_var_uri_str = format!("{}/api/v1/geocode", local_var_configuration.base_path);
46    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
47
48    local_var_req_builder = local_var_req_builder.query(&[("text", &text.to_string())]);
49    if let Some(ref local_var_str) = language {
50        local_var_req_builder = local_var_req_builder.query(&[("language", &local_var_str.to_string())]);
51    }
52    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
53        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
54    }
55
56    let local_var_req = local_var_req_builder.build()?;
57    let local_var_resp = local_var_client.execute(local_var_req).await?;
58
59    let local_var_status = local_var_resp.status();
60    let local_var_content = local_var_resp.text().await?;
61
62    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
63        serde_json::from_str(&local_var_content).map_err(Error::from)
64    } else {
65        let local_var_entity: Option<GeocodeError> = serde_json::from_str(&local_var_content).ok();
66        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
67        Err(Error::ResponseError(local_var_error))
68    }
69}
70
71pub async fn one_to_many(configuration: &configuration::Configuration, one: &str, many: Vec<String>, mode: models::Mode, max: f64, max_matching_distance: f64, arrive_by: bool) -> Result<Vec<models::Duration>, Error<OneToManyError>> {
72    let local_var_configuration = configuration;
73
74    let local_var_client = &local_var_configuration.client;
75
76    let local_var_uri_str = format!("{}/api/v1/one-to-many", local_var_configuration.base_path);
77    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
78
79    local_var_req_builder = local_var_req_builder.query(&[("one", &one.to_string())]);
80    local_var_req_builder = match "multi" {
81        "multi" => local_var_req_builder.query(&many.into_iter().map(|p| ("many".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
82        _ => local_var_req_builder.query(&[("many", &many.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
83    };
84    local_var_req_builder = local_var_req_builder.query(&[("mode", &mode.to_string())]);
85    local_var_req_builder = local_var_req_builder.query(&[("max", &max.to_string())]);
86    local_var_req_builder = local_var_req_builder.query(&[("maxMatchingDistance", &max_matching_distance.to_string())]);
87    local_var_req_builder = local_var_req_builder.query(&[("arriveBy", &arrive_by.to_string())]);
88    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
89        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
90    }
91
92    let local_var_req = local_var_req_builder.build()?;
93    let local_var_resp = local_var_client.execute(local_var_req).await?;
94
95    let local_var_status = local_var_resp.status();
96    let local_var_content = local_var_resp.text().await?;
97
98    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
99        serde_json::from_str(&local_var_content).map_err(Error::from)
100    } else {
101        let local_var_entity: Option<OneToManyError> = serde_json::from_str(&local_var_content).ok();
102        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
103        Err(Error::ResponseError(local_var_error))
104    }
105}
106
107pub async fn reverse_geocode(configuration: &configuration::Configuration, place: &str) -> Result<Vec<models::Match>, Error<ReverseGeocodeError>> {
108    let local_var_configuration = configuration;
109
110    let local_var_client = &local_var_configuration.client;
111
112    let local_var_uri_str = format!("{}/api/v1/reverse-geocode", local_var_configuration.base_path);
113    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
114
115    local_var_req_builder = local_var_req_builder.query(&[("place", &place.to_string())]);
116    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
117        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
118    }
119
120    let local_var_req = local_var_req_builder.build()?;
121    let local_var_resp = local_var_client.execute(local_var_req).await?;
122
123    let local_var_status = local_var_resp.status();
124    let local_var_content = local_var_resp.text().await?;
125
126    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
127        serde_json::from_str(&local_var_content).map_err(Error::from)
128    } else {
129        let local_var_entity: Option<ReverseGeocodeError> = serde_json::from_str(&local_var_content).ok();
130        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
131        Err(Error::ResponseError(local_var_error))
132    }
133}
134