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
macro_rules! api_client_builder {
($base_client:ty, $api_client:ident) => {
/// A builder for
#[doc = concat!("[`", stringify!($api_client), "`].")]
///
/// You can construct one by calling
#[doc = concat!("[`", stringify!($api_client), "::builder()`].")]
#[derive(Default)]
pub struct ApiClientBuilder {
client: Option<$base_client>,
portal_base_url: Option<Url>,
game_base_url: Option<Url>,
api_key: Option<String>,
}
impl ApiClientBuilder {
fn new() -> Self {
Default::default()
}
/// Sets the underlying [`reqwest`] client to use.
///
/// If this is not configured, it will use the default client.
pub fn client(&mut self, client: $base_client) -> &mut Self {
self.client = Some(client);
self
}
/// Configures the base URL for the mod portal API.
///
/// If not configured, it will default to [`DEFAULT_PORTAL_BASE_URL`].
pub fn portal_base_url<T: Into<Url>>(&mut self, base_url: T) -> &mut Self {
self.portal_base_url = Some(base_url.into());
self
}
/// Configures the base URL for the game API.
///
/// If not configured, it will default to [`DEFAULT_GAME_BASE_URL`].
pub fn game_base_url<T: Into<Url>>(&mut self, base_url: T) -> &mut Self {
self.game_base_url = Some(base_url.into());
self
}
/// Configures an API key to use.
///
/// If not configured, the API will be used in anonymous mode.
pub fn api_key<T: Into<String>>(&mut self, api_key: T) -> &mut Self {
self.api_key = Some(api_key.into());
self
}
/// Builds a finished
#[doc = concat!("[`", stringify!($api_client), "`].")]
pub fn build(self) -> $api_client {
let client = self.client.unwrap_or_default();
let portal_base_url = self
.portal_base_url
.unwrap_or(Url::parse(DEFAULT_PORTAL_BASE_URL).unwrap());
let game_base_url = self
.game_base_url
.unwrap_or(Url::parse(DEFAULT_GAME_BASE_URL).unwrap());
$api_client {
client,
portal_base_url,
game_base_url,
api_key: self.api_key,
}
}
}
};
}