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
extern crate log;
extern crate reqwest;
#[macro_use]
extern crate serde;
use model::{Ability, Gear, Ship, Unit};
pub mod model;
const BASE_API_URL: &str = "https://swgoh.gg/api/";
#[macro_export]
macro_rules! get_url {
($p:expr) => {
format!("{}{}/", BASE_API_URL, $p)
};
($p:expr, $i:expr) => {
format!("{}{}/{}/", BASE_API_URL, $p, $i)
};
}
#[macro_export]
macro_rules! execute_get {
($url: expr) => {
reqwest::blocking::get(&$url)?.json()
};
}
pub type Reply<T> = reqwest::Result<T>;
pub fn get_abilities() -> Reply<Vec<Ability>> {
execute_get!(get_url!("abilities"))
}
pub fn get_gears() -> Reply<Vec<Gear>> {
execute_get!(get_url!("gear"))
}
pub fn get_gear(id: u64) -> Reply<Gear> {
execute_get!(get_url!("gear", id))
}
pub fn get_ships() -> Reply<Vec<Ship>> {
execute_get!(get_url!("ships"))
}
pub fn get_ship(id: u64) -> Reply<Ship> {
execute_get!(get_url!("ships", id))
}
pub fn get_units() -> Reply<Vec<Unit>> {
execute_get!(get_url!("characters"))
}
pub fn get_unit(id: u64) -> Reply<Unit> {
execute_get!(get_url!("characters", id))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn abilities_works() {
get_abilities().unwrap();
}
#[test]
fn gears_works() {
get_gears().unwrap();
}
#[test]
fn gear_works() {
get_gear(1).unwrap();
}
#[test]
fn ships_works() {
get_ships().unwrap();
}
#[test]
fn ship_works() {
get_ship(1).unwrap();
}
#[test]
fn units_works() {
let units = get_units().unwrap();
let mt: Vec<&Unit> = units.iter().filter(|u| u.pk == 200).collect();
assert_eq!(mt.len(), 1);
assert_eq!(&mt[0].name, "Darth Vader");
}
#[test]
fn unit_works() {
let unit = get_unit(200).unwrap();
assert_eq!("Darth Vader", &unit.name);
}
}