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
|
use crate::constants;
use reqwest::{
RequestBuilder as ReqwestRequestBuilder,
header::{AUTHORIZATION, CONTENT_TYPE, USER_AGENT, HeaderMap as Headers, HeaderValue},
Url,
};
use super::{
CLIENT,
TOKEN,
HttpError,
routing::RouteInfo,
};
pub struct RequestBuilder<'a> {
body: Option<&'a [u8]>,
headers: Option<Headers>,
route: RouteInfo<'a>,
}
impl<'a> RequestBuilder<'a> {
pub fn new(route_info: RouteInfo<'a>) -> Self {
Self {
body: None,
headers: None,
route: route_info,
}
}
pub fn build(self) -> Request<'a> {
Request::new(self)
}
pub fn body(&mut self, body: Option<&'a [u8]>) -> &mut Self {
self.body = body;
self
}
pub fn headers(&mut self, headers: Option<Headers>) -> &mut Self {
self.headers = headers;
self
}
pub fn route(&mut self, route_info: RouteInfo<'a>) -> &mut Self {
self.route = route_info;
self
}
}
#[derive(Clone, Debug)]
pub struct Request<'a> {
pub(super) body: Option<&'a [u8]>,
pub(super) headers: Option<Headers>,
pub(super) route: RouteInfo<'a>,
}
impl<'a> Request<'a> {
pub fn new(builder: RequestBuilder<'a>) -> Self {
let RequestBuilder { body, headers, route } = builder;
Self { body, headers, route }
}
pub fn build(&'a self) -> Result<ReqwestRequestBuilder, HttpError> {
let Request {
body,
headers: ref request_headers,
route: ref route_info,
} = *self;
let (method, _, path) = route_info.deconstruct();
let mut builder = CLIENT.request(
method.reqwest_method(),
Url::parse(&path)?,
);
if let Some(ref bytes) = body {
builder = builder.body(Vec::from(*bytes));
}
let mut headers = Headers::with_capacity(3);
headers.insert(USER_AGENT, HeaderValue::from_static(&constants::USER_AGENT));
headers.insert(AUTHORIZATION,
HeaderValue::from_str(&TOKEN.lock()).map_err(|e| HttpError::InvalidHeader(e))?);
headers.insert(CONTENT_TYPE, HeaderValue::from_static(&"application/json"));
if let Some(ref request_headers) = request_headers {
headers.extend(request_headers.clone());
}
Ok(builder.headers(headers))
}
pub fn body_ref(&self) -> &Option<&'a [u8]> {
&self.body
}
pub fn body_mut(&mut self) -> &mut Option<&'a [u8]> {
&mut self.body
}
pub fn headers_ref(&self) -> &Option<Headers> {
&self.headers
}
pub fn headers_mut(&mut self) -> &mut Option<Headers> {
&mut self.headers
}
pub fn route_ref(&self) -> &RouteInfo {
&self.route
}
pub fn route_mut(&mut self) -> &mut RouteInfo<'a> {
&mut self.route
}
}
|