1pub mod api;
2pub mod error_ddapi;
3pub mod scheme;
4mod tests;
5
6use error_ddapi::ApiError;
7use reqwest::{Client, Error};
8use serde::de::DeserializeOwned;
9use std::borrow::Cow;
10use urlencoding::encode;
11
12pub enum MasterServer {
13 One,
14 Two,
15 Three,
16 Four,
17}
18
19impl MasterServer {
20 #[allow(dead_code)]
21 fn get_index(&self) -> i32 {
22 match &self {
23 Self::One => 1,
24 Self::Two => 2,
25 Self::Three => 3,
26 Self::Four => 4,
27 }
28 }
29}
30
31pub struct DDApi {
32 client: Client,
33}
34
35impl DDApi {
36 pub fn new() -> Self {
37 let client = Client::new();
38 DDApi { client }
39 }
40
41 pub fn new_client(client: Client) -> Self {
42 DDApi { client }
43 }
44
45 async fn send_request(&self, uri: &str) -> Result<String, Error> {
46 self.client.get(uri).send().await?.text().await
47 }
48
49 async fn _generator<T>(&self, uri: &str) -> Result<T, ApiError>
50 where
51 T: DeserializeOwned,
52 {
53 let response = self.send_request(uri).await?;
54 Ok(serde_json::from_str(&response)?)
55 }
56
57 pub async fn encode_nickname<'a>(&self, nickname: &'a str) -> Cow<'a, str> {
58 encode(nickname)
59 }
60}