mtgapi_client/
lib.rs

1#[macro_use]
2extern crate serde_derive;
3extern crate hyper_rustls;
4extern crate failure_derive;
5#[macro_use]
6extern crate log;
7
8extern crate chrono;
9extern crate failure;
10extern crate itertools;
11extern crate reqwest;
12extern crate serde;
13extern crate serde_json;
14
15pub use crate::api::card::card_api::CardApi as cards;
16use crate::api::card::card_api::CardApi;
17use crate::api::format::format_api::FormatApi;
18use crate::api::set::set_api::SetApi;
19use crate::api::types::type_api::SubtypeApi;
20use crate::api::types::type_api::SupertypeApi;
21use crate::api::types::type_api::TypeApi;
22use reqwest::Client;
23use std::time::Duration;
24use std::rc::Rc;
25
26pub mod api;
27pub mod model;
28
29pub mod prelude {
30    pub use crate::api::card::filter::*;
31    pub use crate::api::card::filtertypes::*;
32    pub use crate::api::set::filter::*;
33    pub use crate::api::set::filtertypes::*;
34    pub use crate::MtgClient;
35}
36
37//const API_URL: &str = "https://api.magicthegathering.io/v1";
38
39/// The MTG.io SDK, use this to access the various api calls
40#[allow(dead_code)]
41pub struct MtgClient {
42    client: Rc<Client>,
43    pub cards: CardApi,
44    pub sets: SetApi,
45    pub types: TypeApi,
46    pub subtypes: SubtypeApi,
47    pub supertypes: SupertypeApi,
48    pub formats: FormatApi,
49}
50
51impl MtgClient {
52    /// Creates a new MTG.io SDK Struct
53    pub fn new(timeout: u64) -> MtgClient {
54        Self::new_with_url("https://api.magicthegathering.io/v1", timeout)
55    }
56
57    /// Creates a new MTG.io SDK Struct with an alternate URL
58    /// "https://api.magicthegathering.io/v1" is the default
59    pub fn new_with_url(url: &str, timeout: u64) -> MtgClient {
60        let client = Rc::new(
61            reqwest::Client::builder()
62                .timeout(Duration::from_secs(timeout))
63                .build()
64                .unwrap(),
65        );
66        let cards = CardApi::new(Rc::downgrade(&client), url.to_string());
67        let sets = SetApi::new(Rc::downgrade(&client), url.to_string());
68        let types = TypeApi::new(Rc::downgrade(&client), url.to_string());
69        let subtypes = SubtypeApi::new(Rc::downgrade(&client), url.to_string());
70        let supertypes = SupertypeApi::new(Rc::downgrade(&client), url.to_string());
71        let formats = FormatApi::new(Rc::downgrade(&client), url.to_string());
72
73        MtgClient {
74            client,
75            cards,
76            sets,
77            types,
78            subtypes,
79            supertypes,
80            formats,
81        }
82    }
83
84    /// /cards enpoint
85    pub fn cards(&self) -> &CardApi {
86        &self.cards
87    }
88
89    /// /sets enpoint
90    pub fn sets(&self) -> &SetApi {
91        &self.sets
92    }
93
94    /// /types enpoint
95    pub fn types(&self) -> &TypeApi {
96        &self.types
97    }
98
99    /// /subtypes enpoint
100    pub fn subtypes(&self) -> &SubtypeApi {
101        &self.subtypes
102    }
103
104    /// /supertypes enpoint
105    pub fn supertypes(&self) -> &SupertypeApi {
106        &self.supertypes
107    }
108
109    /// /formats enpoint
110    pub fn formats(&self) -> &FormatApi {
111        &self.formats
112    }
113}