-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreq.rs
More file actions
252 lines (202 loc) · 7.32 KB
/
req.rs
File metadata and controls
252 lines (202 loc) · 7.32 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use std::{net::IpAddr, time::Duration};
use http::{HeaderValue, header};
use magnus::{RHash, Ruby, TryConvert, typed_data::Obj, value::ReprValue};
use serde::Deserialize;
use wreq::{
Client, Proxy, Version,
header::{HeaderMap, OrigHeaderMap},
};
use super::body::{Body, Form, Json};
use crate::{
client::{query::Query, resp::Response},
emulation::Emulation,
error::wreq_error_to_magnus,
extractor::Extractor,
http::Method,
rt::{self, CancellationToken},
};
/// The parameters for a request.
#[derive(Default, Deserialize)]
#[non_exhaustive]
pub struct Request {
/// The emulation option for the request.
#[serde(skip)]
emulation: Option<Emulation>,
/// The proxy to use for the request.
#[serde(skip)]
proxy: Option<Proxy>,
/// Bind to a local IP Address.
local_address: Option<IpAddr>,
/// Bind to an interface by `SO_BINDTODEVICE`.
interface: Option<String>,
/// The timeout to use for the request.
timeout: Option<u64>,
/// The read timeout to use for the request.
read_timeout: Option<u64>,
/// The HTTP version to use for the request.
#[serde(skip)]
version: Option<Version>,
/// The headers to use for the request.
#[serde(skip)]
headers: Option<HeaderMap>,
/// The original headers to use for the request.
#[serde(skip)]
orig_headers: Option<OrigHeaderMap>,
/// The option enables default headers.
default_headers: Option<bool>,
/// The cookies to use for the request.
#[serde(skip)]
cookies: Option<Vec<HeaderValue>>,
/// Whether to allow redirects.
allow_redirects: Option<bool>,
/// The maximum number of redirects to follow.
max_redirects: Option<usize>,
/// Sets gzip as an accepted encoding.
gzip: Option<bool>,
/// Sets brotli as an accepted encoding.
brotli: Option<bool>,
/// Sets deflate as an accepted encoding.
deflate: Option<bool>,
/// Sets zstd as an accepted encoding.
zstd: Option<bool>,
/// The authentication to use for the request.
auth: Option<String>,
/// The bearer authentication to use for the request.
bearer_auth: Option<String>,
/// The basic authentication to use for the request.
basic_auth: Option<(String, Option<String>)>,
/// The query parameters to use for the request.
query: Option<Query>,
/// The form parameters to use for the request.
form: Option<Form>,
/// The JSON body to use for the request.
json: Option<Json>,
/// The body to use for the request.
#[serde(skip)]
body: Option<Body>,
}
impl Request {
/// Create a new [`Request`] from Ruby keyword arguments.
pub fn new(ruby: &Ruby, hash: RHash) -> Result<Self, magnus::Error> {
let kwargs = hash.as_value();
let mut builder: Self = serde_magnus::deserialize(ruby, kwargs)?;
// extra emulation handling
if let Some(v) = hash.get(ruby.to_symbol("emulation")) {
let emulation_obj = Obj::<Emulation>::try_convert(v)?;
builder.emulation = Some((*emulation_obj).clone());
}
// extra version handling
builder.version = Extractor::<Version>::try_convert(kwargs)?.into_inner();
// extra headers handling
builder.headers = Extractor::<HeaderMap>::try_convert(kwargs)?.into_inner();
// extra original headers handling
builder.orig_headers = Extractor::<OrigHeaderMap>::try_convert(kwargs)?.into_inner();
// extra cookies handling
builder.cookies = Extractor::<Vec<HeaderValue>>::try_convert(kwargs)?.into_inner();
// extra proxy handling
builder.proxy = Extractor::<Proxy>::try_convert(kwargs)?.into_inner();
// extra body handling
if let Some(body) = hash.get(ruby.to_symbol("body")) {
builder.body = Some(Body::try_convert(body)?);
}
Ok(builder)
}
}
pub fn execute_request<U: AsRef<str>>(
token: Option<CancellationToken>,
client: Client,
method: Method,
url: U,
mut request: Request,
) -> Result<Response, magnus::Error> {
rt::try_block_on(token.as_ref(), async move {
let mut builder = client.request(method.into_ffi(), url.as_ref());
// Emulation options.
apply_option!(set_if_some_inner, builder, request.emulation, emulation);
// Version options.
apply_option!(set_if_some, builder, request.version, version);
// Timeout options.
apply_option!(
set_if_some_map,
builder,
request.timeout,
timeout,
Duration::from_secs
);
apply_option!(
set_if_some_map,
builder,
request.read_timeout,
read_timeout,
Duration::from_secs
);
// Network options.
apply_option!(set_if_some, builder, request.proxy, proxy);
apply_option!(set_if_some, builder, request.local_address, local_address);
apply_option!(set_if_some, builder, request.interface, interface);
// Headers options.
apply_option!(set_if_some, builder, request.headers, headers);
apply_option!(set_if_some, builder, request.orig_headers, orig_headers);
apply_option!(
set_if_some,
builder,
request.default_headers,
default_headers
);
// Authentication options.
apply_option!(
set_if_some_map_ref,
builder,
request.auth,
auth,
AsRef::<str>::as_ref
);
apply_option!(set_if_some, builder, request.bearer_auth, bearer_auth);
if let Some(basic_auth) = request.basic_auth.take() {
builder = builder.basic_auth(basic_auth.0, basic_auth.1);
}
// Cookies options.
if let Some(cookies) = request.cookies.take() {
for cookie in cookies {
builder = builder.header(header::COOKIE, cookie);
}
}
// Allow redirects options.
match request.allow_redirects {
Some(false) => {
builder = builder.redirect(wreq::redirect::Policy::none());
}
Some(true) => {
builder = builder.redirect(
request
.max_redirects
.take()
.map(wreq::redirect::Policy::limited)
.unwrap_or_default(),
);
}
None => {}
};
// Compression options.
apply_option!(set_if_some, builder, request.gzip, gzip);
apply_option!(set_if_some, builder, request.brotli, brotli);
apply_option!(set_if_some, builder, request.deflate, deflate);
apply_option!(set_if_some, builder, request.zstd, zstd);
// Query options.
apply_option!(set_if_some_ref, builder, request.query, query);
// Form options.
apply_option!(set_if_some_ref, builder, request.form, form);
// JSON options.
apply_option!(set_if_some_ref, builder, request.json, json);
// Body options.
if let Some(body) = request.body.take() {
builder = builder.body(wreq::Body::from(body));
}
// Send request.
builder
.send()
.await
.map(Response::new)
.map_err(wreq_error_to_magnus)
})
}