mtgapi_client/api/format/
format_api.rs

1use crate::api::error::MtgApiErrorKind;
2use failure::Error;
3use failure::ResultExt;
4use reqwest::Client;
5
6use std::rc::Weak;
7
8use crate::api::response::ApiResponse;
9use crate::api::util;
10
11///Responsible for the calls to the /formats endpoint
12pub struct FormatApi {
13    client: Weak<Client>,
14    url: String,
15}
16
17impl FormatApi {
18    pub(crate) fn new(client: Weak<Client>, url: String) -> FormatApi {
19        FormatApi { client, url }
20    }
21
22    /// Returns all types
23    #[allow(dead_code)]
24    pub async fn all(&self) -> Result<ApiResponse<Vec<String>>, Error> {
25        let url = [&self.url, "/formats"].join("");
26        let mut response = util::send_response(&url, &self.client).await?;
27        let headers = std::mem::take(response.headers_mut());
28        let body = response.text().await.context(MtgApiErrorKind::BodyReadError)?;
29        let formats = util::retrieve_formats_from_body(&body)?;
30        Ok(ApiResponse::new(formats, headers))
31    }
32}