deboa 0.1.5-beta.4

A friendly rest client on top of hyper.
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
use crate::{
    cert::{Certificate, Identity},
    conn::{ConnectionConfig, HttpConnectionDispatcher, HttpConnectionPool},
    dns::DnsResolver,
    errors::{DeboaError, RequestError},
    request::{DeboaRequest, DeboaRequestBuilder, IntoRequest},
    response::DeboaResponse,
};
use async_lock::RwLock;
use http::Version;
use log::info;
use std::{
    future::Future,
    net::{IpAddr, Ipv4Addr},
    ops::Shl,
    time::Duration,
};
use tackle::{Chain, Hook, HookFn};

pub mod cache;
pub mod cert;
pub mod conn;
pub mod cookie;
pub mod dns;
pub mod errors;
pub mod form;
pub mod proto;
pub mod request;
pub mod response;
pub mod serde;
#[cfg(test)]
pub mod tests;
pub mod url;

/// Type alias for Result<T, DeboaError>
/// Convenience alias for handling Deboa errors throughout the library.
///
/// # Examples
///
/// ```
/// use deboa::Result;
///
/// fn example() -> Result<String> {
///     Ok("success".to_string())
/// }
/// ```
///
/// # See Also
/// - [DeboaError](crate::errors::DeboaError)
pub type Result<T> = std::result::Result<T, DeboaError>;

/// Type alias for test results
/// Convenience alias for handling test errors throughout the library.
///
/// # Examples
///
/// ```
/// use deboa::TestResult;
///
/// fn example() -> TestResult<String> {
///     Ok("success".to_string())
/// }
/// ```
pub type TestResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;

/// Type alias for Result<T, DeboaError>
///
/// This is a convenience alias for handling Deboa errors throughout the library.
///
/// # Examples
///
/// ```
/// use deboa::DeboaResult;
///
/// fn example() -> DeboaResult<String> {
///     Ok("success".to_string())
/// }
/// ```
///
/// # See Also
/// - [DeboaError](crate::errors::DeboaError)
pub type DeboaResult<T> = Result<T>;

/// HTTP client trait
pub trait HttpClient {
    /// Execute a request
    ///
    /// # Argument
    ///
    /// - `request` - Request to be sent to server
    ///
    /// # Returns
    ///
    /// - `Result<DeboaResponse>` - A result containing response
    ///
    fn execute<R>(&self, request: R) -> impl Future<Output = Result<DeboaResponse>>
    where
        R: IntoRequest;
}

/// Client parameters struct
pub struct ClientBuilder<I, C, P, R> {
    inner: InnerClient<I, C, P, R>,
}

impl<I, C, P, R> ClientBuilder<I, C, P, R>
where
    I: Identity + Send + Clone + 'static,
    C: Certificate + Send + Clone + 'static,
    P: HttpConnectionPool<Identity = I, Certificate = C> + Default + Send + 'static,
    R: DnsResolver + Default + Send + 'static,
{
    /// Set skip certificate verification
    ///
    /// # Arguments
    ///
    /// - `skip` - True to skip server certificate verification, false otherwise
    ///
    pub fn skip_cert_verification(mut self, skip: bool) -> Self {
        self.inner
            .skip_cert_verification = skip;
        self
    }

    /// Allow set connection timeout
    ///
    /// # Arguments
    ///
    /// - `connection_timeout` - Duration for connection timeout
    ///
    pub fn connection_timeout(mut self, connection_timeout: Duration) -> Self {
        self.inner
            .connection_timeout = connection_timeout;
        self
    }

    /// Allow set request timeout
    ///
    /// # Arguments
    ///
    /// - `request_timeout` - Duration for request timeout
    ///
    pub fn request_timeout(mut self, request_timeout: Duration) -> Self {
        self.inner
            .request_timeout = request_timeout;
        self
    }

    /// Allow set certificate
    ///
    /// # Arguments
    ///
    /// - `certificate` - Custom CA certificate
    ///
    pub fn certificate(mut self, certificate: C) -> Self {
        self.inner
            .certificate = Some(certificate);
        self
    }

    /// Allow set identity
    ///
    /// # Arguments
    ///
    /// - `identity` - A client certificate to be used for mTLS
    ///
    pub fn identity(mut self, identity: I) -> Self {
        self.inner.identity = Some(identity);
        self
    }

    /// Allow set client bind address
    ///
    /// # Arguments
    ///
    /// - `dns_resolver` - A DnsResolver trait implementation
    ///
    pub fn bind_addr(mut self, bind_addr: IpAddr) -> Self {
        self.inner.bind_addr = bind_addr;
        self
    }

    /// Allow set dns resolver
    ///
    /// # Arguments
    ///
    /// - `dns_resolver` - A DnsResolver trait implementation
    ///
    pub fn dns_resolver(mut self, dns_resolver: R) -> Self {
        self.inner
            .dns_resolver = dns_resolver;
        self
    }

    /// Set connction pool
    pub fn connection_pool(mut self, pool: P) -> Self {
        self.inner.pool = RwLock::new(pool);
        self
    }

    /// Allow set supported protocols
    ///
    /// # Arguments
    ///
    /// - `protos` - List of protocols supported by client
    ///
    /// # Notes
    ///
    /// This setting allow to specify which protocol versions
    /// are supported by client while doing ALPN during TLS handshake.
    ///
    pub fn protos(mut self, protos: Vec<Version>) -> Self {
        self.inner.protos = protos;
        self
    }

    /// Set prior knowledge flag
    ///
    /// # Arguments
    ///
    /// - `prior_knowledge` - A bool value indicating about prior knowledge
    ///
    /// # Notes
    ///
    /// This setting tells HTTP client to create connection
    /// using protocol specified on request, set it to false
    /// if you want protocol discovery using ALPN/Alt Svc.
    ///
    pub fn prior_knowledge(mut self, prior_knowledge: bool) -> Self {
        self.inner
            .prior_knowledge = prior_knowledge;
        self
    }

    /// Build the client
    pub fn build(self) -> Client<InnerClient<I, C, P, R>> {
        Client::from_inner(self.inner)
    }
}

