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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
macro_rules! endpoint {
(unnamed $type:ty; for $name:literal) => {
use crate::model::resource::{ApiResourceList, ApiResource};
type ResourceList<T> = ApiResourceList<T>;
type Resource<T> = ApiResource<T>;
crate::endpoint!(@inner $type; for $name);
};
($type:ty; for $name:literal) => {
use crate::model::resource::{NamedApiResourceList, NamedApiResource};
type ResourceList<T> = NamedApiResourceList<T>;
type Resource<T> = NamedApiResource<T>;
crate::endpoint!(@inner $type; for $name);
};
(@inner $type:ty; for $name:literal) => {
use crate::client::{RustemonClient, Id};
use crate::error::Error;
/// 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<ResourceList<$type>, Error> {
rustemon_client.get_by_endpoint::<ResourceList<$type>>($name).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<ResourceList<$type>, Error> {
rustemon_client.get_with_limit_and_offset::<ResourceList<$type>>($name, limit, offset).await
}
/// Returns all entries from the given resource.
///
/// # Arguments
///
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_all_entries(rustemon_client: &RustemonClient) -> Result<Vec<Resource<$type>>, Error> {
let mut first_page = get_page(rustemon_client).await?;
let first_page_entries_count = first_page.results.len() as i64;
let total_entries_count = first_page.count;
let other_entries_count = total_entries_count - first_page_entries_count;
let mut all_entries = Vec::with_capacity(total_entries_count as usize);
all_entries.append(&mut first_page.results);
let mut other_entries = get_page_with_param(first_page_entries_count, other_entries_count, rustemon_client).await?;
all_entries.append(&mut other_entries.results);
Ok(all_entries)
}
/// 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> {
rustemon_client.get_by_endpoint_and_id::<$type>($name, Id::Int(id)).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> {
rustemon_client.get_by_endpoint_and_id::<$type>($name, Id::Str(name)).await
}
};
($type:ty; for $name:literal; with $(($sub:ident, $sub_type:ty))+) => {
crate::endpoint!($type; for $name);
$(
/// Give access to the sub endpoint.
pub mod $sub {
use super::RustemonClient;
use super::Error;
const SUB_STR: &'static 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!("{}/{}/{}", $name, id, SUB_STR);
rustemon_client.get_by_endpoint::<$sub_type>(&sub_path).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, name, SUB_STR);
rustemon_client.get_by_endpoint::<$sub_type>(&sub_path).await
}
}
)+
};
}
pub(crate) use endpoint;