space_traders_api/apis/
global_api.rs

1/*
2 * SpaceTraders API
3 *
4 * SpaceTraders is an open-universe game and learning platform that offers a set of HTTP endpoints to control a fleet of ships and explore a multiplayer universe.  The API is documented using [OpenAPI](https://github.com/SpaceTradersAPI/api-docs). You can send your first request right here in your browser to check the status of the game server.  ```json http {   \"method\": \"GET\",   \"url\": \"https://api.spacetraders.io/v2\", } ```  Unlike a traditional game, SpaceTraders does not have a first-party client or app to play the game. Instead, you can use the API to build your own client, write a script to automate your ships, or try an app built by the community.  We have a [Discord channel](https://discord.com/invite/jh6zurdWk5) where you can share your projects, ask questions, and get help from other players.   
5 *
6 * The version of the OpenAPI document: 2.3.0
7 * Contact: joel@spacetraders.io
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_status`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetStatusError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`register`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum RegisterError {
29    UnknownValue(serde_json::Value),
30}
31
32
33/// Return the status of the game server. This also includes a few global elements, such as announcements, server reset dates and leaderboards.
34pub async fn get_status(configuration: &configuration::Configuration, ) -> Result<models::GetStatus200Response, Error<GetStatusError>> {
35
36    let uri_str = format!("{}/", configuration.base_path);
37    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
38
39    if let Some(ref user_agent) = configuration.user_agent {
40        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
41    }
42    if let Some(ref token) = configuration.bearer_access_token {
43        req_builder = req_builder.bearer_auth(token.to_owned());
44    };
45
46    let req = req_builder.build()?;
47    let resp = configuration.client.execute(req).await?;
48
49    let status = resp.status();
50    let content_type = resp
51        .headers()
52        .get("content-type")
53        .and_then(|v| v.to_str().ok())
54        .unwrap_or("application/octet-stream");
55    let content_type = super::ContentType::from(content_type);
56
57    if !status.is_client_error() && !status.is_server_error() {
58        let content = resp.text().await?;
59        match content_type {
60            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
61            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetStatus200Response`"))),
62            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::GetStatus200Response`")))),
63        }
64    } else {
65        let content = resp.text().await?;
66        let entity: Option<GetStatusError> = serde_json::from_str(&content).ok();
67        Err(Error::ResponseError(ResponseContent { status, content, entity }))
68    }
69}
70
71/// 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 175,000 credits.  > #### Keep your token safe and secure > > Keep careful track of where you store your token. You can generate a new token from our account dashboard, but if someone else gains access to your token they will be able to use it to make API requests on your behalf until the end of the reset.  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 a few basic API requests in just a few minutes.
72pub async fn register(configuration: &configuration::Configuration, register_request: Option<models::RegisterRequest>) -> Result<models::Register201Response, Error<RegisterError>> {
73    // add a prefix to parameters to efficiently prevent name collisions
74    let p_register_request = register_request;
75
76    let uri_str = format!("{}/register", configuration.base_path);
77    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
78
79    if let Some(ref user_agent) = configuration.user_agent {
80        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
81    }
82    if let Some(ref token) = configuration.bearer_access_token {
83        req_builder = req_builder.bearer_auth(token.to_owned());
84    };
85    req_builder = req_builder.json(&p_register_request);
86
87    let req = req_builder.build()?;
88    let resp = configuration.client.execute(req).await?;
89
90    let status = resp.status();
91    let content_type = resp
92        .headers()
93        .get("content-type")
94        .and_then(|v| v.to_str().ok())
95        .unwrap_or("application/octet-stream");
96    let content_type = super::ContentType::from(content_type);
97
98    if !status.is_client_error() && !status.is_server_error() {
99        let content = resp.text().await?;
100        match content_type {
101            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
102            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::Register201Response`"))),
103            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::Register201Response`")))),
104        }
105    } else {
106        let content = resp.text().await?;
107        let entity: Option<RegisterError> = serde_json::from_str(&content).ok();
108        Err(Error::ResponseError(ResponseContent { status, content, entity }))
109    }
110}
111