http-signature-normalization/http-signature-normalization-actix/examples/client.rs

55 lines
1.5 KiB
Rust
Raw Normal View History

2019-09-11 23:06:36 +00:00
use actix::System;
use actix_web::client::Client;
2019-09-13 01:29:24 +00:00
use failure::Fail;
2019-09-11 23:06:36 +00:00
use futures::future::{lazy, Future};
use http_signature_normalization_actix::prelude::*;
2019-09-13 01:12:35 +00:00
use sha2::{Digest, Sha256};
2019-09-11 23:06:36 +00:00
fn main() {
System::new("client-example")
.block_on(lazy(|| {
let config = Config::default();
2019-09-13 01:12:35 +00:00
let mut digest = Sha256::new();
2019-09-11 23:06:36 +00:00
Client::default()
2019-09-13 01:12:35 +00:00
.post("http://127.0.0.1:8010/")
2019-09-11 23:06:36 +00:00
.header("User-Agent", "Actix Web")
.authorization_signature_with_digest(
2019-09-13 01:12:35 +00:00
&config,
"my-key-id",
&mut digest,
"Hewwo-owo",
|s| Ok(base64::encode(s)) as Result<_, MyError>,
2019-09-13 01:12:35 +00:00
)
2019-09-11 23:06:36 +00:00
.unwrap()
.send()
.map_err(|_| ())
.and_then(|mut res| res.body().map_err(|_| ()))
.map(|body| {
println!("{:?}", body);
})
}))
.unwrap();
}
2019-09-13 01:29:24 +00:00
#[derive(Debug, Fail)]
2019-09-11 23:06:36 +00:00
pub enum MyError {
2019-09-13 01:29:24 +00:00
#[fail(display = "Failed to read header, {}", _0)]
Convert(#[cause] ToStrError),
#[fail(display = "Failed to create header, {}", _0)]
Header(#[cause] InvalidHeaderValue),
2019-09-11 23:06:36 +00:00
}
impl From<ToStrError> for MyError {
fn from(e: ToStrError) -> Self {
MyError::Convert(e)
}
}
impl From<InvalidHeaderValue> for MyError {
fn from(e: InvalidHeaderValue) -> Self {
MyError::Header(e)
}
}