deboa 0.1.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
#![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 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 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
    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
    pub fn skip_cert_verification(mut self, skip: bool) -> Self {
        self.inner
            .skip_cert_verification = skip;
        self
    }

    /// Set connection timeout
    pub fn connection_timeout(mut self, connection_timeout: Duration) -> Self {
        self.inner
            .connection_timeout = connection_timeout;
        self
    }

    /// Set request timeout
    pub fn request_timeout(mut self, request_timeout: Duration) -> Self {
        self.inner
            .request_timeout = request_timeout;
        self
    }

    /// Set certificate
    pub fn certificate(mut self, certificate: C) -> Self {
        self.inner
            .certificate = Some(certificate);
        self
    }

    /// Set identity
    pub fn identity(mut self, identity: I) -> Self {
        self.inner.identity = Some(identity);
        self
    }

    /// Set client bind address
    pub fn bind_addr(mut self, bind_addr: IpAddr) -> Self {
        self.inner.bind_addr = bind_addr;
        self
    }

    /// Set dns resolver
    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
    }

    /// 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
    pub fn new(inner: H) -> Self {
        Self { hook: inner }
    }

    /// Add a new hook to the chain
    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
    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
    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
    pub fn from_inner(inner: InnerClient<I, C, P, R>) -> Self {
        Self { hook: inner }
    }

    /// Returns a new builer
    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)
/// - Thread-safe and `Send` + `Sync`
///
/// # 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,
}

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 timeout.
    ///
    pub fn connection_timeout(&self) -> Duration {
        self.connection_timeout
    }

    #[inline]
    /// Allow get request request timeout at any time.
    ///
    /// # Returns
    ///
    /// * `Duration` - The 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
    }
}

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(),
        }
    }
}

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" => 80,
                    "https" => 443,
                    _ => 80,
                }
            });

        let config = ConnectionConfig::builder()
            .scheme(scheme)
            .host(host)
            .port(port)
            .protocol_version(request.version())
            .identity(
                self.identity
                    .as_ref(),
            )
            .certificate(
                self.certificate
                    .as_ref(),
            )
            .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(&config, &self.dns_resolver)
            .await?;

        let request = request.body();

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

        Ok(response)
    }
}