space_traders/apis/
factions_api.rs1use reqwest::StatusCode;
8
9use super::{configuration, Error};
10use crate::apis::ResponseContent;
11
12#[derive(Debug, Clone)]
14pub enum GetFactionSuccess {
15 Status200(crate::models::GetFaction200Response),
17}
18
19impl GetFactionSuccess {
20 fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
21 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#[derive(Debug, Clone)]
34pub enum GetFactionsSuccess {
35 Status200(crate::models::GetFactions200Response),
37}
38
39impl GetFactionsSuccess {
40 fn from_body(status: StatusCode, body: &str) -> Option<serde_json::Result<Self>> {
41 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#[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 match status.as_u16() {
61 _ => None,
62 }
63 }
64}
65
66#[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 match status.as_u16() {
75 _ => None,
76 }
77 }
78}
79
80pub 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 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 if let Some(user_agent) = &configuration.user_agent {
100 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
101 }
102
103 if let Some(token) = &configuration.bearer_access_token {
106 req_builder = req_builder.bearer_auth(token.to_owned());
107 };
108
109 let req = req_builder.build()?;
113 let resp = client.execute(req).await?;
114
115 let status = resp.status();
117 let response_body = resp.text().await?;
118
119 if !status.is_client_error() && !status.is_server_error() {
120 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 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
152pub 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 let uri_str = format!("{}/factions", configuration.base_path);
163 let mut req_builder = client.request(reqwest::Method::GET, &uri_str);
164
165 if let Some(s) = &page {
168 req_builder = req_builder.query(&[("page", &s.to_string())]);
169 }
170 if let Some(s) = &limit {
173 req_builder = req_builder.query(&[("limit", &s.to_string())]);
174 }
175
176 if let Some(user_agent) = &configuration.user_agent {
180 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
181 }
182
183 if let Some(token) = &configuration.bearer_access_token {
186 req_builder = req_builder.bearer_auth(token.to_owned());
187 };
188
189 let req = req_builder.build()?;
193 let resp = client.execute(req).await?;
194
195 let status = resp.status();
197 let response_body = resp.text().await?;
198
199 if !status.is_client_error() && !status.is_server_error() {
200 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 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}