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
|
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use anyhow::Result;
use bytes::Bytes;
use reqsign_core::{Context, OsEnv};
use reqsign_http_send_reqwest::ReqwestHttpSend;
use reqwest::Client;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<()> {
// Create a custom reqwest client with specific configuration
let client = Client::builder()
.timeout(Duration::from_secs(30))
.pool_max_idle_per_host(10)
.user_agent("reqsign-example/1.0")
.danger_accept_invalid_certs(false)
.build()?;
println!("Created custom HTTP client with:");
println!(" - 30 second timeout");
println!(" - Max 10 idle connections per host");
println!(" - Custom user agent");
// Create context with the custom client
let ctx = Context::new()
.with_http_send(ReqwestHttpSend::new(client))
.with_env(OsEnv);
// Test the HTTP client with a simple request
let test_url = "https://httpbin.org/get";
println!("\nTesting HTTP client with GET {test_url}");
let req = http::Request::builder()
.method("GET")
.uri(test_url)
.header("X-Test-Header", "reqsign-example")
.body(Bytes::new())?;
match ctx.http_send(req).await {
Ok(resp) => {
println!("Response status: {}", resp.status());
println!("Response headers:");
for (name, value) in resp.headers() {
println!(" {name}: {value:?}");
}
let body = resp.body();
if let Ok(text) = String::from_utf8(body.to_vec()) {
println!("\nResponse body:");
println!("{text}");
}
}
Err(e) => {
eprintln!("Request failed: {e}");
}
}
// Demonstrate using the default client
println!("\n--- Using default client ---");
let default_ctx = Context::new()
.with_http_send(ReqwestHttpSend::default())
.with_env(OsEnv);
let req2 = http::Request::builder()
.method("POST")
.uri("https://httpbin.org/post")
.header("Content-Type", "application/json")
.body(Bytes::from(r#"{"message": "Hello from reqsign!"}"#))?;
match default_ctx.http_send(req2).await {
Ok(resp) => {
println!("POST request successful!");
println!("Response status: {}", resp.status());
}
Err(e) => {
eprintln!("POST request failed: {e}");
}
}
Ok(())
}
|