/// Client struct
pub struct Client<H> {
    hook: H,
}

impl<H> Client<H>
where
    H: Hook<DeboaRequest, DeboaResponse, Result = Result<DeboaResponse>> + 'static,
{
    /// Initialize a client from hook
    ///
    /// # Arguments
    ///
    /// - `inner` - A existing hook
    ///
    /// # Returns
    ///
    /// - `Self` - New client
    ///
    pub fn new(inner: H) -> Self {
        Self { hook: inner }
    }

    /// Add a new hook to the chain
    ///
    /// # Arguments
    ///
    /// - `chain` - A existing chain to add
    ///
    /// # Returns
    ///
    /// - `Client<Hout>` - A client with hook in
    ///
    pub fn chain<C, Hout>(self, chain: C) -> Client<Hout>
    where
        C: Chain<H, DeboaError, DeboaRequest, DeboaResponse, Hook = Hout>,
        Hout: Hook<DeboaRequest, DeboaResponse, Result = Result<DeboaResponse>> + 'static,
    {
        Client::new(chain.chain(self.hook))
    }

    /// Add a hook from a function
    ///
    /// # Arguments
    ///
    /// - `f` - A function/closure which contains hook logic
    ///
    /// # Returns
    ///
    /// - `Client<Hout>` - A client with hook in
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use deboa::{Result, request::DeboaRequest, response::DeboaResponse};
    /// use deboa_tokio::Client;
    /// use tackle::{Chain, Hook, NextHook};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<()> {
    ///   // Create a new client with default settings
    ///   let client = Client::builder()
    ///     .connection_timeout(10)  // 10 seconds
    ///     .request_timeout(30)     // 30 seconds
    ///     .build();
    ///
    ///   let client = client
    ///     .chain_fn(|req, next| async move {
    ///        println!("Request 1: {:?}", req);
    ///        next.call(req).await
    ///     });
    ///
    ///   let request = DeboaRequest::get("https://example.com/posts/1")?.build()?;
    ///   let response: DeboaResponse = client
    ///     .execute(request)
    ///     .await?;
    ///
    ///   Ok(())
    /// }
    /// ```
    pub fn chain_fn<F, Fut>(self, f: F) -> Client<HookFn<F, H>>
    where
        F: Fn(DeboaRequest, std::rc::Rc<H>) -> Fut + Send,
        Fut: Future<Output = Result<DeboaResponse>>,
    {
        Client::from_fn(HookFn::new(self.hook, f))
    }
}

impl<F, H> Client<HookFn<F, H>> {
    /// Initialize a client from hook
    ///
    /// # Arguments
    ///
    /// - `inner` - HookFn<F, H> - A existing hook
    ///
    /// # Returns
    ///
    /// - `Self` - A new client
    pub fn from_fn(inner: HookFn<F, H>) -> Self {
        Self { hook: inner }
    }
}

