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
use std::collections::HashMap;
use auth::Auth;
use changelog::ChangeLogEntry;
use encap::EncapEntry;
use errors::Error;
use reqwest::Response;
use semver::Version;
pub mod auth;
pub mod changelog;
pub mod errors;
pub mod encap;
const USER_AGENT: &str =
"ampr-api/0.1.x (An AMPRNET API client for Rust) https://github.com/ewpratten/ampr-api";
const BASE_URL: &str = "https://portal.ampr.org/api/v1";
async fn make_raw_get(auth: &Auth, endpoint: &str) -> Result<Response, Error> {
Ok(reqwest::ClientBuilder::new()
.user_agent(USER_AGENT)
.build()
.unwrap()
.post(format!("{}/{}", BASE_URL, endpoint))
.basic_auth(auth.callsign.clone(), Some(auth.api_key.clone()))
.send()
.await?)
}
pub async fn get_api_version(auth: &Auth) -> Result<Version, Error> {
let res = make_raw_get(auth, "version").await?;
Ok(
serde_json::from_str::<HashMap<String, String>>(&res.text().await?)
.unwrap()
.get("version")
.unwrap()
.parse()?,
)
}
pub async fn get_api_changelog(auth: &Auth) -> Result<Vec<ChangeLogEntry>, Error> {
let res = make_raw_get(auth, "changeLog").await?;
let response_map: HashMap<String, String> = serde_json::from_str(&res.text().await?).unwrap();
Ok(response_map
.iter()
.map(|(version, text)| ChangeLogEntry::new(version, text).unwrap())
.collect())
}
pub async fn get_api_endpoints(auth: &Auth) -> Result<HashMap<String, String>, Error> {
let res = make_raw_get(auth, "endpoints").await?;
Ok(serde_json::from_str(&res.text().await?).unwrap())
}
pub async fn get_encap_serial(auth: &Auth) -> Result<f32, Error> {
let res = make_raw_get(auth, "encapSerial").await?;
Ok(
serde_json::from_str::<HashMap<String, String>>(&res.text().await?)
.unwrap()
.get("serial")
.unwrap()
.parse()?,
)
}
pub async fn get_encap(auth: &Auth) -> Result<Vec<EncapEntry>, Error> {
let res = make_raw_get(auth, "endpoints").await?;
Ok(serde_json::from_str(&res.text().await?).unwrap())
}