Files
hyper-custom-cert/crates/hyper-custom-cert/examples/self-signed-certs/main.rs
Geoff Seemueller bbc21abc2b cleanup (#1)
* 0.3.4

* Add `RequestOptions` for per-request customization with headers and timeouts

- Introduced the `RequestOptions` struct for flexible HTTP request configurations.
- Added `request_with_options` and `post_with_options` methods.
- Deprecated `request` and `post` in favor of the new methods.
- Updated examples and tests to reflect the new API.

* run cargo fmt

* Update HTTP client methods to use `request_with_options` for improved flexibility. Adjusted related test cases and examples accordingly.

* Format `request_with_options` calls for improved readability.

* - Downgrade `edition` from 2024 to 2021 in Cargo.toml files for compatibility.
- Fix nested `if let` statements to improve readability and correctness.
- Reorganize imports for consistency and structure.

* Restore github old workflows

* update ci

---------

Co-authored-by: geoffsee <>
2025-08-28 15:25:28 -04:00

67 lines
2.5 KiB
Rust

use hyper_custom_cert::HttpClient;
use std::collections::HashMap;
use std::time::Duration;
#[tokio::main]
async fn main() {
// Default secure client (uses OS trust store when built with default features)
let mut headers = HashMap::new();
headers.insert("x-app".into(), "example".into());
let client = HttpClient::builder()
.with_timeout(Duration::from_secs(10))
.with_default_headers(headers)
.build();
// Demonstrate a request (now returns HttpResponse with raw body data)
let _response = client
.request_with_options("https://example.com", None)
.await
.expect("request should succeed on native targets");
// Production with rustls + custom Root CA (e.g., self-signed for your private service)
// Note: Requires building with: --no-default-features --features rustls
#[cfg(feature = "rustls")]
{
// Option 1: Load CA certificate from raw PEM bytes
let ca_pem: &[u8] =
b"-----BEGIN CERTIFICATE-----\n...your root ca...\n-----END CERTIFICATE-----\n";
let _rustls_client = HttpClient::builder()
.with_timeout(Duration::from_secs(10))
.with_root_ca_pem(ca_pem)
.build();
let _ = _rustls_client
.request_with_options("https://private.local", None)
.await;
// Option 2: Load CA certificate from a file path
// Note: This will panic if the file doesn't exist - ensure your cert file is available
// let _rustls_client_from_file = HttpClient::builder()
// .with_timeout(Duration::from_secs(10))
// .with_root_ca_file("path/to/your/root-ca.pem")
// .build();
// let _ = _rustls_client_from_file.request("https://private.local");
}
// Local development only: accept invalid/self-signed certs (dangerous)
// Build with: --features insecure-dangerous (or with rustls,insecure-dangerous)
#[cfg(feature = "insecure-dangerous")]
{
// Shortcut:
let _dev_client = HttpClient::with_self_signed_certs();
let _ = _dev_client
.request_with_options("https://localhost:8443", None)
.await;
// Or explicit builder method:
let _dev_client2 = HttpClient::builder()
.insecure_accept_invalid_certs(true)
.build();
let _ = _dev_client2
.request_with_options("https://localhost:8443", None)
.await;
}
println!("Example finished. See README for feature flags and commands.");
}