impl<I, C, P, R> Client<InnerClient<I, C, P, R>>
where
    I: Identity + Send + Clone,
    C: Certificate + Send + Clone,
    P: HttpConnectionPool + Default + Send,
    R: DnsResolver + Default + Send,
{
    /// Create a client from inner client
    ///
    /// # Arguments
    ///
    /// - `inner` - A inner client
    ///
    /// # Returns
    ///
    /// - `Self` - A new client instance
    ///
    pub fn from_inner(inner: InnerClient<I, C, P, R>) -> Self {
        Self { hook: inner }
    }

    /// Returns a new builer
    ///
    /// # Returns
    ///
    /// - `ClientBuilder<I, C, P. R>` - A new client builder
    ///
    pub fn builder() -> ClientBuilder<I, C, P, R> {
        ClientBuilder { inner: InnerClient::<I, C, P, R>::default() }
    }
}

///
/// Extension trait for Client to enable the `<<` operator for URL construction.
/// This allows for a more ergonomic way to create requests using the `<<` operator.
/// The operator creates a GET request with the provided URL.
///
/// # Examples
///
/// ``` rust,ignore
/// use deboa::{Client, Result};
/// use deboa_tokio::InnerClient;
///
/// #[tokio::main]
/// fn main() -> Result<()> {
///     let client = Client::<InnerClient>::default();
///     let request = &client << "https://httpbin.org/get";
///     // do something with the request
///     Ok(())
/// }
/// ```
///
/// # Notes
/// - This implementation is primarily for convenience and ergonomics
/// - For more complex request configurations, use the full DeboaRequest API
/// - The `<<` operator is a shorthand for creating GET requests
impl<H> Shl<&str> for &Client<H> {
    type Output = DeboaRequestBuilder;

    fn shl(self, other: &str) -> Self::Output {
        DeboaRequest::get(other).expect("Invalid URL!")
    }
}

impl<H> Default for Client<H>
where
    H: Hook<DeboaRequest, DeboaResponse> + Default,
{
    fn default() -> Self {
        Self { hook: H::default() }
    }
}

impl<H> HttpClient for Client<H>
where
    H: Hook<DeboaRequest, DeboaResponse, Result = Result<DeboaResponse>>,
{
    async fn execute<Req>(&self, request: Req) -> Result<DeboaResponse>
    where
        Req: IntoRequest,
    {
        self.hook
            .call(request.into_request()?)
            .await
    }
}

/// The main HTTP client for making requests.
///
/// `Deboa` is a flexible and efficient HTTP client that supports both synchronous
/// and asynchronous operations. It provides a builder pattern for configuration
/// and supports features like connection pooling, timeouts, and custom error handling.
///
/// # Features
///
/// - Connection pooling for better performance
/// - Configurable timeouts
/// - Support for multiple HTTP protocols (HTTP/1.1, HTTP/2 and HTTP/3)
/// - Thread-safe and `Send` + `Sync`
/// - Prior knowlege support
///
/// # Examples
///
/// ## Basic Usage
///
/// ``` ignore
/// use deboa::{Result};
/// use deboa_tokio::Client;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///   // Create a new client with default settings
///   let client = Client::default();
///
///   // Or configure with custom settings
///   let client = Client::builder()
///     .connection_timeout(10)  // 10 seconds
///     .request_timeout(30)     // 30 seconds
///     .build();
///   Ok(())
/// }
/// ```
///
/// # Thread Safety
///
/// `Deboa` implements `Send` and `Sync`, making it safe to share between threads.
/// The connection pool is managed internally and optimized for concurrent access.
///
/// # Performance
///
/// - Connection pooling reduces latency for repeated requests to the same host
/// - Automatic connection reuse when possible
/// - Configurable timeouts prevent hanging requests
pub struct InnerClient<I, C, P, R> {
    connection_timeout: Duration,
    request_timeout: Duration,
    identity: Option<I>,
    certificate: Option<C>,
    skip_cert_verification: bool,
    pool: RwLock<P>,
    dns_resolver: R,
    bind_addr: IpAddr,
    protos: Vec<Version>,
    prior_knowledge: bool,
}

impl<I, C, P, R> InnerClient<I, C, P, R> {
    #[inline]
    /// Check if certificate verification is skipped.
    ///
    /// # Returns
    ///
    /// * `bool` - `true` if certificate verification is skipped, `false` otherwise.
    pub fn skip_cert_verification(&self) -> bool {
        self.skip_cert_verification
    }

    #[inline]
    /// Allow get request connection timeout at any time.
    ///
    /// # Returns
    ///
    /// * `Duration` - The connection timeout.
    ///
    pub fn connection_timeout(&self) -> Duration {
        self.connection_timeout
    }

