deboa 0.0.9

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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
//! # Deboa - Core API Documentation
//!
//! Hello, and welcome to the core Deboa API documentation!
//!
//! This API documentation is highly technical and is purely a reference.
//!
//! Depend on `deboa` in `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! deboa = "0.0.8"
//! ```
//!
//! <small>Note that development versions, tagged with `-dev`, are not published
//! and need to be specified as [git dependencies].</small>
//!
//! ```rust,no_run
//! use deboa::{Deboa, Result, errors::DeboaError, request::DeboaRequest};
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//!     let mut deboa = Deboa::builder()
//!         .build();
//!
//!     let response = DeboaRequest::get("https://httpbin.org/get")?
//!         .send_with(&mut deboa)
//!         .await?;
//!
//!     println!("Response: {:#?}", response);
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Features
//!
//! To avoid compiling unused dependencies, Deboa feature-gates optional
//! functionality, some enabled by default:
//!
//! | Feature         | Default? | Description                                             |
//! |-----------------|----------|---------------------------------------------------------|
//! | `tokio_rt`      | Yes      | Support tokio runtime (enabled by default).             |
//! | `smol_rt`       | No       | Support smol runtime.                                   |
//! | `http1`         | Yes      | Support for HTTP/1 (enabled by default).                |
//! | `http2`         | Yes      | Support for HTTP/2 (enabled by default).                |
//!
//! Disabled features can be selectively enabled in `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! deboa = { version = "0.0.8", features = ["tokio_rt", "http1", "http2"] }
//! ```
//!
//! Conversely, HTTP/2 can be disabled:
//!
//! ```toml
//! [dependencies]
//! deboa = { version = "0.0.8", default-features = false }
//! ```
//!

#[cfg(all(feature = "tokio-rt", feature = "smol-rt"))]
compile_error!("Only one runtime feature can be enabled at a time.");

#[cfg(not(any(feature = "http1", feature = "http2")))]
compile_error!("At least one HTTP version feature must be enabled.");

pub(crate) const MAX_ERROR_MESSAGE_SIZE: usize = 50000;

use std::fmt::{Debug, Display};

use std::ops::Shl;

use bytes::Bytes;
use http::{header, HeaderValue, Request, Response};
use http_body_util::Full;
use hyper::body::Incoming;

use crate::cert::{ClientCert, Identity};
use crate::client::conn::http::{DeboaConnection, DeboaHttpConnection};

use crate::catcher::DeboaCatcher;
use crate::client::conn::pool::{DeboaHttpConnectionPool, HttpConnectionPool};
use crate::errors::DeboaError;
use crate::request::{DeboaRequest, IntoRequest};
use crate::response::{DeboaResponse, IntoBody};
use crate::url::IntoUrl;

pub use async_trait::async_trait;

pub mod cache;
pub mod catcher;
pub mod cert;
pub mod client;
pub mod cookie;
pub mod errors;
pub mod form;
pub mod fs;
pub mod request;
pub mod response;
pub mod rt;
pub mod url;

#[cfg(test)]
mod tests;

pub type Result<T> = std::result::Result<T, DeboaError>;

impl Shl<&str> for &Deboa {
    type Output = DeboaRequest;

    fn shl(self, other: &str) -> Self::Output {
        DeboaRequest::get(other)
            .expect("Invalid url!")
            .build()
            .expect("Could not build request!")
    }
}

#[derive(PartialEq, Debug)]
/// Enum that represents the HTTP version.
///
/// # Variants
///
/// * `Http1` - The HTTP/1.1 version.
/// * `Http2` - The HTTP/2 version.
pub enum HttpVersion {
    #[cfg(feature = "http1")]
    Http1,
    #[cfg(feature = "http2")]
    Http2,
    #[cfg(feature = "http3")]
    Http3,
}

impl Display for HttpVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            #[cfg(feature = "http1")]
            HttpVersion::Http1 => write!(f, "HTTP/1.1"),
            #[cfg(feature = "http2")]
            HttpVersion::Http2 => write!(f, "HTTP/2"),
            #[cfg(feature = "http3")]
            HttpVersion::Http3 => write!(f, "HTTP/3"),
        }
    }
}

