Skip to content

Commit

Permalink
fix(refresh): BorrowMut for & and owned Client
Browse files Browse the repository at this point in the history
That way, we are most flexible, at the cost of additional code.
  • Loading branch information
Byron committed Feb 27, 2015
1 parent 4486bd5 commit 88d4bf8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
//! use oauth2::{RefreshFlow, AuthenticationType, RefreshResult};
//!
//! # #[test] fn refresh() {
//! let mut c = hyper::Client::new();
//! let mut f = RefreshFlow::new(&mut c);
//! let mut f = RefreshFlow::new(hyper::Client::new());
//! let new_token = match *f.refresh_token(AuthenticationType::Device,
//! "my_client_id", "my_secret",
//! "my_refresh_token") {
Expand Down
18 changes: 12 additions & 6 deletions src/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ use hyper::header::ContentType;
use rustc_serialize::json;
use url::form_urlencoded;
use super::Token;
use std::borrow::BorrowMut;
use std::marker::PhantomData;

/// Implements the [Outh2 Refresh Token Flow](https://developers.google.com/youtube/v3/guides/authentication#devices).
///
/// Refresh an expired access token, as obtained by any other authentication flow.
/// This flow is useful when your `Token` is expired and allows to obtain a new
/// and valid access token.
pub struct RefreshFlow<'a, NC> where NC: 'a {
client: &'a mut hyper::Client<NC>,
pub struct RefreshFlow<C, NC>
where C: BorrowMut<hyper::Client<NC>> {
client: C,
result: RefreshResult,
_m: PhantomData<NC>,
}


Expand All @@ -28,13 +32,15 @@ pub enum RefreshResult {
Success(Token),
}

impl<'a, NC> RefreshFlow<'a, NC>
where NC: hyper::net::NetworkConnector {
impl<C, NC> RefreshFlow<C, NC>
where NC: hyper::net::NetworkConnector,
C: BorrowMut<hyper::Client<NC>> {

pub fn new(client: &'a mut hyper::Client<NC>) -> RefreshFlow<NC> {
pub fn new(client: C) -> RefreshFlow<C, NC> {
RefreshFlow {
client: client,
result: RefreshResult::Error(hyper::HttpError::HttpStatusError),
_m: PhantomData,
}
}

Expand Down Expand Up @@ -67,7 +73,7 @@ impl<'a, NC> RefreshFlow<'a, NC>
.iter().cloned());

let json_str =
match self.client.post(auth_type.as_slice())
match self.client.borrow_mut().post(auth_type.as_slice())
.header(ContentType("application/x-www-form-urlencoded".parse().unwrap()))
.body(req.as_slice())
.send() {
Expand Down

0 comments on commit 88d4bf8

Please sign in to comment.