space_traders/apis/
factions_api.rs

1//!
2//!
3//! Generated by: <https://openapi-generator.tech>
4//!
5//! Version of specification: `2.0.0`
6
7use reqwest::StatusCode;
8
9use super::{configuration, Error};
10use crate::apis::ResponseContent;
11
12/// Enum for successes of method [`get_faction`].
13#[derive(Debug, Clone)]
14pub enum GetFactionSuccess {
15    /// Response for status code 200.
16    Status200(crate::models::GetFaction200Response),
17}
18
19impl GetFactionSuccess {
20    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
21        // Attempt to deserialize the response for the given status code.
22        match status.as_u16() {
23            200 => Some(match serde_json::from_str(body) {
24                Ok(data) => Ok(Self::Status200(data)),
25                Err(err) => Err(err),
26            }),
27            _ => None,
28        }
29    }
30}
31
32/// Enum for successes of method [`get_factions`].
33#[derive(Debug, Clone)]
34pub enum GetFactionsSuccess {
35    /// Response for status code 200.
36    Status200(crate::models::GetFactions200Response),
37}
38
39impl GetFactionsSuccess {
40    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
41        // Attempt to deserialize the response for the given status code.
42        match status.as_u16() {
43            200 => Some(match serde_json::from_str(body) {
44                Ok(data) => Ok(Self::Status200(data)),
45                Err(err) => Err(err),
46            }),
47            _ => None,
48        }
49    }
50}
51
52/// Enum for known errors of method [`get_faction`].
53#[derive(Debug, Clone)]
54pub enum GetFactionError {}
55
56impl GetFactionError {
57    #[allow(unused_variables, clippy::match_single_binding)]
58    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
59        // Attempt to deserialize the response for the given status code.
60        match status.as_u16() {
61            _ => None,
62        }
63    }
64}
65
66/// Enum for known errors of method [`get_factions`].
67#[derive(Debug, Clone)]
68pub enum GetFactionsError {}
69
70impl GetFactionsError {
71    #[allow(unused_variables, clippy::match_single_binding)]
72    fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
73        // Attempt to deserialize the response for the given status code.
74        match status.as_u16() {
75            _ => None,
76        }
77    }
78}
79
80/// View the details of a faction.
81pub async fn get_faction(
82    configuration: &configuration::Configuration,
83    faction_symbol: &str,
84) -> Result<ResponseContent<GetFactionSuccess>, Error<GetFactionError>> {
85    let client = &configuration.client;
86
87    // Create the request from a path.
88    // Make sure to url encode any user given text.
89    let uri_str = format!(
90        "{}/factions/{factionSymbol}",
91        configuration.base_path,
92        factionSymbol = crate::apis::urlencode(faction_symbol)
93    );
94    let mut req_builder = client.request(reqwest::Method::GET, &uri_str);
95
96    // === Add headers to request ===
97
98    // Set the user agent string if given.
99    if let Some(user_agent) = &configuration.user_agent {
100        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
101    }
102
103    // === Add auth to request ===
104
105    if let Some(token) = &configuration.bearer_access_token {
106        req_builder = req_builder.bearer_auth(token.to_owned());
107    };
108
109    // === Request is built.
110
111    // Execute the request.
112    let req = req_builder.build()?;
113    let resp = client.execute(req).await?;
114
115    // Get the response.
116    let status = resp.status();
117    let response_body = resp.text().await?;
118
119    if !status.is_client_error() && !status.is_server_error() {
120        // Try to parse the OK response.
121        match GetFactionSuccess::from_body(status, &response_body) {
122            Some(Ok(content)) => Ok(ResponseContent {
123                status,
124                response_body,
125                content,
126            }),
127            Some(Err(err)) => Err(err.into()),
128            None => Err(Error::UnknownResponse {
129                status,
130                is_error: false,
131                response_body,
132            }),
133        }
134    } else {
135        // Try to parse the Err response.
136        match GetFactionError::from_body(status, &response_body) {
137            Some(Ok(content)) => Err(Error::ResponseError(ResponseContent {
138                status,
139                response_body,
140                content,
141            })),
142            Some(Err(err)) => Err(err.into()),
143            None => Err(Error::UnknownResponse {
144                status,
145                is_error: true,
146                response_body,
147            }),
148        }
149    }
150}
151
152/// Return a paginated list of all the factions in the game.
153pub async fn get_factions(
154    configuration: &configuration::Configuration,
155    page: Option<u32>,
156    limit: Option<u32>,
157) -> Result<ResponseContent<GetFactionsSuccess>, Error<GetFactionsError>> {
158    let client = &configuration.client;
159
160    // Create the request from a path.
161    // Make sure to url encode any user given text.
162    let uri_str = format!("{}/factions", configuration.base_path);
163    let mut req_builder = client.request(reqwest::Method::GET, &uri_str);
164
165    // === Add queries to request ===
166
167    if let Some(s) = &page {
168        req_builder = req_builder.query(&[("page", &s.to_string())]);
169    }
170    // === Add queries to request ===
171
172    if let Some(s) = &limit {
173        req_builder = req_builder.query(&[("limit", &s.to_string())]);
174    }
175
176    // === Add headers to request ===
177
178    // Set the user agent string if given.
179    if let Some(user_agent) = &configuration.user_agent {
180        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
181    }
182
183    // === Add auth to request ===
184
185    if let Some(token) = &configuration.bearer_access_token {
186        req_builder = req_builder.bearer_auth(token.to_owned());
187    };
188
189    // === Request is built.
190
191    // Execute the request.
192    let req = req_builder.build()?;
193    let resp = client.execute(req).await?;
194
195    // Get the response.
196    let status = resp.status();
197    let response_body = resp.text().await?;
198
199    if !status.is_client_error() && !status.is_server_error() {
200        // Try to parse the OK response.
201        match GetFactionsSuccess::from_body(status, &response_body) {
202            Some(Ok(content)) => Ok(ResponseContent {
203                status,
204                response_body,
205                content,
206            }),
207            Some(Err(err)) => Err(err.into()),
208            None => Err(Error::UnknownResponse {
209                status,
210                is_error: false,
211                response_body,
212            }),
213        }
214    } else {
215        // Try to parse the Err response.
216        match GetFactionsError::from_body(status, &response_body) {
217            Some(Ok(content)) => Err(Error::ResponseError(ResponseContent {
218                status,
219                response_body,
220                content,
221            })),
222            Some(Err(err)) => Err(err.into()),
223            None => Err(Error::UnknownResponse {
224                status,
225                is_error: true,
226                response_body,
227            }),
228        }
229    }
230}