space_traders/apis/
default_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_status`].
13#[derive(Debug, Clone)]
14pub enum GetStatusSuccess {
15    /// Response for status code 200.
16    Status200(crate::models::GetStatus200Response),
17}
18
19impl GetStatusSuccess {
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 [`register`].
33#[derive(Debug, Clone)]
34pub enum RegisterSuccess {
35    /// Response for status code 201.
36    Status201(crate::models::Register201Response),
37}
38
39impl RegisterSuccess {
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            201 => Some(match serde_json::from_str(body) {
44                Ok(data) => Ok(Self::Status201(data)),
45                Err(err) => Err(err),
46            }),
47            _ => None,
48        }
49    }
50}
51
52/// Enum for known errors of method [`get_status`].
53#[derive(Debug, Clone)]
54pub enum GetStatusError {}
55
56impl GetStatusError {
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 [`register`].
67#[derive(Debug, Clone)]
68pub enum RegisterError {}
69
70impl RegisterError {
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/// Return the status of the game server. This also includes a few global elements, such as announcements, server reset dates and leaderboards.
81pub async fn get_status(
82    configuration: &configuration::Configuration,
83) -> Result<ResponseContent<GetStatusSuccess>, Error<GetStatusError>> {
84    let client = &configuration.client;
85
86    // Create the request from a path.
87    // Make sure to url encode any user given text.
88    let uri_str = format!("{}/", configuration.base_path);
89    let mut req_builder = client.request(reqwest::Method::GET, &uri_str);
90
91    // === Add headers to request ===
92
93    // Set the user agent string if given.
94    if let Some(user_agent) = &configuration.user_agent {
95        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
96    }
97
98    // === Add auth to request ===
99
100    if let Some(token) = &configuration.bearer_access_token {
101        req_builder = req_builder.bearer_auth(token.to_owned());
102    };
103
104    // === Request is built.
105
106    // Execute the request.
107    let req = req_builder.build()?;
108    let resp = client.execute(req).await?;
109
110    // Get the response.
111    let status = resp.status();
112    let response_body = resp.text().await?;
113
114    if !status.is_client_error() && !status.is_server_error() {
115        // Try to parse the OK response.
116        match GetStatusSuccess::from_body(status, &response_body) {
117            Some(Ok(content)) => Ok(ResponseContent {
118                status,
119                response_body,
120                content,
121            }),
122            Some(Err(err)) => Err(err.into()),
123            None => Err(Error::UnknownResponse {
124                status,
125                is_error: false,
126                response_body,
127            }),
128        }
129    } else {
130        // Try to parse the Err response.
131        match GetStatusError::from_body(status, &response_body) {
132            Some(Ok(content)) => Err(Error::ResponseError(ResponseContent {
133                status,
134                response_body,
135                content,
136            })),
137            Some(Err(err)) => Err(err.into()),
138            None => Err(Error::UnknownResponse {
139                status,
140                is_error: true,
141                response_body,
142            }),
143        }
144    }
145}
146
147/// Creates a new agent and ties it to an account.  The agent symbol must consist of a 3-14 character string, and will be used to represent your agent. This symbol will prefix the symbol of every ship you own. Agent symbols will be cast to all uppercase characters.  This new agent will be tied to a starting faction of your choice, which determines your starting location, and will be granted an authorization token, a contract with their starting faction, a command ship that can fly across space with advanced capabilities, a small probe ship that can be used for reconnaissance, and 150,000 credits.  > #### Keep your token safe and secure > > Save your token during the alpha phase. There is no way to regenerate this token without starting a new agent. In the future you will be able to generate and manage your tokens from the SpaceTraders website.  If you are new to SpaceTraders, It is recommended to register with the COSMIC faction, a faction that is well connected to the rest of the universe. After registering, you should try our interactive [quickstart guide](https://docs.spacetraders.io/quickstart/new-game) which will walk you through basic API requests in just a few minutes.
148pub async fn register(
149    configuration: &configuration::Configuration,
150    register_request: Option<crate::models::RegisterRequest>,
151) -> Result<ResponseContent<RegisterSuccess>, Error<RegisterError>> {
152    let client = &configuration.client;
153
154    // Create the request from a path.
155    // Make sure to url encode any user given text.
156    let uri_str = format!("{}/register", configuration.base_path);
157    let mut req_builder = client.request(reqwest::Method::POST, &uri_str);
158
159    // === Add headers to request ===
160
161    // Set the user agent string if given.
162    if let Some(user_agent) = &configuration.user_agent {
163        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
164    }
165
166    // === Add body to request ===
167
168    req_builder = req_builder.json(&register_request);
169
170    // === Request is built.
171
172    // Execute the request.
173    let req = req_builder.build()?;
174    let resp = client.execute(req).await?;
175
176    // Get the response.
177    let status = resp.status();
178    let response_body = resp.text().await?;
179
180    if !status.is_client_error() && !status.is_server_error() {
181        // Try to parse the OK response.
182        match RegisterSuccess::from_body(status, &response_body) {
183            Some(Ok(content)) => Ok(ResponseContent {
184                status,
185                response_body,
186                content,
187            }),
188            Some(Err(err)) => Err(err.into()),
189            None => Err(Error::UnknownResponse {
190                status,
191                is_error: false,
192                response_body,
193            }),
194        }
195    } else {
196        // Try to parse the Err response.
197        match RegisterError::from_body(status, &response_body) {
198            Some(Ok(content)) => Err(Error::ResponseError(ResponseContent {
199                status,
200                response_body,
201                content,
202            })),
203            Some(Err(err)) => Err(err.into()),
204            None => Err(Error::UnknownResponse {
205                status,
206                is_error: true,
207                response_body,
208            }),
209        }
210    }
211}