mtgapi_client/api/
util.rs

1use crate::api::error::MtgApiErrorKind;
2use failure::Error;
3use failure::ResultExt;
4use crate::model::card::CardDetail;
5use crate::model::set::SetDetail;
6use reqwest::Client;
7use reqwest::Response;
8use serde_json;
9use std::rc::Weak;
10
11pub(crate) async fn send_response(url: &str, client: &Weak<Client>) -> Result<Response, Error> {
12    let client = match client.upgrade() {
13        Some(client) => Ok(client),
14        None => Err(MtgApiErrorKind::ClientDropped),
15    }?;
16    info!("GET; {}", &url);
17    Ok(client.get(url).send().await?)
18}
19
20pub(crate) fn retrieve_cards_from_body(body: &str) -> Result<Vec<CardDetail>, Error> {
21    use crate::model::card::CardsDto;
22    match serde_json::from_str::<CardsDto>(&body).context(MtgApiErrorKind::CardBodyParseError)? {
23        CardsDto::Cards { cards } => Ok(cards),
24        CardsDto::Error { error, status } => match status {
25            Some(status) => Err(MtgApiErrorKind::ApiError {
26                cause: format!("{}: {}", status, error),
27            }.into()),
28            None => Err(MtgApiErrorKind::ApiError { cause: error }.into()),
29        },
30    }
31}
32
33pub(crate) fn retrieve_card_from_body(body: &str) -> Result<Box<CardDetail>, Error> {
34    use crate::model::card::CardDto;
35    match serde_json::from_str::<CardDto>(&body).context(MtgApiErrorKind::CardBodyParseError)? {
36        CardDto::Card { card } => Ok(card),
37        CardDto::Error { error, status } => match status {
38            Some(status) => Err(MtgApiErrorKind::ApiError {
39                cause: format!("{}: {}", status, error),
40            }.into()),
41            None => Err(MtgApiErrorKind::ApiError { cause: error }.into()),
42        },
43    }
44}
45
46pub(crate) fn retrieve_sets_from_body(body: &str) -> Result<Vec<SetDetail>, Error> {
47    use crate::model::set::SetsDto;
48    match serde_json::from_str::<SetsDto>(&body).context(MtgApiErrorKind::SetBodyParseError)? {
49        SetsDto::Sets { sets } => Ok(sets),
50        SetsDto::Error { error, status } => match status {
51            Some(status) => Err(MtgApiErrorKind::ApiError {
52                cause: format!("{}: {}", status, error),
53            }.into()),
54            None => Err(MtgApiErrorKind::ApiError { cause: error }.into()),
55        },
56    }
57}
58
59pub(crate) fn retrieve_set_from_body(body: &str) -> Result<Box<SetDetail>, Error> {
60    use crate::model::set::SetDto;
61    match serde_json::from_str::<SetDto>(&body).context(MtgApiErrorKind::SetBodyParseError)? {
62        SetDto::Set { set } => Ok(set),
63        SetDto::Error { error, status } => match status {
64            Some(status) => Err(MtgApiErrorKind::ApiError {
65                cause: format!("{}: {}", status, error),
66            }.into()),
67            None => Err(MtgApiErrorKind::ApiError { cause: error }.into()),
68        },
69    }
70}
71
72pub(crate) fn retrieve_formats_from_body(body: &str) -> Result<Vec<String>, Error> {
73    use crate::model::format::FormatDto;
74    match serde_json::from_str::<FormatDto>(&body).context(MtgApiErrorKind::FormatBodyParseError)? {
75        FormatDto::Formats { formats } => Ok(formats),
76        FormatDto::Error { error, status } => match status {
77            Some(status) => Err(MtgApiErrorKind::ApiError {
78                cause: format!("{}: {}", status, error),
79            }.into()),
80            None => Err(MtgApiErrorKind::ApiError { cause: error }.into()),
81        },
82    }
83}
84
85pub(crate) fn retrieve_types_from_body(body: &str) -> Result<Vec<String>, Error> {
86    use crate::model::types::TypesDto;
87    match serde_json::from_str::<TypesDto>(&body).context(MtgApiErrorKind::TypeBodyParseError)? {
88        TypesDto::Types { types } => Ok(types),
89        TypesDto::Error { error, status } => match status {
90            Some(status) => Err(MtgApiErrorKind::ApiError {
91                cause: format!("{}: {}", status, error),
92            }.into()),
93            None => Err(MtgApiErrorKind::ApiError { cause: error }.into()),
94        },
95    }
96}
97
98pub(crate) fn retrieve_subtypes_from_body(body: &str) -> Result<Vec<String>, Error> {
99    use crate::model::types::SubtypesDto;
100    match serde_json::from_str::<SubtypesDto>(&body).context(MtgApiErrorKind::TypeBodyParseError)? {
101        SubtypesDto::Subtypes { subtypes } => Ok(subtypes),
102        SubtypesDto::Error { error, status } => match status {
103            Some(status) => Err(MtgApiErrorKind::ApiError {
104                cause: format!("{}: {}", status, error),
105            }.into()),
106            None => Err(MtgApiErrorKind::ApiError { cause: error }.into()),
107        },
108    }
109}
110
111pub(crate) fn retrieve_supertypes_from_body(body: &str) -> Result<Vec<String>, Error> {
112    use crate::model::types::SupertypesDto;
113    match serde_json::from_str::<SupertypesDto>(&body).context(MtgApiErrorKind::TypeBodyParseError)?
114    {
115        SupertypesDto::Supertypes { supertypes } => Ok(supertypes),
116        SupertypesDto::Error { error, status } => match status {
117            Some(status) => Err(MtgApiErrorKind::ApiError {
118                cause: format!("{}: {}", status, error),
119            }.into()),
120            None => Err(MtgApiErrorKind::ApiError { cause: error }.into()),
121        },
122    }
123}