fibreq 1.0.0

Non-blocking HTTP client for Tarantool apps.
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! The `request` module defines structures and functionalities for creating and manipulating HTTP requests.
//!
//! This module provides the `Request` struct for representing HTTP requests, along with a `Builder`
//! pattern for constructing requests with various options like headers, body content, and timeouts.
//! It leverages `http_types` for underlying HTTP elements and offers an ergonomic API for request construction.

use crate::{client, debug, error, response, types, utils};
use http_types::headers;
use http_types::headers::ToHeaderValues;
use std::{collections, fmt, time};

#[cfg(not(feature = "picodata_tarantool"))]
use tarantool::fiber::{self};

#[cfg(feature = "picodata_tarantool")]
use picodata_tarantool::system::tarantool::fiber::{self};

/// Represents an HTTP request, including headers, body, and configuration for timeouts.
#[derive(Debug)]
#[allow(clippy::struct_field_names)]
pub struct Request {
    body: Option<http_types::Body>,
    headers: types::Headers,
    inner: http_types::Request,

    tls_timeout: Option<time::Duration>,
    request_timeout: Option<time::Duration>,
    response_timeout: Option<time::Duration>,
}

impl debug::BodyWrapper for Request {
    fn body_back(&mut self, body: Vec<u8>) {
        self.body = Some(body.into());
    }
}

impl Request {
    /// Creates a new `Request` with the specified HTTP method and URL.
    ///
    /// Automatically sets default headers such as `Host`, `User-Agent`, `Accept`, and `Connection`.
    ///
    /// # Parameters
    ///
    /// - `method`: The HTTP method for the request.
    /// - `url`: The URL to which the request will be sent.
    ///
    /// # Returns
    ///
    /// A new instance of `Request`.
    pub fn new(method: http_types::Method, url: http_types::Url) -> Self {
        let headers = collections::HashMap::from([
            (
                headers::HOST,
                url.host()
                    .unwrap()
                    .to_string()
                    .to_header_values()
                    .unwrap()
                    .collect::<headers::HeaderValues>(),
            ),
            (
                headers::USER_AGENT,
                "fibreq/1.0"
                    .to_header_values()
                    .unwrap()
                    .collect::<headers::HeaderValues>(),
            ),
            (
                headers::ACCEPT,
                "*/*"
                    .to_header_values()
                    .unwrap()
                    .collect::<headers::HeaderValues>(),
            ),
            (
                headers::CONNECTION,
                "Keep-Alive"
                    .to_header_values()
                    .unwrap()
                    .collect::<headers::HeaderValues>(),
            ),
        ]);
        Self {
            headers,
            body: None,
            tls_timeout: None,
            request_timeout: None,
            response_timeout: None,
            inner: http_types::Request::new(method, url),
        }
    }

    /// Splits the `Request` into its constituent parts.
    ///
    /// This method is primarily used internally to pass the request components to the execution engine.
    ///
    /// # Returns
    ///
    /// A tuple containing the `HttpRequest`, headers, body, and configured timeouts.
    #[allow(clippy::type_complexity)]
    pub(crate) fn pieces(
        self,
    ) -> (
        http_types::Request,
        types::Headers,
        Option<http_types::Body>,
        time::Duration,
        time::Duration,
        time::Duration,
    ) {
        (
            self.inner,
            self.headers,
            self.body,
            self.tls_timeout.unwrap_or(time::Duration::from_secs(30)),
            self.request_timeout
                .unwrap_or(time::Duration::from_secs(30)),
            self.response_timeout
                .unwrap_or(time::Duration::from_secs(30)),
        )
    }

    /// Returns the url of the HTTP request.
    ///
    /// # Returns
    ///
    /// The HTTP url code as `&http_types::Url`.
    pub fn url(&self) -> &http_types::Url {
        self.inner.url()
    }

    /// Returns the headers of the HTTP request.
    ///
    /// # Returns
    ///
    /// The HTTP headers as `&types::Headers`.
    #[inline]
    pub fn headers(&self) -> &types::Headers {
        &self.headers
    }

    /// Returns the mutable headers of the HTTP request.
    ///
    /// # Returns
    ///
    /// The HTTP headers as `&mut types::Headers`.
    #[inline]
    pub fn headers_mut(&mut self) -> &mut types::Headers {
        &mut self.headers
    }

    /// Returns the body of the HTTP request.
    ///
    /// # Returns
    ///
    /// The HTTP body as `Option<&http_types::Body>`.
    #[inline]
    pub fn body(&self) -> Option<&http_types::Body> {
        self.body.as_ref()
    }

