Struct HttpClient

Source
pub struct HttpClient { /* private fields */ }
Expand description

Reusable HTTP client configured via HttpClientBuilder.

§Examples

Build a client with a custom timeout and default headers:

use hyper_custom_cert::HttpClient;
use std::time::Duration;
use std::collections::HashMap;

let mut headers = HashMap::new();
headers.insert("x-app".into(), "demo".into());

let client = HttpClient::builder()
    .with_timeout(Duration::from_secs(10))
    .with_default_headers(headers)
    .build();

// Placeholder call; does not perform I/O in this crate.
let _ = client.request("https://example.com");

Implementations§

Source§

impl HttpClient

Source

pub fn new() -> Self

Construct a new client using secure defaults by delegating to the builder.

Source

pub fn builder() -> HttpClientBuilder

Start building a client with explicit configuration.

Examples found in repository?
examples/self-signed-certs/main.rs (line 10)
5fn main() {
6    // Default secure client (uses OS trust store when built with default features)
7    let mut headers = HashMap::new();
8    headers.insert("x-app".into(), "example".into());
9
10    let client = HttpClient::builder()
11        .with_timeout(Duration::from_secs(10))
12        .with_default_headers(headers)
13        .build();
14
15    // Demonstrate a request (no network I/O in this example crate yet)
16    client
17        .request("https://example.com")
18        .expect("request should succeed on native targets");
19
20    // Production with rustls + custom Root CA (e.g., self-signed for your private service)
21    // Note: Requires building with: --no-default-features --features rustls
22    #[cfg(feature = "rustls")]
23    {
24        // Option 1: Load CA certificate from raw PEM bytes
25        let ca_pem: &[u8] =
26            b"-----BEGIN CERTIFICATE-----\n...your root ca...\n-----END CERTIFICATE-----\n";
27        let _rustls_client = HttpClient::builder()
28            .with_timeout(Duration::from_secs(10))
29            .with_root_ca_pem(ca_pem)
30            .build();
31        let _ = _rustls_client.request("https://private.local");
32
33        // Option 2: Load CA certificate from a file path
34        // Note: This will panic if the file doesn't exist - ensure your cert file is available
35        // let _rustls_client_from_file = HttpClient::builder()
36        //     .with_timeout(Duration::from_secs(10))
37        //     .with_root_ca_file("path/to/your/root-ca.pem")
38        //     .build();
39        // let _ = _rustls_client_from_file.request("https://private.local");
40    }
41
42    // Local development only: accept invalid/self-signed certs (dangerous)
43    // Build with: --features insecure-dangerous (or with rustls,insecure-dangerous)
44    #[cfg(feature = "insecure-dangerous")]
45    {
46        // Shortcut:
47        let _dev_client = HttpClient::with_self_signed_certs();
48        let _ = _dev_client.request("https://localhost:8443");
49
50        // Or explicit builder method:
51        let _dev_client2 = HttpClient::builder()
52            .insecure_accept_invalid_certs(true)
53            .build();
54        let _ = _dev_client2.request("https://localhost:8443");
55    }
56
57    println!("Example finished. See README for feature flags and commands.");
58}
Source

pub fn with_self_signed_certs() -> Self

Convenience constructor that enables acceptance of self-signed/invalid certificates. This is gated behind the insecure-dangerous feature and intended strictly for development and testing. NEVER enable in production.

