up_api/v1/macros.rs
1macro_rules! implement_pagination_v1 {
2 ($t:ty) => {
3 impl $t {
4 async fn follow_link(
5 client: &Client,
6 url: &str,
7 ) -> Result<Self, error::Error> {
8 let res = reqwest::Client::new()
9 .get(url)
10 .header("Authorization", client.auth_header())
11 .send()
12 .await
13 .map_err(error::Error::Request)?;
14
15 match res.status() {
16 reqwest::StatusCode::OK => {
17 let body =
18 res.text()
19 .await
20 .map_err(error::Error::BodyRead)?;
21 let response: Self =
22 serde_json::from_str(&body)
23 .map_err(error::Error::Json)?;
24
25 Ok(response)
26 },
27 _ => {
28 let body =
29 res.text()
30 .await
31 .map_err(error::Error::BodyRead)?;
32 let error: error::ErrorResponse =
33 serde_json::from_str(&body)
34 .map_err(error::Error::Json)?;
35
36 Err(error::Error::Api(error))
37 }
38 }
39 }
40
41 /// Follows the link to the next page, returns None of the next
42 /// page does not exist.
43 pub async fn next(
44 &self,
45 client: &Client,
46 ) -> Option<Result<Self, error::Error>> {
47 match
48 self
49 .links
50 .next
51 .as_ref()
52 .map(|url| Self::follow_link(client, &url)) {
53
54 Some(data) => Some(data.await),
55 None => None,
56 }
57 }
58
59 /// Follows the link to the previous page, returns None of the
60 /// previous page does not exist.
61 pub async fn prev(
62 &self, client: &Client,
63 ) -> Option<Result<Self, error::Error>> {
64 match
65 self
66 .links
67 .prev
68 .as_ref()
69 .map(|url| Self::follow_link(client, &url)) {
70
71 Some(data) => Some(data.await),
72 None => None,
73 }
74 }
75 }
76 }
77}
78
79