asoiaf_api/requester/
mod.rs

1use self::pagination::Pagination;
2
3pub mod filter;
4pub mod pagination;
5pub mod request;
6
7/// Trait that is used to convert a struct to a url.
8/// It is used to make requests to the API.
9pub(crate) trait ToUrl {
10    fn to_url(&self) -> String;
11}
12
13pub trait ToRequest {
14    fn to_request(&self) -> String;
15    fn update_pagination(&mut self, pagination: Pagination);
16    fn next_page(&mut self);
17}
18
19/// Wrapper for the [`reqwest::Client`] struct that contains the token
20/// and the actual url that is used to make the request.
21/// It is used to make requests to the API.
22///
23/// There is a cache system in branch [`cache`](https:://github.com/Yag000/asoiaf-rs/tree/cache),
24/// but as [since 2017](https://github.com/joakimskoog/AnApiOfIceAndFire/issues/130https://github.com/joakimskoog/AnApiOfIceAndFire/issues/130).
25/// this feature is not supported by the API, it is not merged into master.
26pub(crate) async fn get(request: &impl ToRequest) -> Result<String, reqwest::Error> {
27    let request_string = request.to_request();
28
29    let client = reqwest::Client::new();
30    let response = client
31        .get(format!(
32            "https://www.anapioficeandfire.com/api/{}",
33            request_string
34        ))
35        .send()
36        .await?;
37
38    let response = response.error_for_status()?;
39
40    response.text().await
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use crate::requester::request::BookRequest;
47
48    #[tokio::test]
49    async fn test_get() {
50        let request = BookRequest::default();
51        let response = get(&request).await;
52        assert!(response.is_ok());
53        assert!(!response.unwrap().is_empty());
54    }
55}