Skip to content

Commit

Permalink
return-future-uncapsuled-and-rewrite-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
steveej committed Oct 17, 2018
1 parent 86e85b4 commit 2f36b4c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
77 changes: 43 additions & 34 deletions examples/login.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
extern crate dkregistry;
extern crate futures;
extern crate tokio_core;

use std::{boxed, error};
use tokio_core::reactor::Core;

use futures::future::Future;

type Result<T> = std::result::Result<T, boxed::Box<error::Error>>;

fn main() {
Expand All @@ -30,42 +33,48 @@ fn main() {
}

fn run(host: &str, user: Option<String>, passwd: Option<String>) -> Result<()> {
let mut tcore = try!(Core::new());
let dclient = try!(
dkregistry::v2::Client::configure(&tcore.handle())
.registry(host)
.insecure_registry(false)
.username(user)
.password(passwd)
.build()
);

let futcheck = try!(dclient.is_v2_supported());
let supported = try!(tcore.run(futcheck));
if !supported {
return Err("API v2 not supported".into());
}
let mut tcore = Core::new()?;

let futauth = try!(dclient.is_auth(None));
let logged_in = try!(tcore.run(futauth));
if logged_in {
return Err("no login performed, but already authenticated".into());
}
let dclient = dkregistry::v2::Client::configure(&tcore.handle())
.registry(host)
.insecure_registry(false)
.username(user)
.password(passwd)
.build()?;

let fut_token = try!(dclient.login(&[]));
let token = try!(tcore.run(fut_token));
let futures = futures::future::ok::<_, dkregistry::errors::Error>(dclient.clone())
.and_then(|dclient| {
dclient.is_v2_supported().and_then(|v2_supported| {
if !v2_supported {
return Err("API v2 not supported".into());
} else {
Ok(dclient)
}
})
}).and_then(|dclient| {
dclient.is_auth(None).and_then(|is_auth| {
if !is_auth {
return Err("no login performed, but already authenticated".into());
} else {
Ok(dclient)
}
})
}).and_then(|dclient| {
dclient.login(&[]).and_then(|token| {
dclient.is_auth(Some(token.token())).and_then(|is_auth| {
if !is_auth {
return Err("login failed".into());
} else {
println!("logged in!")
}
Ok(dclient)
})
})
}).and_then(|dclient| dclient.is_v2_supported());

let futauth = try!(dclient.is_auth(Some(token.token())));
let done = try!(tcore.run(futauth));

match done {
false => return Err("login failed".into()),
true => println!("logged in!",),
match tcore.run(futures) {
Ok(login_successful) if login_successful => Ok(()),
Err(e) => Err(Box::new(e)),
_ => Err("Login unsucessful".into()),
}
let futcheck = try!(dclient.is_v2_supported());
if !try!(tcore.run(futcheck)) {
return Err("API check failed after login".into());
};

return Ok(());
}
18 changes: 10 additions & 8 deletions src/v2/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ impl Client {
/// Perform registry authentication and return an authenticated token.
///
/// On success, the returned token will be valid for all requested scopes.
pub fn login(&self, scopes: &[&str]) -> Result<FutureTokenAuth> {
pub fn login(&self, scopes: &[&str]) -> FutureTokenAuth {
let subclient = self.hclient.clone();
let creds = self.credentials.clone();
let scope = scopes
.iter()
.fold("".to_string(), |acc, &s| acc + "&scope=" + s);
let auth = self
.get_token_provider()?
.get_token_provider()
.unwrap()
.and_then(move |token_ep| {
let auth_ep = token_ep + scope.as_str();
trace!("Token endpoint: {}", auth_ep);
Expand Down Expand Up @@ -113,14 +114,15 @@ impl Client {
}).inspect(|_| {
trace!("Got token");
});
Ok(Box::new(auth))
Box::new(auth)
}

/// Check whether the client is authenticated with the registry.
pub fn is_auth(&self, token: Option<&str>) -> Result<FutureBool> {
let url = try!(hyper::Uri::from_str(
(self.base_url.clone() + "/v2/").as_str()
));
pub fn is_auth(&self, token: Option<&str>) -> FutureBool {
let url = match hyper::Uri::from_str((self.base_url.clone() + "/v2/").as_str()) {
Ok(url) => url,
Err(e) => return Box::new(futures::future::err(e.into())),
};
let mut req = self.new_request(hyper::Method::GET, url.clone());
if let Some(t) = token {
let bearer = format!("Bearer {}", t);
Expand All @@ -144,6 +146,6 @@ impl Client {
_ => Err(format!("is_auth: wrong HTTP status '{}'", status).into()),
}
});
Ok(Box::new(fres))
Box::new(fres)
}
}
8 changes: 3 additions & 5 deletions src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,11 @@ impl Client {
req
}

pub fn is_v2_supported(&self) -> Result<FutureBool> {
pub fn is_v2_supported(&self) -> FutureBool {
let api_header = "Docker-Distribution-API-Version";
let api_version = "registry/2.0";

let url = try!(hyper::Uri::from_str(
(self.base_url.clone() + "/v2/").as_str()
));
let url = hyper::Uri::from_str((self.base_url.clone() + "/v2/").as_str()).unwrap();
let req = self.new_request(hyper::Method::GET, url.clone());
let freq = self.hclient.request(req);
let fres = freq
Expand All @@ -126,7 +124,7 @@ impl Client {
}).inspect(|b| {
trace!("v2 API supported: {}", b);
});
Ok(Box::new(fres))
Box::new(fres)
}
}

Expand Down

0 comments on commit 2f36b4c

Please sign in to comment.