-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples/*: streamline, refactor, and rewrite to use future chaining
- Loading branch information
Showing
7 changed files
with
260 additions
and
217 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
extern crate dkregistry; | ||
extern crate futures; | ||
|
||
use futures::prelude::*; | ||
|
||
pub fn authenticate_client<'a>( | ||
client: &'a mut dkregistry::v2::Client, | ||
login_scope: &'a str, | ||
) -> impl futures::future::Future<Item = &'a dkregistry::v2::Client, Error = dkregistry::errors::Error> | ||
{ | ||
futures::future::ok::<_, dkregistry::errors::Error>(client) | ||
.and_then(|dclient| { | ||
dclient.is_v2_supported().and_then(|v2_supported| { | ||
if !v2_supported { | ||
Err("API v2 not supported".into()) | ||
} else { | ||
Ok(dclient) | ||
} | ||
}) | ||
}).and_then(|dclient| { | ||
dclient.is_auth(None).and_then(|is_auth| { | ||
if is_auth { | ||
Err("no login performed, but already authenticated".into()) | ||
} else { | ||
Ok(dclient) | ||
} | ||
}) | ||
}).and_then(move |dclient| { | ||
dclient.login(&[&login_scope]).and_then(move |token| { | ||
dclient | ||
.is_auth(Some(token.token())) | ||
.and_then(move |is_auth| { | ||
if !is_auth { | ||
Err("login failed".into()) | ||
} else { | ||
println!("logged in!"); | ||
Ok(dclient.set_token(Some(token.token()))) | ||
} | ||
}) | ||
}) | ||
}) | ||
} |
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
Oops, something went wrong.