mtgapi_client/api/set/
set_api.rs

1use crate::api::error::MtgApiErrorKind;
2use crate::api::set::filter::SetFilter;
3use failure::Error;
4use failure::ResultExt;
5use reqwest::Client;
6
7use crate::model::card::CardDetail;
8use crate::model::set::SetDetail;
9use std::rc::Weak;
10
11use crate::api::response::ApiResponse;
12use crate::api::util;
13
14///Responsible for the calls to the /sets endpoint
15pub struct SetApi {
16    client: Weak<Client>,
17    url: String,
18}
19
20impl SetApi {
21    pub(crate) fn new(client: Weak<Client>, url: String) -> SetApi {
22        SetApi { client, url }
23    }
24
25    /// Returns all Sets
26    #[allow(dead_code)]
27    pub async fn all(&self) -> Result<ApiResponse<Vec<SetDetail>>, Error> {
28        let url = [&self.url, "/sets"].join("");
29        let mut response = util::send_response(&url, &self.client).await?;
30        let headers = std::mem::take(response.headers_mut());
31        let body = response.text().await.context(MtgApiErrorKind::BodyReadError)?;
32        let sets = util::retrieve_sets_from_body(&body)?;
33        Ok(ApiResponse::new(sets, headers))
34    }
35
36    /// Returns all sets matching the supplied filter
37    #[allow(dead_code)]
38    pub async fn all_filtered(&self, filter: SetFilter) -> Result<ApiResponse<Vec<SetDetail>>, Error> {
39        let url = SetApi::create_filtered_url(&self.url, filter);
40        let mut response = util::send_response(&url, &self.client).await?;
41        let headers = std::mem::take(response.headers_mut());
42        let body = response.text().await.context(MtgApiErrorKind::BodyReadError)?;
43        let sets = util::retrieve_sets_from_body(&body)?;
44        Ok(ApiResponse::new(sets, headers))
45    }
46
47    /// Returns the specified set by the set code
48    pub async fn find<'a, T>(&self, code: T) -> Result<ApiResponse<SetDetail>, Error>
49    where
50        T: Into<&'a str>,
51    {
52        let url = [&self.url, "/sets/", code.into()].join("");
53        let mut response = util::send_response(&url, &self.client).await?;
54        let headers = std::mem::take(response.headers_mut());
55        let body = response.text().await.context(MtgApiErrorKind::BodyReadError)?;
56        let set = util::retrieve_set_from_body(&body)?;
57        Ok(ApiResponse::new(*set, headers))
58    }
59
60    /// Returns a sample booster pack of cards from the specified set
61    pub async fn booster<'a, T>(&self, code: T) -> Result<ApiResponse<Vec<CardDetail>>, Error>
62    where
63        T: Into<&'a str>,
64    {
65        let url = [&self.url, "/sets/", code.into(), "/booster"].join("");
66        let mut response = util::send_response(&url, &self.client).await?;
67        let headers = std::mem::take(response.headers_mut());
68        let body = response.text().await.context(MtgApiErrorKind::BodyReadError)?;
69        let cards = util::retrieve_cards_from_body(&body)?;
70        Ok(ApiResponse::new(cards, headers))
71    }
72
73    fn create_filtered_url(api_url: &str, filter: SetFilter) -> String {
74        let url = [api_url, "/sets"].join("");
75        if filter.0.is_empty() {
76            url
77        } else {
78            [url, filter.0].join("?")
79        }
80    }
81}