space_traders_api/apis/
factions_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_faction`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetFactionError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`get_factions`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum GetFactionsError {
29    UnknownValue(serde_json::Value),
30}
31
32
33/// View the details of a faction.
34pub async fn get_faction(configuration: &configuration::Configuration, faction_symbol: &str) -> Result<models::GetFaction200Response, Error<GetFactionError>> {
35    // add a prefix to parameters to efficiently prevent name collisions
36    let p_faction_symbol = faction_symbol;
37
38    let uri_str = format!("{}/factions/{factionSymbol}", configuration.base_path, factionSymbol=crate::apis::urlencode(p_faction_symbol));
39    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
40
41    if let Some(ref user_agent) = configuration.user_agent {
42        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
43    }
44    if let Some(ref token) = configuration.bearer_access_token {
45        req_builder = req_builder.bearer_auth(token.to_owned());
46    };
47    if let Some(ref token) = configuration.bearer_access_token {
48        req_builder = req_builder.bearer_auth(token.to_owned());
49    };
50
51    let req = req_builder.build()?;
52    let resp = configuration.client.execute(req).await?;
53
54    let status = resp.status();
55    let content_type = resp
56        .headers()
57        .get("content-type")
58        .and_then(|v| v.to_str().ok())
59        .unwrap_or("application/octet-stream");
60    let content_type = super::ContentType::from(content_type);
61
62    if !status.is_client_error() && !status.is_server_error() {
63        let content = resp.text().await?;
64        match content_type {
65            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
66            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetFaction200Response`"))),
67            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::GetFaction200Response`")))),
68        }
69    } else {
70        let content = resp.text().await?;
71        let entity: Option<GetFactionError> = serde_json::from_str(&content).ok();
72        Err(Error::ResponseError(ResponseContent { status, content, entity }))
73    }
74}
75
76/// Return a paginated list of all the factions in the game.
77pub async fn get_factions(configuration: &configuration::Configuration, page: Option<i32>, limit: Option<i32>) -> Result<models::GetFactions200Response, Error<GetFactionsError>> {
78    // add a prefix to parameters to efficiently prevent name collisions
79    let p_page = page;
80    let p_limit = limit;
81
82    let uri_str = format!("{}/factions", configuration.base_path);
83    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
84
85    if let Some(ref param_value) = p_page {
86        req_builder = req_builder.query(&[("page", &param_value.to_string())]);
87    }
88    if let Some(ref param_value) = p_limit {
89        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
90    }
91    if let Some(ref user_agent) = configuration.user_agent {
92        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
93    }
94    if let Some(ref token) = configuration.bearer_access_token {
95        req_builder = req_builder.bearer_auth(token.to_owned());
96    };
97
98    let req = req_builder.build()?;
99    let resp = configuration.client.execute(req).await?;
100
101    let status = resp.status();
102    let content_type = resp
103        .headers()
104        .get("content-type")
105        .and_then(|v| v.to_str().ok())
106        .unwrap_or("application/octet-stream");
107    let content_type = super::ContentType::from(content_type);
108
109    if !status.is_client_error() && !status.is_server_error() {
110        let content = resp.text().await?;
111        match content_type {
112            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
113            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetFactions200Response`"))),
114            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::GetFactions200Response`")))),
115        }
116    } else {
117        let content = resp.text().await?;
118        let entity: Option<GetFactionsError> = serde_json::from_str(&content).ok();
119        Err(Error::ResponseError(ResponseContent { status, content, entity }))
120    }
121}
122