| Crates.io | native-svc |
| lib.rs | native-svc |
| version | 0.1.2 |
| created_at | 2025-06-28 17:41:53.523881+00 |
| updated_at | 2025-06-30 12:24:04.20088+00 |
| description | An HTTP adapter that implements the embedded-svc interface using hyper as the backend |
| homepage | |
| repository | https://github.com/ThalusA/native-svc |
| max_upload_size | |
| id | 1730016 |
| size | 51,246 |
A lightweight, blocking HTTP client for testing embedded Rust environments in native environments, built on hyper and tokio. It implements the embedded_svc::http::client::Connection trait with TLS support, zero-copy buffering, custom error handling, and synchronous I/O over HTTP bodies.
An HTTP adapter that implements the embedded-svc interface using hyper as the backend.
This library enables HTTP/HTTPS requests with a familiar synchronous API while leveraging the power and robustness of hyper under the hood.
embedded-svchyper-tlshyper and tokio for optimal performanceAdd this to your Cargo.toml:
[dependencies]
native-svc = "0.1.0"
use native_svc::HyperHttpConnection;
use embedded_svc::http::client::Client;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a connection
let conn = HyperHttpConnection::new()?;
let mut client = Client::wrap(conn);
// Perform a GET request
let request = client.get("https://httpbin.org/get")?;
let mut response = request.submit()?;
// Read the response
let mut body = Vec::new();
let mut buf = [0u8; 1024];
while let Ok(n) = response.read(&mut buf) {
if n == 0 { break; }
body.extend_from_slice(&buf[..n]);
}
println!("Status: {}", response.status());
println!("Body: {}", String::from_utf8_lossy(&body));
Ok(())
}
use native_svc::HyperHttpConnection;
use embedded_svc::http::client::Client;
use embedded_svc::io::Write;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let conn = HyperHttpConnection::new()?;
let mut client = Client::wrap(conn);
// Prepare JSON data
let json_data = r#"{"name": "John", "age": 30}"#;
let headers = &[
("Content-Type", "application/json"),
("Content-Length", &json_data.len().to_string()),
];
// Create and send the request
let mut request = client.post("https://httpbin.org/post", headers)?;
request.write_all(json_data.as_bytes())?;
request.flush()?;
let mut response = request.submit()?;
// Process the response
println!("Status: {}", response.status());
if let Some(content_type) = response.header("content-type") {
println!("Content-Type: {}", content_type);
}
Ok(())
}
use native_svc::{HyperHttpConnection, HyperError};
fn make_request() -> Result<String, HyperError> {
let conn = HyperHttpConnection::new()?;
let mut client = embedded_svc::http::client::Client::wrap(conn);
let request = client.get("https://example.com")?;
let mut response = request.submit()?;
let mut body = String::new();
let mut buffer = [0u8; 1024];
loop {
match response.read(&mut buffer)? {
0 => break,
n => body.push_str(&String::from_utf8_lossy(&buffer[..n])),
}
}
Ok(body)
}
The library is organized into modules:
lib.rs: Main HyperHttpConnection structure and trait implementationserror.rs: Custom error types with detailed error handlingembedded_svc::http::client::Connection: Main interface for HTTP connectionsembedded_svc::io::Read: Reading response bodyembedded_svc::io::Write: Writing request bodyembedded_svc::http::Status: HTTP status accessembedded_svc::http::Headers: HTTP headers accessRun tests with:
cargo test
Note: Integration tests require an Internet connection as they use httpbin.org.
hyperhyper-tlsContributions are welcome! Here's how to contribute:
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)cargo clippy passes without warningsThis project is licensed under MIT. See the LICENSE files for details.
tokio and hyperMade with โค๏ธ in Rust