    #[inline]
    /// Allow get request request timeout at any time.
    ///
    /// # Returns
    ///
    /// * `Duration` - The request timeout.
    ///
    pub fn request_timeout(&self) -> Duration {
        self.request_timeout
    }

    /// Allow get connection pool at any time.
    ///
    /// # Returns
    ///
    /// * `Option<std::cell::Ref<'_, HttpConnectionPool>>` - The connection pool.
    ///
    #[inline]
    pub fn connection_pool(&self) -> &RwLock<P> {
        &self.pool
    }

    /// Allow get DNS resolver at any time.
    ///
    /// # Returns
    ///
    /// * `Arc<dyn DnsResolver>` - The DNS resolver.
    ///
    #[inline]
    pub fn dns_resolver(&self) -> &R {
        &self.dns_resolver
    }

    /// Allow get bind address at any time.
    ///
    /// # Returns
    ///
    /// * `IpAddr` - The bind address.
    ///
    #[inline]
    pub fn bind_addr(&self) -> IpAddr {
        self.bind_addr
    }

    /// Allow get certificate at any time.
    ///
    /// # Returns
    ///
    /// * `Option<Identity>` - The certificate.
    ///
    pub fn certificate(&self) -> &Option<C> {
        &self.certificate
    }

    /// Allow get identity at any time.
    ///
    /// # Returns
    ///
    /// * `Option<Identity>` - The identity.
    ///
    #[inline]
    pub fn identity(&self) -> &Option<I> {
        &self.identity
    }

    /// Allow get supported protocols
    ///
    /// # Returns
    ///
    /// * `Vec<Verstion>` - A vector of supported protocols
    ///
    #[inline]
    pub fn protos(&self) -> &Vec<Version> {
        &self.protos
    }

    /// Allow get prior knowledge
    ///
    /// # Returns
    ///
    /// * `bool` - A request version implies prior knowledge
    ///
    #[inline]
    pub fn prior_knowledge(&self) -> bool {
        self.prior_knowledge
    }
}

impl<I, C, P, R> Default for InnerClient<I, C, P, R>
where
    I: Identity + Send + Clone,
    C: Certificate + Send + Clone,
    P: HttpConnectionPool + Default + Send,
    R: DnsResolver + Default + Send,
{
    fn default() -> Self {
        Self {
            bind_addr: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
            connection_timeout: Duration::from_secs(30),
            request_timeout: Duration::from_secs(30),
            identity: None,
            certificate: None,
            skip_cert_verification: false,
            pool: RwLock::new(P::default()),
            dns_resolver: R::default(),
            protos: vec![Version::HTTP_11],
            prior_knowledge: false,
        }
    }
}

impl<I, C, P, R> Hook<DeboaRequest, DeboaResponse> for InnerClient<I, C, P, R>
where
    I: Identity + Send + Clone,
    C: Certificate + Send + Clone,
    P: HttpConnectionPool<Identity = I, Certificate = C> + Send,
    R: DnsResolver + Send,
{
    type Result = Result<DeboaResponse>;
    type Error = DeboaError;

    async fn call(&self, request: DeboaRequest) -> Result<DeboaResponse> {
        info!("Building request: {} {}", request.method(), request.uri());

        let uri = request
            .uri()
            .clone();

        let Some(scheme) = uri.scheme_str() else {
            return Err(DeboaError::Request(RequestError::Send {
                message: "Missing scheme".to_string(),
            }));
        };

        let Some(host) = uri.host() else {
            return Err(DeboaError::Request(RequestError::Send {
                message: "Missing host".to_string(),
            }));
        };

        let port = uri
            .port_u16()
            .unwrap_or({
                match scheme {
                    "http" | "ws" => 80,
                    "https" | "wss" => 443,
                    _ => 80,
                }
            });

        let mut config = ConnectionConfig::builder()
            .scheme(scheme)
            .host(host)
            .port(port)
            .protocol_version(request.version())
            .identity(
                self.identity
                    .as_ref(),
            )
            .certificate(
                self.certificate
                    .as_ref(),
            )
            .prior_knowledge(self.prior_knowledge)
            .skip_cert_verification(self.skip_cert_verification)
            .client_bind_addr(self.bind_addr)
            .build();

        let mut pool = self
            .pool
            .write()
            .await;

        let conn = pool
            .create_connection(&mut config, &self.dns_resolver)
            .await?;

        let request = request.body();

        let response = conn
            .send_request(request, self.request_timeout)
            .await?;

        Ok(response)
    }
}