brawl_api/http/client.rs
1//! Contains the `Client` class, responsible for API authentication.
2
3use reqwest::blocking::{
4 Client as ReqClient, ClientBuilder as ReqClientBuilder,
5 RequestBuilder
6};
7
8#[cfg(feature = "async")]
9use reqwest::{
10 Client as AReqClient, ClientBuilder as AReqClientBuilder,
11 RequestBuilder as ARequestBuilder
12};
13
14use crate::constants::USER_AGENT as BRAWL_USER_AGENT;
15use crate::http::request::Request;
16use crate::error::Result;
17
18#[derive(Debug, Clone)]
19pub struct Client {
20 pub auth_key: String,
21 pub(crate) inner: ReqClient,
22
23 #[cfg(feature = "async")]
24 pub(crate) a_inner: AReqClient,
25}
26
27/// Represents an HTTP client which holds the user's API auth key, and is required on every fetch
28/// method for authentication. This is usually the starting point for using this library.
29///
30/// See the [`Client::new`] method to start.
31///
32/// [`Client::new`]: #method.new
33impl Client {
34 /// Creates a new Client with a given API auth key.
35 ///
36 /// # Examples
37 ///
38 /// ```rust
39 /// use brawl_api::Client;
40 ///
41 /// let my_client = Client::new("my auth key");
42 /// ```
43 pub fn new(auth_key: &str) -> Client {
44 let inner_b: ReqClientBuilder = ReqClient::builder().user_agent(BRAWL_USER_AGENT);
45
46 #[cfg(feature = "async")]
47 let a_inner_b: AReqClientBuilder = AReqClient::builder().user_agent(BRAWL_USER_AGENT);
48
49 Client {
50 auth_key: String::from(auth_key),
51 inner: inner_b.build().unwrap(),
52
53 #[cfg(feature = "async")]
54 a_inner: a_inner_b.build().unwrap(),
55 }
56 }
57
58 /// (For sync usage) Provides an immutable reference to the [`inner`] field.
59 ///
60 /// [`inner`]: #structfield.inner
61 pub fn inner(&self) -> &ReqClient { &self.inner }
62
63 /// (For sync usage) Provides a mutable reference to the [`inner`] field.
64 ///
65 /// [`inner`]: #structfield.inner
66 pub fn inner_mut(&mut self) -> &mut ReqClient { &mut self.inner }
67
68 /// (For async usage) Provides an immutable reference to the [`a_inner`] field.
69 ///
70 /// [`a_inner`]: #structfield.a_inner
71 #[cfg(feature = "async")]
72 pub fn a_inner(&self) -> &AReqClient { &self.a_inner }
73
74 /// (For async usage) Provides a mutable reference to the [`a_inner`] field.
75 ///
76 /// [`a_inner`]: #structfield.a_inner
77 #[cfg(feature = "async")]
78 pub fn a_inner_mut(&mut self) -> &mut AReqClient { &mut self.a_inner }
79
80 /// Creates a Request instance for one specific endpoint and returns it.
81 pub fn endpoint_request(&self, endpoint: &str) -> Request<'_> {
82 let mut req = Request::<'_>::default();
83 req.endpoint = String::from(endpoint);
84 req
85 }
86
87 /// (For sync usage) Creates a Request instance for one specific endpoint and calls
88 /// [`Request::build`] on the newly-made instance, returning a (blocking) `RequestBuilder`.
89 /// (GET)
90 pub(crate) fn build_endpoint_get(&self, endpoint: &str) -> Result<RequestBuilder> {
91 self.endpoint_request(endpoint).build(&self)
92 }
93
94 /// (For async usage) Creates a Request instance for one specific endpoint and calls
95 /// [`Request::build`] on the newly-made instance, returning a (non-blocking) `RequestBuilder`.
96 /// (GET)
97 #[cfg(feature = "async")]
98 pub(crate) fn a_build_endpoint_get(&self, endpoint: &str) -> Result<ARequestBuilder> {
99 self.endpoint_request(endpoint).a_build(&self)
100 }
101}