Examples found in repository?
examples/self-signed-certs/main.rs (line 47)
5fn main() {
6    // Default secure client (uses OS trust store when built with default features)
7    let mut headers = HashMap::new();
8    headers.insert("x-app".into(), "example".into());
9
10    let client = HttpClient::builder()
11        .with_timeout(Duration::from_secs(10))
12        .with_default_headers(headers)
13        .build();
14
15    // Demonstrate a request (no network I/O in this example crate yet)
16    client
17        .request("https://example.com")
18        .expect("request should succeed on native targets");
19
20    // Production with rustls + custom Root CA (e.g., self-signed for your private service)
21    // Note: Requires building with: --no-default-features --features rustls
22    #[cfg(feature = "rustls")]
23    {
24        // Option 1: Load CA certificate from raw PEM bytes
25        let ca_pem: &[u8] =
26            b"-----BEGIN CERTIFICATE-----\n...your root ca...\n-----END CERTIFICATE-----\n";
27        let _rustls_client = HttpClient::builder()
28            .with_timeout(Duration::from_secs(10))
29            .with_root_ca_pem(ca_pem)
30            .build();
31        let _ = _rustls_client.request("https://private.local");
32
33        // Option 2: Load CA certificate from a file path
34        // Note: This will panic if the file doesn't exist - ensure your cert file is available
35        // let _rustls_client_from_file = HttpClient::builder()
36        //     .with_timeout(Duration::from_secs(10))
37        //     .with_root_ca_file("path/to/your/root-ca.pem")
38        //     .build();
39        // let _ = _rustls_client_from_file.request("https://private.local");
40    }
41
42    // Local development only: accept invalid/self-signed certs (dangerous)
43    // Build with: --features insecure-dangerous (or with rustls,insecure-dangerous)
44    #[cfg(feature = "insecure-dangerous")]
45    {
46        // Shortcut:
47        let _dev_client = HttpClient::with_self_signed_certs();
48        let _ = _dev_client.request("https://localhost:8443");
49
50        // Or explicit builder method:
51        let _dev_client2 = HttpClient::builder()
52            .insecure_accept_invalid_certs(true)
53            .build();
54        let _ = _dev_client2.request("https://localhost:8443");
55    }
56
57    println!("Example finished. See README for feature flags and commands.");
58}
Source§

impl HttpClient

Source

pub fn request(&self, _url: &str) -> Result<(), ClientError>

Minimal runtime method to demonstrate how requests would be issued. On native targets, this currently returns Ok(()) as a placeholder without performing network I/O.

Examples found in repository?
examples/self-signed-certs/main.rs (line 17)
5fn main() {
6    // Default secure client (uses OS trust store when built with default features)
7    let mut headers = HashMap::new();
8    headers.insert("x-app".into(), "example".into());
9
10    let client = HttpClient::builder()
11        .with_timeout(Duration::from_secs(10))
12        .with_default_headers(headers)
13        .build();
14
15    // Demonstrate a request (no network I/O in this example crate yet)
16    client
17        .request("https://example.com")
18        .expect("request should succeed on native targets");
19
20    // Production with rustls + custom Root CA (e.g., self-signed for your private service)
21    // Note: Requires building with: --no-default-features --features rustls
22    #[cfg(feature = "rustls")]
23    {
24        // Option 1: Load CA certificate from raw PEM bytes
25        let ca_pem: &[u8] =
26            b"-----BEGIN CERTIFICATE-----\n...your root ca...\n-----END CERTIFICATE-----\n";
27        let _rustls_client = HttpClient::builder()
28            .with_timeout(Duration::from_secs(10))
29            .with_root_ca_pem(ca_pem)
30            .build();
31        let _ = _rustls_client.request("https://private.local");
32
33        // Option 2: Load CA certificate from a file path
34        // Note: This will panic if the file doesn't exist - ensure your cert file is available
35        // let _rustls_client_from_file = HttpClient::builder()
36        //     .with_timeout(Duration::from_secs(10))
37        //     .with_root_ca_file("path/to/your/root-ca.pem")
38        //     .build();
39        // let _ = _rustls_client_from_file.request("https://private.local");
40    }
41
42    // Local development only: accept invalid/self-signed certs (dangerous)
43    // Build with: --features insecure-dangerous (or with rustls,insecure-dangerous)
44    #[cfg(feature = "insecure-dangerous")]
45    {
46        // Shortcut:
47        let _dev_client = HttpClient::with_self_signed_certs();
48        let _ = _dev_client.request("https://localhost:8443");
49
50        // Or explicit builder method:
51        let _dev_client2 = HttpClient::builder()
52            .insecure_accept_invalid_certs(true)
53            .build();
54        let _ = _dev_client2.request("https://localhost:8443");
55    }
56
57    println!("Example finished. See README for feature flags and commands.");
58}

Trait Implementations§

Source§

impl Default for HttpClient

Default construction uses builder defaults.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.