feat(doit): json decode and delegation

Now json errors are handled and delegated with the option to retry,
and all other values are just decoded according to plan.

For now, I brutally unwrap json values assuming this will work, because
it really should work.
This commit is contained in:
Sebastian Thiel
2015-03-16 17:03:01 +01:00
parent 2c79f6e3cf
commit eef1471357
5 changed files with 1189 additions and 730 deletions

View File

@@ -42,7 +42,7 @@ impl<T: Seek + Read> ReadSeek for T {}
/// A utility type which can decode a server response that indicates error
#[derive(RustcDecodable)]
struct JsonServerError {
pub struct JsonServerError {
error: String,
error_description: Option<String>
}
@@ -68,6 +68,22 @@ pub trait Delegate {
fn api_key(&mut self) -> Option<String> {
None
}
/// Called whenever the Authenticator didn't yield a token. The delegate
/// may attempt to provide one, or just take is a general information about the
/// pending impending failure
fn token(&mut self) -> Option<oauth2::Token> {
None
}
/// Called whenever the http request returns with a non-success status code.
/// This can involve authentication issues, or anything else that very much
/// depends on the used API method.
/// The delegate should check the status, header and decoded json error to decide
/// whether to retry or not. In the latter case, the underlying call will fail.
fn http_failure(&mut self, _: &hyper::client::Response, JsonServerError) -> oauth2::Retry {
oauth2::Retry::Abort
}
}
#[derive(Default)]
@@ -91,6 +107,9 @@ pub enum Result<T = ()> {
/// An additional, free form field clashed with one of the built-in optional ones
FieldClash(&'static str),
/// Indicates an HTTP repsonse with a non-success status code
Failure(hyper::client::Response),
/// It worked !
Success(T),
}

File diff suppressed because it is too large Load Diff