1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use reqwest::Url;
use reqwest::blocking::get;

use crate::endpoints;
use crate::error::Error;

fn fix_bom(data: &[u8]) -> &[u8] {
    if data.starts_with(&[0xEF, 0xBB, 0xBF]) {
        &data[3..]
    } else {
        data
    }
}

/// BattleBit API Struct
pub struct BBApi(Url);

impl BBApi {

    /// Shorthand for `BBApi::Default()`  
    /// Creates a new BBApi with the default URL
    pub fn new() -> BBApi {
        BBApi::default()
    }

    /// Creates a new BBApi with the given URL
    pub fn with_url(url: Url) -> BBApi {
        BBApi(url)
    }

    /// Fetches the server list and puts it into a `Vec<ServerData>`
    pub fn server_list(&self) -> Result<Vec<endpoints::ServerData>, Error> {
        let url = self.0.join("Servers/GetServerList")?;
        let data = get(url)?.bytes()?;
        let data = serde_json::from_slice(fix_bom(&data))?;

        Ok(data)
    }
}

impl Default for BBApi {
    /// Creates a new BBApi with the default URL  
    /// Default URL is `https://publicapi.battlebit.cloud/`
    fn default() -> Self {
        Self(Url::parse("https://publicapi.battlebit.cloud/").unwrap())
    }
}