ampr_api/
lib.rs

1use std::collections::HashMap;
2
3use auth::Auth;
4use changelog::ChangeLogEntry;
5use encap::EncapEntry;
6use errors::Error;
7use reqwest::Response;
8use semver::Version;
9
10pub mod auth;
11pub mod changelog;
12pub mod errors;
13pub mod encap;
14
15/// User agent used to identify the client.
16const USER_AGENT: &str =
17    "ampr-api/0.1.x (An AMPRNET API client for Rust) https://github.com/ewpratten/ampr-api";
18
19/// API base URL
20const BASE_URL: &str = "https://portal.ampr.org/api/v1";
21
22async fn make_raw_get(auth: &Auth, endpoint: &str) -> Result<Response, Error> {
23    Ok(reqwest::ClientBuilder::new()
24        .user_agent(USER_AGENT)
25        .build()
26        .unwrap()
27        .post(format!("{}/{}", BASE_URL, endpoint))
28        .basic_auth(auth.callsign.clone(), Some(auth.api_key.clone()))
29        .send()
30        .await?)
31}
32
33/// Returns the current version of the API.
34pub async fn get_api_version(auth: &Auth) -> Result<Version, Error> {
35    // Set up and send the request.
36    let res = make_raw_get(auth, "version").await?;
37
38    // Parse the response.
39    Ok(
40        serde_json::from_str::<HashMap<String, String>>(&res.text().await?)
41            .unwrap()
42            .get("version")
43            .unwrap()
44            .parse()?,
45    )
46}
47
48/// Returns a complete history of changes to the API
49pub async fn get_api_changelog(auth: &Auth) -> Result<Vec<ChangeLogEntry>, Error> {
50    // Set up and send the request.
51    let res = make_raw_get(auth, "changeLog").await?;
52
53    // Parse the response.
54    let response_map: HashMap<String, String> = serde_json::from_str(&res.text().await?).unwrap();
55
56    // Build the log
57    Ok(response_map
58        .iter()
59        .map(|(version, text)| ChangeLogEntry::new(version, text).unwrap())
60        .collect())
61}
62
63/// Returns a list of available endpoints as key/value pairs.
64///
65/// The key contains the name of the endpoint, whilst the value contains the method for
66/// accessing the endpoint, i.e. one of: `GET`, `POST`, `PUT`, `PATCH` or `DELETE`, followed by a
67/// single space, followed by a brief description of each endpoint
68pub async fn get_api_endpoints(auth: &Auth) -> Result<HashMap<String, String>, Error> {
69    // Set up and send the request.
70    let res = make_raw_get(auth, "endpoints").await?;
71
72    // Parse the response.
73    Ok(serde_json::from_str(&res.text().await?).unwrap())
74}
75
76/// Returns the current encap serial number
77pub async fn get_encap_serial(auth: &Auth) -> Result<f32, Error> {
78    // Set up and send the request.
79    let res = make_raw_get(auth, "encapSerial").await?;
80
81    // Parse the response.
82    Ok(
83        serde_json::from_str::<HashMap<String, String>>(&res.text().await?)
84            .unwrap()
85            .get("serial")
86            .unwrap()
87            .parse()?,
88    )
89}
90
91/// Returns the current live encap data
92pub async fn get_encap(auth: &Auth) -> Result<Vec<EncapEntry>, Error> {
93    // Set up and send the request.
94    let res = make_raw_get(auth, "endpoints").await?;
95
96    // Parse the response.
97    Ok(serde_json::from_str(&res.text().await?).unwrap())
98}