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
102
103
104
105
106
107
macro_rules! endpoint {
($type:ty; for $name:literal) => {
use reqwest::Url;
use crate::model::resource::NamedApiResourceList;
use crate::client::RustemonClient;
use crate::error::Error;
const ENDPOINT: &str = concat!("https:///pokeapi.co/api/v2/", $name, "/");
/// Returns the default page regarding the resource.
///
/// # Arguments
///
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_page(rustemon_client: &RustemonClient) -> Result<NamedApiResourceList<$type>, Error> {
let url = Url::parse(ENDPOINT).unwrap();
rustemon_client.get_by_url::<NamedApiResourceList<$type>>(url).await
}
/// Returns the page targeted by the parameters.
///
/// # Arguments
///
/// `offset` - The offset to start retrieving the data from.
/// `limit` - Maximum number of elements returned by the call.
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_page_with_param(
offset: i64,
limit: i64,
rustemon_client: &RustemonClient
) -> Result<NamedApiResourceList<$type>, Error> {
let url = Url::parse_with_params(
ENDPOINT,
&[("limit", limit.to_string()), ("offset", offset.to_string())],
)
.unwrap();
rustemon_client.get_by_url::<NamedApiResourceList<$type>>(url).await
}
/// Returns the resource, using its id.
///
/// # Arguments
///
/// `id` - The unique ID of the resource to get.
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_by_id(id: i64, rustemon_client: &RustemonClient) -> Result<$type, Error> {
let url = Url::parse(ENDPOINT).unwrap().join(&id.to_string()).unwrap();
rustemon_client.get_by_url::<$type>(url).await
}
/// Returns the resource, using its name.
///
/// # Arguments
///
/// `name` - The name of the resource to get.
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_by_name(name: &str, rustemon_client: &RustemonClient) -> Result<$type, Error> {
let url = Url::parse(ENDPOINT).unwrap().join(name).unwrap();
rustemon_client.get_by_url::<$type>(url).await
}
};
($type:ty; for $name:literal; with $(($sub:ident, $sub_type:ty))+) => {
crate::endpoint!($type; for $name);
$(
pub mod $sub {
use reqwest::Url;
use crate::client::RustemonClient;
use crate::error::Error;
use super::ENDPOINT;
const SUB_ENDPOINT: &str = stringify!($sub);
/// Returns the resource, using its id.
///
/// # Arguments
///
/// `id` - The unique ID of the resource to get.
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_by_id(id: i64, rustemon_client: &RustemonClient) -> Result<$sub_type, Error> {
let sub_path = format!("{}/{}", id, SUB_ENDPOINT);
let url = Url::parse(ENDPOINT).unwrap().join(&sub_path).unwrap();
rustemon_client.get_by_url::<$sub_type>(url).await
}
/// Returns the resource, using its name.
///
/// # Arguments
///
/// `name` - The name of the resource to get.
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_by_name(name: &'static str, rustemon_client: &RustemonClient) -> Result<$sub_type, Error> {
let sub_path = format!("{}/{}", name, SUB_ENDPOINT);
let url = Url::parse(ENDPOINT).unwrap().join(&sub_path).unwrap();
rustemon_client.get_by_url::<$sub_type>(url).await
}
}
)+
};
}
pub(crate) use endpoint;