diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/api.rs | 20 |
1 files changed, 12 insertions, 8 deletions
@@ -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) + } } |