/// A builder for configuring and creating a new `Deboa` client instance.
///
/// This builder allows you to configure various aspects of the HTTP client before
/// constructing it. You can set timeouts, configure protocols, add error handlers,
/// and more.
///
/// # Examples
///
/// ``` rust, no_run
/// use deboa::{Deboa, HttpVersion};
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///   let client = deboa::Deboa::builder()
///     .connection_timeout(30)  // 30 seconds
///     .request_timeout(10)     // 10 seconds
///     .protocol(HttpVersion::Http2)  // Use HTTP/2
///     .build();
///
///   // Use the client to make requests...
///   Ok(())
/// }
/// ```
///
/// # Default Configuration
///
/// - Connection timeout: 30 seconds
/// - Request timeout: 30 seconds
/// - Protocol: HTTP/1.1
/// - No client certificates
/// - No custom error catchers
pub struct DeboaBuilder {
    connection_timeout: u64,
    request_timeout: u64,
    client_cert: Option<ClientCert>,
    identity: Option<Identity>,
    catchers: Option<Vec<Box<dyn DeboaCatcher>>>,
    protocol: HttpVersion,
}

impl DeboaBuilder {
    /// Sets the maximum duration to wait when connecting to a server.
    ///
    /// This timeout affects the initial TCP connection establishment. If the server
    /// doesn't respond within this duration, the connection will fail with a timeout error.
    ///
    /// # Arguments
    ///
    /// * `connection_timeout` - The timeout in seconds.
    ///
    /// # Examples
    ///
    /// ``` rust, no_run
    /// use deboa::Deboa;
    /// let builder = Deboa::builder()
    ///     .connection_timeout(10);  // 10 seconds
    /// ```
    ///
    /// # Note
    /// A value of 0 means no timeout (not recommended in production).
    pub fn connection_timeout(mut self, connection_timeout: u64) -> Self {
        self.connection_timeout = connection_timeout;
        self
    }

    /// Sets the maximum duration for the entire HTTP request/response cycle.
    ///
    /// This includes connection time, request writing, server processing, and response reading.
    /// If the entire operation takes longer than this duration, it will be aborted.
    ///
    /// # Arguments
    ///
    /// * `request_timeout` - The timeout in seconds.
    ///
    /// # Examples
    ///
    /// ``` rust, no_run
    /// use deboa::Deboa;
    /// let builder = Deboa::builder()
    ///     .request_timeout(30);  // 30 seconds
    /// ```
    ///
    /// # Note
    /// A value of 0 means no timeout (not recommended in production).
    pub fn request_timeout(mut self, request_timeout: u64) -> Self {
        self.request_timeout = request_timeout;
        self
    }

    /// Configures a client certificate for mutual TLS authentication.
    ///
    /// This is used when the server requires client certificate authentication.
    /// The certificate should be in PEM format and include both the certificate
    /// and private key.
    ///
    /// # Arguments
    ///
    /// * `client_cert` - The client certificate configuration.
    ///
    /// # Examples
    ///
    /// ``` compile_fail
    /// use deboa::{Deboa, ClientCert};
    ///
    /// #[tokio::main]
    ///
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let cert = ClientCert::from_pem_file("client.pem")?;
    ///     let builder = Deboa::builder()
    ///         .client_cert(cert);
    ///     Ok(())
    /// }
    /// ```
    pub fn client_cert(mut self, client_cert: ClientCert) -> Self {
        self.client_cert = Some(client_cert);
        self
    }

