Skip to content

Commit

Permalink
Merge pull request #80 from lucab/ups/v2-helpers
Browse files Browse the repository at this point in the history
v2: add an ensure_v2 helper
  • Loading branch information
steveej authored Jan 9, 2019
2 parents 7deaeb8 + b7b2b13 commit 9cf2bdc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//!
//! ## Example
//!
//! ```rust
//! ```rust,no_run
//! # extern crate dkregistry;
//! # extern crate tokio_core;
//! # fn main() {
Expand Down
16 changes: 15 additions & 1 deletion src/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! ## Example
//!
//! ```rust
//! ```rust,no_run
//! # extern crate dkregistry;
//! # extern crate tokio_core;
//! # fn main() {
Expand Down Expand Up @@ -102,6 +102,20 @@ impl Client {
Ok(req)
}

/// Ensure remote registry supports v2 API.
pub fn ensure_v2_registry(self) -> impl Future<Item = Self, Error = Error> {
self.is_v2_supported()
.map(move |ok| (ok, self))
.and_then(|(ok, client)| {
if !ok {
bail!("remote server does not support docker-registry v2 API")
} else {
Ok(client)
}
})
}

/// Check whether remote registry supports v2 API.
pub fn is_v2_supported(&self) -> FutureBool {
let api_header = "Docker-Distribution-API-Version";
let api_version = "registry/2.0";
Expand Down
8 changes: 5 additions & 3 deletions tests/mock/api_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ fn test_version_check_status_ok() {
.build()
.unwrap();

let futcheck = dclient.is_v2_supported();
let is_v2 = dclient.is_v2_supported();
let ok = tcore.run(is_v2).unwrap();
assert_eq!(ok, true);

let res = tcore.run(futcheck).unwrap();
assert_eq!(res, true);
let ensure_v2 = dclient.ensure_v2_registry();
let dclient = tcore.run(ensure_v2).unwrap();

mockito::reset();
}
Expand Down

0 comments on commit 9cf2bdc

Please sign in to comment.