aboutsummaryrefslogtreecommitdiffstats
path: root/src/api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api.rs')
-rw-r--r--src/api.rs20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/api.rs b/src/api.rs
index 6c85d76..b0b89cf 100644
--- a/src/api.rs
+++ b/src/api.rs
@@ -63,21 +63,25 @@ impl Api {
/// Fetch the list of stations and their basic data from the api.
pub fn stations(&self) -> ApiResult<Vec<Station>> {
- let response_json = self.client.get(&url_for("stations"))
- .send()?
- .text()?;
-
+ let response_json = self.get("stations")?;
let v: StationContainer = serde_json::from_str(&response_json)?;
Ok(v.stations)
}
/// Fetch availability status for all stations
pub fn station_availability(&self) -> ApiResult<Vec<StationAvailability>> {
- let response_json = self.client.get(&url_for("stations/availability"))
- .send()?
- .text()?;
-
+ let response_json = self.get("stations/availability")?;
let v: StationAvailabilityContainer = serde_json::from_str(&response_json)?;
Ok(v.stations)
}
+
+ /// A helper method to fetch payloads from the upstream API
+ fn get(&self, path: &str) -> ApiResult<String> {
+ self.client
+ .get(&url_for(path))
+ .send()?
+ .error_for_status()?
+ .text()
+ .map_err(crate::error::Error::ReqwestError)
+ }
}