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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
//! Contains the `Client` class, responsible for API authentication.

use reqwest::blocking::{
    Client as ReqClient, ClientBuilder as ReqClientBuilder,
    RequestBuilder
};

#[cfg(feature = "async")]
use reqwest::{
    Client as AReqClient, ClientBuilder as AReqClientBuilder,
    RequestBuilder as ARequestBuilder
};

use crate::constants::USER_AGENT as BRAWL_USER_AGENT;
use crate::http::request::Request;
use crate::error::Result;

#[derive(Debug, Clone)]
pub struct Client {
    pub auth_key: String,
    pub(crate) inner: ReqClient,

    #[cfg(feature = "async")]
    pub(crate) a_inner: AReqClient,
}

/// Represents an HTTP client which holds the user's API auth key, and is required on every fetch
/// method for authentication. This is usually the starting point for using this library.
///
/// See the [`Client::new`] method to start.
///
/// [`Client::new`]: #method.new
impl Client {
    /// Creates a new Client with a given API auth key.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use brawl_api::Client;
    ///
    /// let my_client = Client::new("my auth key");
    /// ```
    pub fn new(auth_key: &str) -> Client {
        let inner_b: ReqClientBuilder = ReqClient::builder().user_agent(BRAWL_USER_AGENT);

        #[cfg(feature = "async")]
        let a_inner_b: AReqClientBuilder = AReqClient::builder().user_agent(BRAWL_USER_AGENT);

        Client {
            auth_key: String::from(auth_key),
            inner: inner_b.build().unwrap(),

            #[cfg(feature = "async")]
            a_inner: a_inner_b.build().unwrap(),
        }
    }

    /// (For sync usage) Provides an immutable reference to the [`inner`] field.
    ///
    /// [`inner`]: #structfield.inner
    pub fn inner(&self) -> &ReqClient { &self.inner }

    /// (For sync usage) Provides a mutable reference to the [`inner`] field.
    ///
    /// [`inner`]: #structfield.inner
    pub fn inner_mut(&mut self) -> &mut ReqClient { &mut self.inner }

    /// (For async usage) Provides an immutable reference to the [`a_inner`] field.
    ///
    /// [`a_inner`]: #structfield.a_inner
    #[cfg(feature = "async")]
    pub fn a_inner(&self) -> &AReqClient { &self.a_inner }

    /// (For async usage) Provides a mutable reference to the [`a_inner`] field.
    ///
    /// [`a_inner`]: #structfield.a_inner
    #[cfg(feature = "async")]
    pub fn a_inner_mut(&mut self) -> &mut AReqClient { &mut self.a_inner }

    /// Creates a Request instance for one specific endpoint and returns it.
    pub fn endpoint_request(&self, endpoint: &str) -> Request<'_> {
        let mut req = Request::<'_>::default();
        req.endpoint = String::from(endpoint);
        req
    }

    /// (For sync usage) Creates a Request instance for one specific endpoint and calls
    /// [`Request::build`] on the newly-made instance, returning a (blocking) `RequestBuilder`.
    /// (GET)
    pub(crate) fn build_endpoint_get(&self, endpoint: &str) -> Result<RequestBuilder> {
        self.endpoint_request(endpoint).build(&self)
    }

    /// (For async usage) Creates a Request instance for one specific endpoint and calls
    /// [`Request::build`] on the newly-made instance, returning a (non-blocking) `RequestBuilder`.
    /// (GET)
    #[cfg(feature = "async")]
    pub(crate) fn a_build_endpoint_get(&self, endpoint: &str) -> Result<ARequestBuilder> {
        self.endpoint_request(endpoint).a_build(&self)
    }
}