asoiaf_api/
client.rs

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
use crate::{
    error::Error,
    item::{
        iterator::{BookIterator, CharacterIterator, HouseIterator},
        Book, Character, House,
    },
    requester::{
        filter::{BookFilter, CharacterFilter, HouseFilter},
        get,
        pagination::Pagination,
        request::{BookRequest, CharacterRequest, HouseRequest},
        ToRequest,
    },
};

/**
 * The main struct of this library. It is used to make requests to the API.
 * It holds methods to get all the items of a type, or to iterate over them.
 */
#[derive(Default)]
pub struct Client {}

impl Client {
    pub fn new() -> Self {
        Self {}
    }

    pub(crate) async fn get_request<T>(&self, request: &impl ToRequest) -> Result<Vec<T>, Error>
    where
        T: serde::de::DeserializeOwned,
    {
        let answer = get(request).await?;

        let answer = serde_json::from_str::<Vec<T>>(&answer)?;
        Ok(answer)
    }

    pub async fn get_books(&self) -> Result<Vec<Book>, Error> {
        let request = BookRequest::default();
        self.get_request(&request).await
    }

    pub fn get_book_iterator(&self, limit: usize) -> BookIterator {
        let request = BookRequest::default().pagination(Pagination::new(1, limit));
        BookIterator::new(request)
    }

    pub fn get_book_filter_iterator(&self, book_filter: BookFilter, limit: usize) -> BookIterator {
        let request = BookRequest::default()
            .pagination(Pagination::new(1, limit))
            .filter(book_filter);
        BookIterator::new(request)
    }

    pub async fn get_characters(&self) -> Result<Vec<Character>, Error> {
        let request = CharacterRequest::default().pagination(Pagination::new(1, 1000));
        self.get_request(&request).await
    }

    pub fn get_character_iterator(&self, limit: usize) -> CharacterIterator {
        let request = CharacterRequest::default().pagination(Pagination::new(1, limit));
        CharacterIterator::new(request)
    }

    pub fn get_character_filter_iterator(
        &self,
        character_filter: CharacterFilter,
        limit: usize,
    ) -> CharacterIterator {
        let request = CharacterRequest::default()
            .pagination(Pagination::new(1, limit))
            .filter(character_filter);
        CharacterIterator::new(request)
    }

    pub async fn get_houses(&self) -> Result<Vec<House>, Error> {
        let request = HouseRequest::default();
        self.get_request(&request).await
    }

    pub fn get_house_iterator(&self, limit: usize) -> HouseIterator {
        let request = HouseRequest::default().pagination(Pagination::new(1, limit));
        HouseIterator::new(request)
    }

    pub fn get_house_filter_iterator(
        &self,
        house_filter: HouseFilter,
        limit: usize,
    ) -> HouseIterator {
        let request = HouseRequest::default()
            .pagination(Pagination::new(1, limit))
            .filter(house_filter);
        HouseIterator::new(request)
    }
}