    /// Adds an error handler for specific types of errors.
    ///
    /// Catchers are called when an error occurs during request execution.
    /// They can be used to implement custom error handling logic, such as
    /// automatic retries, logging, or error transformation.
    ///
    /// # Arguments
    ///
    /// * `catcher` - A function or closure that handles specific error types.
    ///
    /// # Examples
    ///
    /// ## Basic Error Logging
    ///
    /// ``` compile_fail
    /// use deboa::Deboa;
    /// use std::error::Error;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn Error>> {
    ///     let builder = Deboa::builder()
    ///         .catch(|e: std::io::Error| {
    ///             eprintln!("Network error: {}", e);
    ///             Ok(())  // Continue execution
    ///         });
    ///     Ok(())
    /// }
    /// ```
    ///
    /// ## Automatic Retries
    ///
    /// ``` compile_fail
    /// use deboa::Deboa;
    /// use std::error::Error;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn Error>> {
    ///     let builder = Deboa::builder()
    ///         .catch(|e: std::io::Error| {
    ///             if e.kind() == std::io::ErrorKind::TimedOut {
    ///                 eprintln!("Request timed out, will retry...");
    ///                 // Return error to trigger retry logic
    ///             Err(Box::new(e))
    ///         } else {
    ///             // For other errors, continue with the error
    ///             Ok(())
    ///         }
    ///     });
    ///     Ok(())
    /// }
    /// ```
    ///
    /// # Notes
    /// - Multiple catchers can be added for different error types
    /// - Catchers are called in the order they are added
    /// - If a catcher returns `Ok(())`, error handling continues to the next catcher
    /// - If a catcher returns `Err(e)`, error propagation stops and the error is returned
    /// - The default error handler will be called if no catcher handles the error
    pub fn catch<C: DeboaCatcher>(mut self, catcher: C) -> Self {
        if let Some(catchers) = &mut self.catchers {
            catchers.push(Box::new(catcher));
        } else {
            self.catchers = Some(vec![Box::new(catcher)]);
        }
        self
    }

    /// Sets the HTTP protocol version to use for requests.
    ///
    /// By default, the client will use HTTP/1.1. You can choose to use HTTP/2
    /// for better performance, especially for multiple requests to the same server.
    ///
    /// # Arguments
    ///
    /// * `protocol` - The HTTP protocol version to use.
    ///
    /// # Examples
    ///
    /// ``` rust, no_run
    /// use deboa::{Deboa, HttpVersion};
    ///
    /// let builder = Deboa::builder()
    ///     .protocol(HttpVersion::Http2);  // Use HTTP/2
    /// ```
    ///
    /// # Note
    /// The actual protocol version used may be negotiated with the server
    /// during the TLS handshake.
    pub fn protocol(mut self, protocol: HttpVersion) -> Self {
        self.protocol = protocol;
        self
    }

    /// Constructs a new `Deboa` client with the configured settings.
    ///
    /// This consumes the builder and returns a new `Deboa` instance that can
    /// be used to make HTTP requests.
    ///
    /// # Returns
    ///
    /// A new `Deboa` client instance.
    ///
    /// # Examples
    ///
    /// ``` rust, no_run
    /// use deboa::Deboa;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///   let client = Deboa::builder()
    ///     .connection_timeout(10)
    ///     .request_timeout(30)
    ///     .build();
    ///
    ///   // client is now ready to make requests
    ///   Ok(())
    /// }
    /// ```
    ///
    /// # Panics
    ///
    /// This method may panic if the underlying HTTP client cannot be created
    /// with the specified configuration.
    pub fn build(self) -> Deboa {
        Deboa {
            connection_timeout: self.connection_timeout,
            request_timeout: self.request_timeout,
            client_cert: self.client_cert,
            identity: self.identity,
            catchers: self.catchers,
            protocol: self.protocol,
            pool: HttpConnectionPool::new(),
        }
    }
}

/// 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
/// - Custom error handling with catchers
/// - Support for multiple HTTP protocols (HTTP/1.1, HTTP/2)
/// - Thread-safe and `Send` + `Sync`
///
/// # Examples
///
/// ## Basic Usage
///
/// ``` rust,no_run
/// use deboa::Deboa;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///   // Create a new client with default settings
///   let client = Deboa::new();
///
///   // Or configure with custom settings
///   let client = Deboa::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 Deboa {
    connection_timeout: u64,
    request_timeout: u64,
    client_cert: Option<ClientCert>,
    identity: Option<Identity>,
    catchers: Option<Vec<Box<dyn DeboaCatcher>>>,
    protocol: HttpVersion,
    pool: HttpConnectionPool,
}

impl AsRef<Deboa> for Deboa {
    fn as_ref(&self) -> &Deboa {
        self
    }
}

impl AsMut<Deboa> for Deboa {
    fn as_mut(&mut self) -> &mut Deboa {
        self
    }
}

impl Debug for Deboa {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Deboa")
            .field("connection_timeout", &self.connection_timeout)
            .field("request_timeout", &self.request_timeout)
            .field("protocol", &self.protocol)
            .finish()
    }
}

