Skip to main content

auraxis/api/
client.rs

1use crate::api::collections::CensusCollection;
2use crate::api::request::CensusRequestBuilder;
3
4use reqwest::Client;
5
6pub const API_URL: &str = "https://census.daybreakgames.com";
7
8#[derive(Debug, Clone)]
9pub struct ApiClientConfig {
10    pub environment: Option<String>,
11    pub service_id: Option<String>,
12    pub api_url: Option<String>,
13}
14
15impl Default for ApiClientConfig {
16    fn default() -> Self {
17        Self {
18            environment: Some(String::from("ps2:v2")),
19            service_id: None,
20            api_url: Some(String::from(API_URL)),
21        }
22    }
23}
24
25#[derive(Debug, Clone)]
26pub struct ApiClient {
27    environment: String,
28    base_url: String,
29    http_client: Client,
30}
31
32impl ApiClient {
33    pub fn new(config: ApiClientConfig) -> Self {
34        let base_url = match config.api_url {
35            None => String::from(API_URL),
36            Some(url) => url,
37        };
38
39        let base_url = match config.service_id {
40            None => base_url,
41            Some(service_id) => base_url + &*format!("/s:{}", service_id),
42        };
43
44        let environment = match config.environment {
45            None => "ps2:v2".into(),
46            Some(env) => env,
47        };
48
49        Self {
50            base_url,
51            environment,
52            http_client: Client::new(),
53        }
54    }
55
56    pub fn get(&self, collection: impl Into<String> + Clone) -> CensusRequestBuilder {
57        let url = format!("{}/get/{}", &self.base_url, &self.environment);
58
59        let url = format!("{}/{}", url, collection.clone().into());
60
61        CensusRequestBuilder::new(self.http_client.clone(), collection.into(), url)
62    }
63
64    pub async fn count(&self, _collection: CensusCollection) {
65        let _url = format!("{}/count/{}", &self.base_url, &self.environment);
66    }
67}