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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::{Auth, ClientBuilder};
use crate::{endpoints::Endpoint, Error, ParsedResponse};
use reqwest::Url;
use serde::Deserialize;

pub struct Client {
    pub(super) client: reqwest::blocking::Client,
    pub(super) auth: Option<Auth>,
    pub(super) url_base: Url,
}

impl Client {
    #[must_use]
    #[inline]
    pub fn builder() -> ClientBuilder {
        ClientBuilder::new()
    }

    #[inline]
    pub fn get<'de, E: Endpoint<'de>>(
        &self,
        params: E::Parameters,
    ) -> Result<ParsedResponse<E::ReturnType>, Error> {
        self.get_custom_return_type::<E, E::ReturnType>(params)
    }

    /// This method is here to allow users of this library
    /// to change the type the response is parsed as.
    ///
    /// Use `Client::get` where possible to use the regular types.<br/>
    /// Use [`serde_json::Value`] as `R` to parse any valid response.
    #[inline]
    pub fn get_custom_return_type<'a, 'b, E: Endpoint<'a>, R: Deserialize<'b>>(
        &self,
        params: E::Parameters,
    ) -> Result<ParsedResponse<R>, Error> {
        E::build_url(&self.url_base, params).and_then(|url| self.get_url(url))
    }

    /// This method only modifies the given url in that it will add the user token if applicable
    pub fn get_url<'de, R>(&self, mut url: Url) -> Result<ParsedResponse<R>, Error>
    where
        R: Deserialize<'de>,
    {
        if let Some(Auth::Token(token)) = &self.auth {
            url.query_pairs_mut().append_pair("token", token);
        }

        self.client
            .get(url)
            .send()
            .map_err(Error::NetError)
            .and_then(ParsedResponse::new)
    }
}

impl Default for Client {
    /// Can panic. If failable, build manually.
    #[must_use]
    fn default() -> Self {
        Self::builder().build().unwrap()
    }
}