impl Deboa {
    /// Creates a new `Deboa` instance with default settings.
    ///
    /// This is equivalent to calling `Deboa::builder().build()` and provides
    /// a quick way to get started with sensible defaults.
    ///
    /// # Default Configuration
    ///
    /// - Connection timeout: 30 seconds
    /// - Request timeout: 30 seconds
    /// - Protocol: HTTP/1.1
    /// - No client certificates
    /// - No custom error catchers
    ///
    /// # Returns
    ///
    /// A new `Deboa` instance with default settings.
    ///
    /// # Examples
    ///
    /// ``` rust,no_run
    /// use deboa::Deboa;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///   let client = Deboa::new();
    ///   // client is ready to make requests
    ///   Ok(())
    /// }
    /// ```
    ///
    /// # See Also
    ///
    /// - [`Deboa::builder()`] for custom configuration
    /// - [`Deboa::default()`] for the same functionality via the `Default` trait
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Deboa {
            connection_timeout: 0,
            request_timeout: 0,
            client_cert: None,
            identity: None,
            catchers: None,
            protocol: HttpVersion::Http1,
            pool: HttpConnectionPool::new(),
        }
    }

    /// Allow create a new Deboa instance.
    ///
    /// # Returns
    ///
    /// * `DeboaBuilder` - The new DeboaBuilder instance.
    ///
    pub fn builder() -> DeboaBuilder {
        DeboaBuilder {
            connection_timeout: 0,
            request_timeout: 0,
            client_cert: None,
            identity: None,
            catchers: None,
            protocol: HttpVersion::Http1,
        }
    }

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

    /// Allow change protocol at any time.
    ///
    /// # Arguments
    ///
    /// * `protocol` - The protocol to be used.
    ///
    /// # Returns
    ///
    /// * `&mut Self` - The Deboa instance.
    ///
    pub fn set_protocol(&mut self, protocol: HttpVersion) -> &mut Self {
        self.protocol = protocol;
        self
    }

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

    /// Allow change request connection timeout at any time.
    ///
    /// # Arguments
    ///
    /// * `timeout` - The new timeout.
    ///
    /// # Returns
    ///
    /// * `&mut Self` - The Deboa instance.
    ///
    pub fn set_connection_timeout(&mut self, timeout: u64) -> &mut Self {
        self.connection_timeout = timeout;
        self
    }

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

    /// Allow change request request timeout at any time.
    ///
    /// # Arguments
    ///
    /// * `timeout` - The new timeout.
    ///
    /// # Returns
    ///
    /// * `&mut Self` - The Deboa instance.
    ///
    pub fn set_request_timeout(&mut self, timeout: u64) -> &mut Self {
        self.request_timeout = timeout;
        self
    }

    /// Allow get client certificate at any time.
    ///
    /// # Returns
    ///
    /// * `Option<ClientCert>` - The client certificate.
    ///
    #[inline]
    #[deprecated(note = "Use identity instead", since = "0.0.8")]
    pub fn client_cert(&self) -> Option<&ClientCert> {
        self.client_cert
            .as_ref()
    }

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

    /// Allow change client certificate at any time.
    ///
    /// # Arguments
    ///
    /// * `client_cert` - The client certificate to be used.
    ///
    /// # Returns
    ///
    /// * `&mut Self` - The Deboa instance.
    ///
    #[deprecated(note = "Use set_identity instead", since = "0.0.8")]
    pub fn set_client_cert(&mut self, client_cert: Option<ClientCert>) -> &mut Self {
        self.client_cert = client_cert;
        self
    }

    /// Allow change identity at any time.
    ///
    /// # Arguments
    ///
    /// * `identity` - The identity to be used.
    ///
    /// # Returns
    ///
    /// * `&mut Self` - The Deboa instance.
    ///
    pub fn set_identity(&mut self, identity: Option<Identity>) -> &mut Self {
        self.identity = identity;
        self
    }

    /// Adds an error handler for specific types of errors.
    ///
    /// Catchers are called when an error occurs during request execution.
    /// They can be used to implement custom error handling logic, such as
    /// automatic retries, logging, or error transformation.
    ///
    /// # Arguments
    ///
    /// * `catcher` - A function or closure that handles specific error types.
    ///
    /// # Examples
    ///
    /// ## Basic Error Logging
    ///
    /// ```compile_fail
    /// use deboa::Deboa;
    /// use std::error::Error;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn Error>> {
    /// let builder = Deboa::builder()
    ///     .catch(|e: std::io::Error| {
    ///         eprintln!("Network error: {}", e);
    ///         Ok(())  // Continue execution
    ///     });
    /// # Ok(()) }
    /// ```
    ///
    /// ## Automatic Retries
    ///
    /// ```compile_fail
    /// use deboa::Deboa;
    /// use std::error::Error;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn Error>> {
    /// let builder = Deboa::builder()
    ///     .catch(|e: std::io::Error| {
    ///         if e.kind() == std::io::ErrorKind::TimedOut {
    ///             eprintln!("Request timed out, will retry...");
    ///             // Return error to trigger retry logic
    ///             Err(Box::new(e))
    ///         } else {
    ///             // For other errors, continue with the error
    ///             Ok(())
    ///         }
    ///     });
    /// # Ok(()) }
    /// ```
    ///
    /// # Notes
    /// - Multiple catchers can be added for different error types
    /// - Catchers are called in the order they are added
    /// - If a catcher returns `Ok(())`, error handling continues to the next catcher
    /// - If a catcher returns `Err(e)`, error propagation stops and the error is returned
    /// - The default error handler will be called if no catcher handles the error
    /// # Returns
    ///
    /// * `&mut Self` - The Deboa instance.
    ///
    pub fn catch<C: DeboaCatcher>(&mut self, catcher: C) -> &mut Self {
        if let Some(catchers) = &mut self.catchers {
            catchers.push(Box::new(catcher));
        } else {
            self.catchers = Some(vec![Box::new(catcher)]);
        }
        self
    }

    /// Executes an HTTP request and returns the response.
    ///
    /// This is the primary method for making HTTP requests. It handles the entire
    /// request/response lifecycle, including retries, error handling, and response processing.
    ///
    /// # Arguments
    ///
    /// * `request` - The request to execute. This can be:
    ///   - A string URL (for GET requests)
    ///   - A `DeboaRequest` instance (for more control)
    ///   - Any type that implements `IntoRequest`
    ///
    /// # Returns
    ///
    /// A `Result` containing either:
    /// - `Ok(DeboaResponse)` - The successful HTTP response
    /// - `Err(DeboaError)` - If the request fails or encounters an error
    ///
    /// # Examples
    ///
    /// ## Simple GET Request
    ///
    /// ```rust,no_run
    /// use deboa::Deboa;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///   let mut client = Deboa::new();
    ///   let response = client.execute("https://httpbin.org/get").await?;
    ///   println!("Status: {}", response.status());
    ///   println!("Body: {}", response.text().await?);
    ///   Ok(())
    /// }
    /// ```
    ///
    /// ## POST Request with JSON Body
    ///
    /// ```compile_fail
    /// use deboa::{Deboa, request::post};
    /// use serde_json::json;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///   let mut client = Deboa::new();
    ///   let response = client
    ///     .execute(
    ///         post("https://httpbin.org/post")
    ///             .text("text")?
    ///     )
    ///     .await?;
    ///   Ok(())
    /// }
    /// ```
    ///
    /// # Error Handling
    ///
    /// The method will automatically handle:
    /// - Network errors
    /// - Timeouts (if configured)
    /// - Invalid responses
    /// - Status code errors (unless configured otherwise)
    ///
    /// # Retries
    ///
    /// By default, failed requests are not automatically retried. To enable retries:
    ///
    /// ```compile_fail
    /// use deboa::{Deboa, request::get};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///   let mut client = Deboa::new();
    ///   let request = get("https://example.com").retries(3); // Retry up to 3 times
    ///   let response = client.execute(request).await?;
    ///   Ok(())
    /// }
    /// ```
    ///
    /// # Panics
    /// - If the request is invalid
    /// - If the response is a non-success status code
    ///
    /// # Performance
    ///
    /// - Uses connection pooling for better performance
    /// - Automatically reuses connections when possible
    /// - Supports HTTP/1.1 and HTTP/2
    pub async fn execute<R>(&mut self, request: R) -> Result<DeboaResponse>
    where
        R: IntoRequest,
    {
        let mut request = request.into_request()?;

        if let Some(catchers) = &self.catchers {
            let mut response = None;
            for catcher in catchers {
                response = catcher
                    .on_request(request.as_mut())
                    .await?;
            }

            if let Some(response) = response {
                let mut new_response = response;
                for catcher in catchers {
                    catcher
                        .on_response(new_response.as_mut())
                        .await?;
                }
                return Ok(new_response);
            }
        }

        let mut retry_count: u32 = 0;
        let response = loop {
            let response = self
                .send_request(request.as_mut())
                .await;
            if let Err(err) = response {
                if retry_count == request.retries() {
                    break Err(err);
                }
                #[cfg(feature = "tokio-rt")]
                tokio::time::sleep(tokio::time::Duration::from_secs(2_u32.pow(retry_count) as u64))
                    .await;
                #[cfg(feature = "smol-rt")]
                smol::Timer::after(std::time::Duration::from_secs(2_u32.pow(retry_count) as u64))
                    .await;
                retry_count += 1;
                continue;
            }

            let response = response.unwrap();

            if response
                .status()
                .is_redirection()
            {
                let location = response
                    .headers()
                    .get(header::LOCATION);
                if let Some(location) = location {
                    let location = location
                        .to_str()
                        .unwrap();
                    let result = request
                        .as_mut()
                        .set_url(location);
                    if let Err(err) = result {
                        break Err(err);
                    }
                }
                continue;
            }

            break Ok(response);
        };

        let res_url = request
            .url()
            .to_string();
        let mut response = self
            .process_response(res_url, response?)
            .await?;

        if let Some(catchers) = &self.catchers {
            for catcher in catchers {
                catcher
                    .on_response(response.as_mut())
                    .await?;
            }
        }

        Ok(response)
    }

    /// Allow send a request.
    ///
    /// # Arguments
    ///
    /// * `request` - The request to be sent.
    ///
    /// # Returns
    ///
    /// * `Result<Response<Incoming>>` - The response.
    ///
    async fn send_request<R>(&mut self, request: &R) -> Result<Response<Incoming>>
    where
        R: AsRef<DeboaRequest>,
    {
        let url = request
            .as_ref()
            .url();
        let mut uri = url
            .path()
            .to_string();
        if let Some(query) = url.query() {
            uri.push('?');
            uri.push_str(query);
        }

        let method = request
            .as_ref()
            .method();

        let mut builder = Request::builder()
            .uri(uri)
            .method(
                method
                    .to_string()
                    .as_str(),
            );
        {
            let req_headers = builder
                .headers_mut()
                .unwrap();

            request
                .as_ref()
                .headers()
                .into_iter()
                .fold(&mut *req_headers, |acc, (key, value)| {
                    acc.insert(key, value.into());
                    acc
                });

            if let Some(deboa_cookies) = request
                .as_ref()
                .cookies()
            {
                let mut cookies = Vec::<String>::new();

                for cookie in deboa_cookies.values() {
                    cookies.push(cookie.to_string());
                }

                if let Ok(cookie_header) = HeaderValue::from_str(&cookies.join("; ")) {
                    req_headers.insert(header::COOKIE, cookie_header);
                }
            }
        }

        let request = builder.body(Full::new(Bytes::from(
            request
                .as_ref()
                .raw_body()
                .to_vec(),
        )));
        if let Err(err) = request {
            return Err(DeboaError::Request(errors::RequestError::Send {
                url: url.to_string(),
                method: method.to_string(),
                message: err.to_string(),
            }));
        }

        let request = request.unwrap();

        let conn = self
            .pool
            .create_connection(url, &self.protocol, &self.client_cert)
            .await?;
        match conn {
            #[cfg(feature = "http1")]
            DeboaConnection::Http1(ref mut conn) => {
                conn.send_request(request)
                    .await
            }
            #[cfg(feature = "http2")]
            DeboaConnection::Http2(ref mut conn) => {
                conn.send_request(request)
                    .await
            }
        }
    }

    /// Allow process a response.
    ///
    /// # Arguments
    ///
    /// * `response` - The response to be processed.
    ///
    /// # Returns
    ///
    /// * `Result<DeboaResponse>` - The response.
    ///
    async fn process_response<U>(
        &self,
        url: U,
        response: Response<Incoming>,
    ) -> Result<DeboaResponse>
    where
        U: IntoUrl,
    {
        let response = response.map(|body| body.into_body());
        let response = DeboaResponse::new(url.into_url()?, response);
        Ok(response)
    }
}