    /// Returns the mutable body of the HTTP request.
    ///
    /// # Returns
    ///
    /// The HTTP body as `Option<&mut http_types::Body>`.
    #[inline]
    pub fn body_mut(&mut self) -> Option<&mut http_types::Body> {
        self.body.as_mut()
    }

    /// Returns a wrapped version of `Request` body.
    ///
    /// It's binary representation will be brought back to request on [`debug::WrappedBody`] `Drop`.
    ///
    /// # Returns
    ///
    /// A wrapped request body.
    #[inline]
    pub fn wrapped_body(&mut self) -> debug::WrappedBody<'_, Request> {
        match self.body.take() {
            Some(body) => {
                let b =
                    fiber::block_on(body.into_bytes()).map_err(|e| Box::new(error::Error::HTTP(e)));
                debug::WrappedBody::new(self, Some(b))
            }
            None => debug::WrappedBody::new(self, None),
        }
    }
}

/// A builder for constructing instances of `Request`.
///
/// Provides a fluent interface to set various aspects of an HTTP request,
/// including headers, body, authentication, and timeouts. It leverages a
/// `client::Client` for sending the constructed request.
#[derive(Debug)]
pub struct Builder {
    request: Request,
    client: client::Client,
}

impl Builder {
    /// Constructs a new `Builder` with the given `Request` and `Client`.
    ///
    /// This method is internal and intended to be used by the Fibreq library
    /// to initiate request construction.
    ///
    /// # Parameters
    ///
    /// - `request`: The initial `Request` object.
    /// - `client`: The `Client` that will be used to send the request.
    pub(crate) fn new(request: Request, client: client::Client) -> Self {
        Self { request, client }
    }

    /// Sets a single header for the request.
    ///
    /// # Type Parameters
    ///
    /// - `K`: The type of the header name, implementing `Into<headers::HeaderName>`.
    /// - `V`: The type of the header value, implementing `ToHeaderValues`.
    ///
    /// # Parameters
    ///
    /// - `key`: The name of the header.
    /// - `value`: The value of the header.
    ///
    /// # Returns
    ///
    /// Returns `Self` for chaining, or an `Error` if the header value conversion fails.
    ///
    /// # Errors
    ///
    /// Returns an `error::Error::HTTP` in case of failed converting value to header value.
    #[allow(clippy::needless_pass_by_value)]
    pub fn header<K, V>(self, key: K, value: V) -> Result<Self, Box<error::Error>>
    where
        K: Into<headers::HeaderName>,
        V: ToHeaderValues,
    {
        let Self {
            mut request,
            client,
        } = self;
        let value = value
            .to_header_values()
            .map_err(Box::new(error::Error::HTTP))?
            .collect::<headers::HeaderValues>();
        request.headers.insert(key.into(), value);
        Ok(Self { request, client })
    }

    /// Sets multiple headers for the request.
    ///
    /// # Type Parameters
    ///
    /// - `K`: The type of the header name, implementing `Into<headers::HeaderName>`.
    /// - `V`: The type of the header value, implementing `ToHeaderValues`.
    ///
    /// # Parameters
    ///
    /// - `headers`: A collection of header names and values.
    ///
    /// # Returns
    ///
    /// Returns `Self` for chaining, or an `Error` if any header value conversion fails.
    ///
    /// # Errors
    ///
    /// Returns an `error::Error::HTTP` in case of failed converting value to header value.
    pub fn headers<K, V>(
        self,
        headers: collections::HashMap<K, V>,
    ) -> Result<Self, Box<error::Error>>
    where
        K: Into<headers::HeaderName>,
        V: ToHeaderValues,
    {
        let Self {
            mut request,
            client,
        } = self;
        for (key, value) in headers {
            request.headers.insert(
                key.into(),
                value
                    .to_header_values()
                    .map_err(Box::new(error::Error::HTTP))?
                    .collect::<headers::HeaderValues>(),
            );
        }

        Ok(Self { request, client })
    }

    /// Adds basic authentication to the request.
    ///
    /// # Parameters
    ///
    /// - `username`: The username for basic auth.
    /// - `password`: The password for basic auth, optional.
    ///
    /// # Returns
    ///
    /// Returns `Self` for chaining. Panics if setting the header fails.
    pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> Self
    where
        U: fmt::Display,
        P: fmt::Display,
    {
        self.header(
            headers::AUTHORIZATION,
            utils::basic_auth(username, password),
        )
        .expect("Basic auth header cannot fail.")
    }

