-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move serialization and requests from Rail Client to Trait
- Loading branch information
1 parent
23b4e13
commit 8e92fc0
Showing
5 changed files
with
95 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub mod bus; | ||
pub mod rail; | ||
|
||
pub mod traits; | ||
pub mod error; | ||
|
||
pub use rail::client::Client as RailClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::error::{Error, ErrorResponse}; | ||
|
||
use reqwest; | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
use serde_json; | ||
|
||
pub trait ApiKey { | ||
fn api_key(&self) -> &str; | ||
} | ||
|
||
pub trait Fetch: Requester + Deserializer { | ||
fn fetch<U, V>(&self, path: &str, query: Option<V>) -> Result<U, Error> | ||
where | ||
U: DeserializeOwned, | ||
V: Serialize + Sized, | ||
{ | ||
self.request(path, query).and_then(Self::deserialize) | ||
} | ||
} | ||
|
||
pub trait Requester: ApiKey { | ||
fn request<T>(&self, path: &str, query: Option<T>) -> Result<String, Error> | ||
where | ||
T: Serialize + Sized, | ||
{ | ||
let mut request = reqwest::Client::new().get(path); | ||
|
||
if let Some(some_query) = query { | ||
request = request.query(&some_query) | ||
} | ||
|
||
request | ||
.header("api_key", self.api_key()) | ||
.send() | ||
.and_then(|mut response| response.text()) | ||
.map_err(|err| Error::new(err.to_string())) | ||
} | ||
} | ||
|
||
pub trait Deserializer { | ||
fn deserialize<T>(response: String) -> Result<T, Error> | ||
where | ||
T: DeserializeOwned, | ||
{ | ||
serde_json::from_str::<T>(&response).or_else(|_| { | ||
match serde_json::from_str::<ErrorResponse>(&response) { | ||
Ok(json) => Err(Error::new(json.message.to_string())), | ||
Err(err) => Err(Error::new(err.to_string())), | ||
} | ||
}) | ||
} | ||
} | ||
|
||
impl<T> Requester for T where T: ApiKey {} | ||
|
||
impl<T> Deserializer for T where T: ApiKey {} | ||
|
||
impl<T> Fetch for T where T: ApiKey {} |