Skip to main content

mobilitydata_client/apis/
beta_api.rs

1/*
2 * Mobility Database Catalog
3 *
4 * API for the Mobility Database Catalog. See [https://mobilitydatabase.org/](https://mobilitydatabase.org/).  The Mobility Database API uses OAuth2 authentication. To initiate a successful API request, an access token must be included as a bearer token in the HTTP header. Access tokens are valid for one hour. To obtain an access token, you'll first need a refresh token, which is long-lived and does not expire. 
5 *
6 * The version of the OpenAPI document: 1.0.0
7 * Contact: api@mobilitydata.org
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`get_gbfs_feeds`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetGbfsFeedsError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`get_gtfs_feed`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum GetGtfsFeedError {
29    UnknownValue(serde_json::Value),
30}
31
32
33/// Get GBFS feeds from the Mobility Database.
34pub async fn get_gbfs_feeds(configuration: &configuration::Configuration, limit: Option<i32>, offset: Option<i32>, provider: Option<&str>, producer_url: Option<&str>, country_code: Option<&str>, subdivision_name: Option<&str>, municipality: Option<&str>, system_id: Option<&str>, version: Option<&str>) -> Result<Vec<models::GbfsFeed>, Error<GetGbfsFeedsError>> {
35    // add a prefix to parameters to efficiently prevent name collisions
36    let p_query_limit = limit;
37    let p_query_offset = offset;
38    let p_query_provider = provider;
39    let p_query_producer_url = producer_url;
40    let p_query_country_code = country_code;
41    let p_query_subdivision_name = subdivision_name;
42    let p_query_municipality = municipality;
43    let p_query_system_id = system_id;
44    let p_query_version = version;
45
46    let uri_str = format!("{}/v1/gbfs_feeds", configuration.base_path);
47    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
48
49    if let Some(ref param_value) = p_query_limit {
50        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
51    }
52    if let Some(ref param_value) = p_query_offset {
53        req_builder = req_builder.query(&[("offset", &param_value.to_string())]);
54    }
55    if let Some(ref param_value) = p_query_provider {
56        req_builder = req_builder.query(&[("provider", &param_value.to_string())]);
57    }
58    if let Some(ref param_value) = p_query_producer_url {
59        req_builder = req_builder.query(&[("producer_url", &param_value.to_string())]);
60    }
61    if let Some(ref param_value) = p_query_country_code {
62        req_builder = req_builder.query(&[("country_code", &param_value.to_string())]);
63    }
64    if let Some(ref param_value) = p_query_subdivision_name {
65        req_builder = req_builder.query(&[("subdivision_name", &param_value.to_string())]);
66    }
67    if let Some(ref param_value) = p_query_municipality {
68        req_builder = req_builder.query(&[("municipality", &param_value.to_string())]);
69    }
70    if let Some(ref param_value) = p_query_system_id {
71        req_builder = req_builder.query(&[("system_id", &param_value.to_string())]);
72    }
73    if let Some(ref param_value) = p_query_version {
74        req_builder = req_builder.query(&[("version", &param_value.to_string())]);
75    }
76    if let Some(ref user_agent) = configuration.user_agent {
77        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
78    }
79    if let Some(ref token) = configuration.bearer_access_token {
80        req_builder = req_builder.bearer_auth(token.to_owned());
81    };
82
83    let req = req_builder.build()?;
84    let resp = configuration.client.execute(req).await?;
85
86    let status = resp.status();
87    let content_type = resp
88        .headers()
89        .get("content-type")
90        .and_then(|v| v.to_str().ok())
91        .unwrap_or("application/octet-stream");
92    let content_type = super::ContentType::from(content_type);
93
94    if !status.is_client_error() && !status.is_server_error() {
95        let content = resp.text().await?;
96        match content_type {
97            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
98            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec&lt;models::GbfsFeed&gt;`"))),
99            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec&lt;models::GbfsFeed&gt;`")))),
100        }
101    } else {
102        let content = resp.text().await?;
103        let entity: Option<GetGbfsFeedsError> = serde_json::from_str(&content).ok();
104        Err(Error::ResponseError(ResponseContent { status, content, entity }))
105    }
106}
107
108/// Get the specified GTFS feed from the Mobility Database. Once a week, we check if the latest dataset has been updated and, if so, we update it in our system accordingly.
109pub async fn get_gtfs_feed(configuration: &configuration::Configuration, id: &str) -> Result<models::GtfsFeed, Error<GetGtfsFeedError>> {
110    // add a prefix to parameters to efficiently prevent name collisions
111    let p_path_id = id;
112
113    let uri_str = format!("{}/v1/gtfs_feeds/{id}", configuration.base_path, id=crate::apis::urlencode(p_path_id));
114    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
115
116    if let Some(ref user_agent) = configuration.user_agent {
117        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
118    }
119    if let Some(ref token) = configuration.bearer_access_token {
120        req_builder = req_builder.bearer_auth(token.to_owned());
121    };
122
123    let req = req_builder.build()?;
124    let resp = configuration.client.execute(req).await?;
125
126    let status = resp.status();
127    let content_type = resp
128        .headers()
129        .get("content-type")
130        .and_then(|v| v.to_str().ok())
131        .unwrap_or("application/octet-stream");
132    let content_type = super::ContentType::from(content_type);
133
134    if !status.is_client_error() && !status.is_server_error() {
135        let content = resp.text().await?;
136        match content_type {
137            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
138            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GtfsFeed`"))),
139            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::GtfsFeed`")))),
140        }
141    } else {
142        let content = resp.text().await?;
143        let entity: Option<GetGtfsFeedError> = serde_json::from_str(&content).ok();
144        Err(Error::ResponseError(ResponseContent { status, content, entity }))
145    }
146}
147