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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use api::card::filter::CardFilter;
use api::card::filtertypes::CardResponseField;
use api::error::MtgApiErrorKind;
use failure::Error;
use failure::ResultExt;
use reqwest::Client;

use api::response::ApiResponse;
use model::card::CardDetail;
use std::sync::Weak;

use api::util;
use API_URL;

///Responsible for the calls to the /cards endpoint
pub struct CardApi {
    client: Weak<Client>,
}

impl CardApi {
    pub(crate) fn new(client: Weak<Client>) -> CardApi {
        CardApi { client }
    }

    /// Returns a Request Object to fetch all cards
    #[allow(dead_code)]
    pub fn all(&self) -> Box<AllCardsRequest> {
        AllCardsRequest::new(self.client.clone(), 100)
    }

    /// Returns a Request Object to fetch all cards with a filter
    #[allow(dead_code)]
    pub fn all_filtered(&self, filter: CardFilter) -> Box<AllCardsRequest> {
        AllCardsRequest::new_filtered(self.client.clone(), 100, filter)
    }

    /// Returns a specific card by a specific id
    pub fn find(&self, id: u32) -> Result<ApiResponse<CardDetail>, Error> {
        let url = [API_URL, "/cards/", &id.to_string()].join("");
        let mut response = util::send_response(&url, &self.client)?;
        let body = response.text().context(MtgApiErrorKind::BodyReadError)?;
        let card = util::retrieve_card_from_body(&body)?;
        Ok(ApiResponse::new(card, response.headers()))
    }
}

/// Request Object to be used to execute requests to the API
#[allow(dead_code)]
pub struct AllCardsRequest {
    page: u32,
    client: Weak<Client>,
    url: String,
    filter: CardFilter,
    order_by: CardResponseField,
    page_size: u32,
}

impl AllCardsRequest {
    fn new(client: Weak<Client>, page_size: u32) -> Box<AllCardsRequest> {
        let url = [API_URL, "cards"].join("/");
        Box::new(AllCardsRequest {
            page: 1,
            client,
            url,
            filter: CardFilter(String::new()),
            page_size,
            order_by: CardResponseField::Name,
        })
    }

    fn new_filtered(
        client: Weak<Client>,
        page_size: u32,
        filter: CardFilter,
    ) -> Box<AllCardsRequest> {
        let url = [API_URL, "cards"].join("/");
        Box::new(AllCardsRequest {
            page: 1,
            client,
            url,
            filter,
            page_size,
            order_by: CardResponseField::Name,
        })
    }

    /// Executes the call to the API.
    /// Repeated calls to this method will return the different pages of the cards API

    /// ```no_run
    /// # use std::error::Error;
    /// # use mtgapi_client::prelude::*;
    /// # fn try_main() -> Result<(), Box<Error>> {
    /// let sdk = MtgClient::new(60);
    /// let mut get_cards_request = sdk.cards().all();
    /// let mut cards = Vec::new();
    /// loop {
    ///     let response = get_cards_request.next_page()?;
    ///     if response.content.is_empty() {break}
    ///     cards.extend(response.content);
    /// }
    /// #
    /// # Ok(())
    /// # }
    /// #
    /// # fn main() {
    /// #     try_main().unwrap();
    /// # }
    /// ```
    /// # Errors
    ///
    /// If this function can't connect to the API or does not manage
    /// to read the response, it will return an error.
    ///
    #[allow(dead_code)]
    pub fn next_page(&mut self) -> Result<ApiResponse<Vec<CardDetail>>, Error> {
        let url = self.create_filtered_url();
        let mut response = util::send_response(&url, &self.client)?;
        self.page += 1;
        let headers = response.headers().clone();
        let body = response.text().context(MtgApiErrorKind::CardBodyParseError)?;
        let cards = util::retrieve_cards_from_body(&body)?;
        Ok(ApiResponse::new(cards, &headers))
    }

    /// Sets the ordering of the cards
    #[allow(dead_code)]
    pub fn order_by(&mut self, field: CardResponseField) {
        self.order_by = field;
    }

    /// Sets the page for the following API calls
    #[allow(dead_code)]
    pub fn set_page(&mut self, page: u32) {
        self.page = page;
    }

    /// Sets the page size for the following API calls
    #[allow(dead_code)]
    pub fn set_page_size(&mut self, size: u32) {
        self.page_size = size;
    }

    fn create_filtered_url(&self) -> String {
        let page_filter = ["page", self.page.to_string().as_str()].join("=");
        let paged_filter = if self.filter.0.is_empty() {
            [self.filter.0.as_str(), &page_filter].join("")
        } else {
            [self.filter.0.as_str(), &page_filter].join("&")
        };
        let page_size_filter = ["pageSize", &self.page_size.to_string()].join("=");
        let paged_filter_sized = [paged_filter, page_size_filter].join("&");

        [self.url.as_str(), paged_filter_sized.as_str()].join("?")
    }
}