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
//! Defines the client used to access Pokeapi.
use http_cache_reqwest::{CACacheManager, Cache, CacheMode, CacheOptions, HttpCache};
use reqwest::{Client, Url};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use serde::de::DeserializeOwned;
use crate::error::Error;
/// Custom client used to call Pokeapi.
pub struct RustemonClient {
client: ClientWithMiddleware,
}
impl RustemonClient {
/// Returns a new instance of RustemonClient.
///
/// # Arguments
///
/// * `cache_mode` - The [CacheMode] to inject into the client.
/// * `options` - The [CacheOptions] to inject into the client.
pub fn new(cache_mode: CacheMode, options: Option<CacheOptions>) -> Self {
Self {
client: ClientBuilder::new(Client::new())
.with(Cache(HttpCache {
mode: cache_mode,
manager: CACacheManager::default(),
options,
}))
.build(),
}
}
/// Returns the deserialized answer resulting from the call made to Pokeapi.
///
/// # Arguments
///
/// `url` - The url to call in order to retrieves the json to deserialize.
pub(crate) async fn get_by_url<T>(&self, url: Url) -> Result<T, Error>
where
T: DeserializeOwned,
{
let json_answer = self.client.get(url).send().await?.json().await?;
Ok(json_answer)
}
}
impl Default for RustemonClient {
/// Returns a RustemonClient with default configuration.
fn default() -> Self {
Self {
client: ClientBuilder::new(Client::new())
.with(Cache(HttpCache {
mode: CacheMode::Default,
manager: CACacheManager::default(),
options: None,
}))
.build(),
}
}
}