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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use actix_http::client::{ConnectError, Connection, Connector};
use actix_http::http::{
header::IntoHeaderValue, HeaderMap, HeaderName, HttpTryFrom, Uri,
};
use actix_service::Service;
use crate::connect::{Connect, ConnectorWrapper};
use crate::Client;
pub struct ClientBuilder {
connector: Rc<RefCell<dyn Connect>>,
default_headers: bool,
allow_redirects: bool,
max_redirects: usize,
headers: HeaderMap,
}
impl ClientBuilder {
pub fn new() -> Self {
ClientBuilder {
default_headers: true,
allow_redirects: true,
max_redirects: 10,
headers: HeaderMap::new(),
connector: Rc::new(RefCell::new(ConnectorWrapper(
Connector::new().service(),
))),
}
}
pub fn connector<T>(mut self, connector: T) -> Self
where
T: Service<Request = Uri, Error = ConnectError> + 'static,
T::Response: Connection,
<T::Response as Connection>::Future: 'static,
T::Future: 'static,
{
self.connector = Rc::new(RefCell::new(ConnectorWrapper(connector)));
self
}
pub fn disable_redirects(mut self) -> Self {
self.allow_redirects = false;
self
}
pub fn max_redirects(mut self, num: usize) -> Self {
self.max_redirects = num;
self
}
pub fn no_default_headers(mut self) -> Self {
self.default_headers = false;
self
}
pub fn header<K, V>(mut self, key: K, value: V) -> Self
where
HeaderName: HttpTryFrom<K>,
<HeaderName as HttpTryFrom<K>>::Error: fmt::Debug,
V: IntoHeaderValue,
V::Error: fmt::Debug,
{
match HeaderName::try_from(key) {
Ok(key) => match value.try_into() {
Ok(value) => {
self.headers.append(key, value);
}
Err(e) => log::error!("Header value error: {:?}", e),
},
Err(e) => log::error!("Header name error: {:?}", e),
}
self
}
pub fn finish(self) -> Client {
Client {
connector: self.connector,
}
}
}