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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use hyper::header::{Header, HeaderFormat};
use std::borrow::Cow;
use super::RequestHead;
pub type StaticCowStr = Cow<'static, str>;
pub trait Interceptor: Send + Sync + 'static {
fn intercept(&self, req: &mut RequestHead);
fn chain<I>(self, other: I) -> Chain<Self, I> where Self: Sized, I: Interceptor {
Chain(self, other)
}
}
impl<F> Interceptor for F where F: Fn(&mut RequestHead) + Send + Sync + 'static {
fn intercept(&self, req: &mut RequestHead) {
(*self)(req)
}
}
pub struct Chain<I1, I2>(I1, I2);
impl<I1: Interceptor, I2: Interceptor> Interceptor for Chain<I1, I2> {
fn intercept(&self, req: &mut RequestHead) {
self.0.intercept(req);
self.1.intercept(req);
}
}
pub struct NoIntercept;
impl Interceptor for NoIntercept {
fn intercept(&self, _req: &mut RequestHead) {}
}
pub struct AddHeader<H: Header + HeaderFormat>(pub H);
impl<H: Header + HeaderFormat> Interceptor for AddHeader<H> {
fn intercept(&self, req: &mut RequestHead) {
req.header(self.0.clone());
}
}
pub struct PrependUrl(StaticCowStr);
impl PrependUrl {
pub fn new<U: Into<StaticCowStr>>(url: U) -> Self {
PrependUrl(url.into())
}
}
impl Interceptor for PrependUrl {
fn intercept(&self, req: &mut RequestHead) {
req.prepend_url(&self.0);
}
}
pub struct AppendUrl(StaticCowStr);
impl AppendUrl {
pub fn new<U: Into<StaticCowStr>>(url: U) -> Self {
AppendUrl(url.into())
}
}
pub struct AppendQuery(Vec<(StaticCowStr, StaticCowStr)>);
impl AppendQuery {
pub fn new() -> Self {
AppendQuery(Vec::new())
}
pub fn pair<K, V>(mut self, key: K, val: V) -> Self
where K: Into<StaticCowStr>, V: Into<StaticCowStr> {
self.pair_mut(key, val);
self
}
pub fn pair_mut<K, V>(&mut self, key: K, val: V) -> &mut Self
where K: Into<StaticCowStr>, V: Into<StaticCowStr> {
self.0.push((key.into(), val.into()));
self
}
}
impl Interceptor for AppendQuery {
fn intercept(&self, req: &mut RequestHead) {
req.query(self.0.iter());
}
}