Skip to main content

mobilitydata_client/apis/
licenses_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_license`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetLicenseError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`get_licenses`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum GetLicensesError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`get_matching_licenses`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum GetMatchingLicensesError {
36    UnknownValue(serde_json::Value),
37}
38
39
40/// Get the specified license from the Mobility Database, including the license rules.
41pub async fn get_license(configuration: &configuration::Configuration, id: &str) -> Result<models::LicenseWithRules, Error<GetLicenseError>> {
42    // add a prefix to parameters to efficiently prevent name collisions
43    let p_path_id = id;
44
45    let uri_str = format!("{}/v1/licenses/{id}", configuration.base_path, id=crate::apis::urlencode(p_path_id));
46    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
47
48    if let Some(ref user_agent) = configuration.user_agent {
49        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
50    }
51    if let Some(ref token) = configuration.bearer_access_token {
52        req_builder = req_builder.bearer_auth(token.to_owned());
53    };
54
55    let req = req_builder.build()?;
56    let resp = configuration.client.execute(req).await?;
57
58    let status = resp.status();
59    let content_type = resp
60        .headers()
61        .get("content-type")
62        .and_then(|v| v.to_str().ok())
63        .unwrap_or("application/octet-stream");
64    let content_type = super::ContentType::from(content_type);
65
66    if !status.is_client_error() && !status.is_server_error() {
67        let content = resp.text().await?;
68        match content_type {
69            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
70            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::LicenseWithRules`"))),
71            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::LicenseWithRules`")))),
72        }
73    } else {
74        let content = resp.text().await?;
75        let entity: Option<GetLicenseError> = serde_json::from_str(&content).ok();
76        Err(Error::ResponseError(ResponseContent { status, content, entity }))
77    }
78}
79
80/// Get the list of all licenses in the DB.
81pub async fn get_licenses(configuration: &configuration::Configuration, limit: Option<i32>, offset: Option<i32>) -> Result<Vec<models::LicenseBase>, Error<GetLicensesError>> {
82    // add a prefix to parameters to efficiently prevent name collisions
83    let p_query_limit = limit;
84    let p_query_offset = offset;
85
86    let uri_str = format!("{}/v1/licenses", configuration.base_path);
87    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
88
89    if let Some(ref param_value) = p_query_limit {
90        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
91    }
92    if let Some(ref param_value) = p_query_offset {
93        req_builder = req_builder.query(&[("offset", &param_value.to_string())]);
94    }
95    if let Some(ref user_agent) = configuration.user_agent {
96        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
97    }
98    if let Some(ref token) = configuration.bearer_access_token {
99        req_builder = req_builder.bearer_auth(token.to_owned());
100    };
101
102    let req = req_builder.build()?;
103    let resp = configuration.client.execute(req).await?;
104
105    let status = resp.status();
106    let content_type = resp
107        .headers()
108        .get("content-type")
109        .and_then(|v| v.to_str().ok())
110        .unwrap_or("application/octet-stream");
111    let content_type = super::ContentType::from(content_type);
112
113    if !status.is_client_error() && !status.is_server_error() {
114        let content = resp.text().await?;
115        match content_type {
116            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
117            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec&lt;models::LicenseBase&gt;`"))),
118            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::LicenseBase&gt;`")))),
119        }
120    } else {
121        let content = resp.text().await?;
122        let entity: Option<GetLicensesError> = serde_json::from_str(&content).ok();
123        Err(Error::ResponseError(ResponseContent { status, content, entity }))
124    }
125}
126
127/// Get the list of matching licenses based on the provided license URL
128pub async fn get_matching_licenses(configuration: &configuration::Configuration, get_matching_licenses_request: models::GetMatchingLicensesRequest) -> Result<Vec<models::MatchingLicense>, Error<GetMatchingLicensesError>> {
129    // add a prefix to parameters to efficiently prevent name collisions
130    let p_body_get_matching_licenses_request = get_matching_licenses_request;
131
132    let uri_str = format!("{}/v1/licenses:match", configuration.base_path);
133    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
134
135    if let Some(ref user_agent) = configuration.user_agent {
136        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
137    }
138    if let Some(ref token) = configuration.bearer_access_token {
139        req_builder = req_builder.bearer_auth(token.to_owned());
140    };
141    req_builder = req_builder.json(&p_body_get_matching_licenses_request);
142
143    let req = req_builder.build()?;
144    let resp = configuration.client.execute(req).await?;
145
146    let status = resp.status();
147    let content_type = resp
148        .headers()
149        .get("content-type")
150        .and_then(|v| v.to_str().ok())
151        .unwrap_or("application/octet-stream");
152    let content_type = super::ContentType::from(content_type);
153
154    if !status.is_client_error() && !status.is_server_error() {
155        let content = resp.text().await?;
156        match content_type {
157            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
158            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `Vec&lt;models::MatchingLicense&gt;`"))),
159            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::MatchingLicense&gt;`")))),
160        }
161    } else {
162        let content = resp.text().await?;
163        let entity: Option<GetMatchingLicensesError> = serde_json::from_str(&content).ok();
164        Err(Error::ResponseError(ResponseContent { status, content, entity }))
165    }
166}
167