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
use api::error::MtgApiErrorKind;
use failure::Error;
use failure::ResultExt;
use reqwest::Client;

use std::sync::Weak;

use api::response::ApiResponse;
use api::util;
use API_URL;

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

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

    /// Returns all types
    #[allow(dead_code)]
    pub fn all(&self) -> Result<ApiResponse<Vec<String>>, Error> {
        let url = [API_URL, "/formats"].join("");
        let mut response = util::send_response(&url, &self.client)?;
        let body = response.text().context(MtgApiErrorKind::BodyReadError)?;
        let formats = util::retrieve_formats_from_body(&body)?;
        Ok(ApiResponse::new(formats, response.headers()))
    }
}