diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2019-02-23 22:55:36 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2019-02-23 22:55:36 +0100 |
commit | 6c1a836ec0a347a50cb4f71ff178d792ecb88b2b (patch) | |
tree | 71c26f6be36dd78a49a1ee3474fd33d691639279 /src | |
parent | 2feeca134368f128e2133adc4be91d9bd17c0dc5 (diff) | |
download | oslobike-6c1a836ec0a347a50cb4f71ff178d792ecb88b2b.tar.gz oslobike-6c1a836ec0a347a50cb4f71ff178d792ecb88b2b.tar.bz2 oslobike-6c1a836ec0a347a50cb4f71ff178d792ecb88b2b.zip |
Propagate errors from the server back up through the Api.
Also create a small helper method to fetch the payload from the server,
to avoid too much repitition.
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) + } } |