    /// Adds bearer token authentication to the request.
    ///
    /// # Parameters
    ///
    /// - `token`: The bearer token.
    ///
    /// # Returns
    ///
    /// Returns `Self` for chaining. Panics if setting the header fails.
    pub fn bearer_auth<T>(self, token: T) -> Self
    where
        T: fmt::Display,
    {
        self.header(headers::AUTHORIZATION, format!("Bearer {token}"))
            .expect("Bearer auth header cannot fail.")
    }

    /// Sets the body of the request.
    ///
    /// # Type Parameters
    ///
    /// - `T`: The type of the body, convertible into `http_types::Body`.
    ///
    /// # Parameters
    ///
    /// - `body`: The body content.
    ///
    /// # Returns
    ///
    /// Returns `Self` for chaining.
    pub fn body<T: Into<http_types::Body>>(self, body: T) -> Self {
        let Self {
            mut request,
            client,
        } = self;
        request.body = Some(body.into());
        Self { request, client }
    }

    /// Adds query parameters to the request URL.
    ///
    /// # Type Parameters
    ///
    /// - `T`: The type of the query parameters, serializable into query string.
    ///
    /// # Parameters
    ///
    /// - `query`: The query parameters.
    ///
    /// # Returns
    ///
    /// Returns `Result<Self, error::Error>`, enabling further chaining of builder methods
    /// or returning an error if serialization fails.
    ///
    /// # Errors
    ///
    /// Returns an `error::Error::HTTP` in case of failed set query.
    pub fn query<T: serde::Serialize>(self, query: &T) -> Result<Self, Box<error::Error>> {
        let Self {
            mut request,
            client,
        } = self;
        request
            .inner
            .set_query(query)
            .map_err(Box::new(error::Error::HTTP))?;
        Ok(Self { request, client })
    }

    /// Sets the request body to a JSON payload.
    ///
    /// Serializes the given object as JSON and sets it as the request body. This method
    /// automatically sets the `Content-Type` header to `application/json`.
    ///
    /// # Type Parameters
    ///
    /// - `T`: The type of the object to serialize into JSON.
    ///
    /// # Parameters
    ///
    /// - `json`: A reference to the object to be serialized.
    ///
    /// # Returns
    ///
    /// Returns `Result<Self, error::Error>`, enabling further chaining of builder methods
    /// or returning an error if serialization fails.
    ///
    /// # Errors
    ///
    /// Returns an `error::Error::HTTP` in case of failed serialization.
    pub fn json<T: serde::Serialize>(self, json: &T) -> Result<Self, Box<error::Error>> {
        let builder = self
            .header(headers::CONTENT_TYPE, "application/json")
            .expect("Json header cannot fail.");
        Ok(builder.body(serde_json::to_vec(json).map_err(|e| Box::new(error::Error::JSON(e)))?))
    }

    /// Sets a custom TLS handshake timeout for the request.
    ///
    /// # Parameters
    ///
    /// - `timeout`: The duration to set as the TLS handshake timeout.
    ///
    /// # Returns
    ///
    /// Returns `Self` for chaining.
    pub fn tls_timeout(self, timeout: time::Duration) -> Self {
        let Self {
            mut request,
            client,
        } = self;
        request.tls_timeout = Some(timeout);
        Self { request, client }
    }

    /// Sets a custom timeout for sending the request.
    ///
    /// # Parameters
    ///
    /// - `timeout`: The duration to set as the request sending timeout.
    ///
    /// # Returns
    ///
    /// Returns `Self` for chaining.
    pub fn request_timeout(self, timeout: time::Duration) -> Self {
        let Self {
            mut request,
            client,
        } = self;
        request.request_timeout = Some(timeout);
        Self { request, client }
    }

    /// Sets a custom timeout for the response read.
    ///
    /// # Parameters
    ///
    /// - `timeout`: The duration to set as the response timeout.
    ///
    /// # Returns
    ///
    /// Returns `Self` for chaining.
    pub fn response_timeout(self, timeout: time::Duration) -> Self {
        let Self {
            mut request,
            client,
        } = self;
        request.response_timeout = Some(timeout);
        Self { request, client }
    }

    /// Finalizes the builder and returns the constructed `Request`.
    ///
    /// # Returns
    ///
    /// The fully constructed `Request` object.
    pub fn build(self) -> Request {
        self.request
    }

    /// Sends the constructed request using the associated client.
    ///
    /// This method executes the request and waits for the response.
    ///
    /// # Returns
    ///
    /// Returns a `Result<response::Response, error::Error>`, containing the response
    /// or an error if the request fails.
    ///
    /// # Errors
    ///
    /// Returns an `error::Error` same as `client::Client::execute`.
    pub fn send(self) -> Result<response::Response, Box<error::Error>> {
        self.client.execute(self.request)
    }
}