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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::time::Duration;
use reqwest::Url;
use serde::{Deserialize, Serialize};
use crate::client::Client;
use crate::tls::{Certificate, Identity};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Auth {
pub username: String,
pub password: Option<String>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ClientConfig {
url: Url,
root_certs: Vec<Certificate>,
identity: Option<Identity>,
auth: Option<Auth>,
}
impl ClientConfig {
pub fn new(url: Url) -> ClientConfig {
ClientConfig {
url,
root_certs: Vec::new(),
identity: None,
auth: None,
}
}
pub fn add_root_certificate(mut self, cert: Certificate) -> ClientConfig {
self.root_certs.push(cert);
self
}
pub fn identity(mut self, identity: Identity) -> ClientConfig {
self.identity = Some(identity);
self
}
pub fn auth(mut self, username: String, password: Option<String>) -> ClientConfig {
self.auth = Some(Auth { username, password });
self
}
pub fn build(self) -> Result<Client, anyhow::Error> {
let mut builder = mz_http_proxy::reqwest::client_builder();
for root_cert in self.root_certs {
builder = builder.add_root_certificate(root_cert.into());
}
if let Some(ident) = self.identity {
builder = builder.identity(ident.into());
}
let inner = builder
.redirect(reqwest::redirect::Policy::none())
.timeout(Duration::from_secs(60))
.build()
.unwrap();
Client::new(inner, self.url, self.auth)
}
}