1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::ops::Deref;
use reqwest::header::{HeaderMap, HeaderValue, InvalidHeaderValue};
#[derive(Debug, thiserror::Error)]
#[allow(clippy::module_name_repetitions)]
pub enum ClientError {
#[error("failed to set access key header: {0}")]
AccessKey(#[from] InvalidHeaderValue),
#[error("failed to build client: {0}")]
BuildClient(#[from] reqwest::Error),
}
#[derive(Debug)]
pub struct Client(reqwest::Client);
impl Client {
pub fn new(access_key: &str) -> Result<Self, ClientError> {
let access_key = HeaderValue::from_str(access_key)?;
let mut headers = HeaderMap::with_capacity(1);
headers.insert("AccessKey", access_key);
let inner = reqwest::Client::builder()
.default_headers(headers)
.https_only(true)
.build()?;
Ok(Self(inner))
}
}
impl Deref for Client {
type Target = reqwest::Client;
fn deref(&self) -> &Self::Target {
&self.0
}
}