Skip to content

Instantly share code, notes, and snippets.

@nemosupremo
Created April 24, 2019 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nemosupremo/cfb2e27f6b276767374a146441007126 to your computer and use it in GitHub Desktop.
Save nemosupremo/cfb2e27f6b276767374a146441007126 to your computer and use it in GitHub Desktop.
struct AxisApiClient {
client: Client,
}
fn check_authorization(
client: Client,
res: Response,
) -> impl Future<Item = Response, Error = reqwest::Error> {
match res.status() {
http::StatusCode::UNAUTHORIZED => {
info!("authorizing");
let context = AuthContext::new("john", "hello", res.url().path());
let wwwheader = match res.headers()[header::WWW_AUTHENTICATE].to_str() {
Ok(w) => w,
Err(_) => return Either::B(done(Ok(res))),
};
let mut prompt = match digest_auth::parse(wwwheader) {
Ok(p) => p,
Err(_) => return Either::B(done(Ok(res))),
};
let answer = match prompt.respond(&context) {
Ok(a) => a,
Err(_) => return Either::B(done(Ok(res))),
};
Either::A(
client
.get(res.url().as_str())
.header(header::AUTHORIZATION, answer.to_string())
.send(),
)
}
_ => Either::B(done(Ok(res))),
}
}
impl AxisApiClient {
fn new() -> AxisApiClient {
AxisApiClient {
client: Client::new(),
}
}
fn query_people_api(&self, resource: String) -> impl Future<Item = (), Error = ()> {
let client = self.client.clone();
client
.get("http://localhost:8080/")
.send()
.and_then(|res| check_authorization(client, res))
.and_then(|mut res| {
if res.status() == 200 {
info!("authorized");
let body = std::mem::replace(res.body_mut(), Decoder::empty());
body.concat2()
} else {
// some unexpected error
// Err("Unexpected response!")
panic!("Unexptected response")
}
})
.map_err(move |err| warn!("Failed to query API {}: {}", resource, err))
.and_then(|body| {
info!("{:?}", body);
Ok(())
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment