| Crates.io | switchy_http |
| lib.rs | switchy_http |
| version | 0.1.4 |
| created_at | 2025-05-07 19:59:26.633128+00 |
| updated_at | 2025-07-21 19:15:23.720497+00 |
| description | Switchy HTTP Networking package |
| homepage | |
| repository | https://github.com/MoosicBox/MoosicBox |
| max_upload_size | |
| id | 1664399 |
| size | 68,326 |
A generic HTTP client abstraction library providing unified interfaces for HTTP operations with pluggable backend implementations.
Add this to your Cargo.toml:
[dependencies]
moosicbox_http = "0.1.1"
# Choose your backend
moosicbox_http = { version = "0.1.1", features = ["reqwest"] }
# or
moosicbox_http = { version = "0.1.1", features = ["simulator"] }
use moosicbox_http::{Client, Error};
#[tokio::main]
async fn main() -> Result<(), Error> {
let client = Client::new();
// GET request
let mut response = client
.get("https://api.example.com/users")
.header("Authorization", "Bearer token123")
.query_param("page", "1")
.query_param("limit", "10")
.send()
.await?;
println!("Status: {:?}", response.status());
let text = response.text().await?;
println!("Response: {}", text);
Ok(())
}
use moosicbox_http::Client;
use serde_json::json;
async fn create_user() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let user_data = json!({
"name": "John Doe",
"email": "john@example.com"
});
let response = client
.post("https://api.example.com/users")
.header("Content-Type", "application/json")
.json(&user_data)
.send()
.await?;
if response.status().is_success() {
println!("User created successfully");
}
Ok(())
}
use moosicbox_http::Client;
use serde::{Deserialize, Serialize};
#[derive(Deserialize)]
struct User {
id: u32,
name: String,
email: String,
}
async fn fetch_user_data() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
// JSON response
let user: User = client
.get("https://api.example.com/users/1")
.send()
.await?
.json()
.await?;
println!("User: {} ({})", user.name, user.email);
// Raw bytes
let image_data = client
.get("https://api.example.com/users/1/avatar")
.send()
.await?
.bytes()
.await?;
println!("Downloaded {} bytes", image_data.len());
// Text response
let readme = client
.get("https://raw.githubusercontent.com/example/repo/README.md")
.send()
.await?
.text()
.await?;
println!("README content: {}", readme);
Ok(())
}
use moosicbox_http::Client;
use futures::StreamExt;
async fn download_large_file() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let response = client
.get("https://example.com/large-file.zip")
.send()
.await?;
let mut stream = response.bytes_stream();
let mut total_bytes = 0;
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
total_bytes += chunk.len();
println!("Downloaded {} bytes so far", total_bytes);
// Process chunk (e.g., write to file)
}
println!("Download complete: {} total bytes", total_bytes);
Ok(())
}
use moosicbox_http::{Client, Header};
async fn api_request_with_auth() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let response = client
.get("https://api.example.com/protected")
.header(Header::Authorization.as_ref(), "Bearer secret-token")
.header(Header::UserAgent.as_ref(), "MoosicBox/1.0")
.query_param("format", "json")
.query_param_opt("filter", Some("active"))
.query_params(&[("sort", "name"), ("order", "asc")])
.send()
.await?;
println!("Response status: {:?}", response.status());
Ok(())
}
use moosicbox_http::{Client, Error};
async fn handle_errors() {
let client = Client::new();
match client.get("https://invalid-url").send().await {
Ok(response) => {
println!("Request succeeded: {:?}", response.status());
}
Err(Error::Reqwest(e)) => {
println!("Network error: {}", e);
}
Err(Error::Deserialize(e)) => {
println!("JSON parsing error: {}", e);
}
Err(Error::Decode) => {
println!("Response decoding error");
}
}
}
GenericClient<RB>: Main HTTP client interface with method shortcutsGenericRequestBuilder<R>: Request builder interface for headers, params, and bodyGenericResponse: Response interface for status, headers, and body accessGenericClientBuilder<RB, C>: Client builder interfaceClientWrapper: Wraps backend-specific clients with unified interfaceRequestBuilderWrapper: Wraps backend-specific request buildersResponseWrapper: Wraps backend-specific responsesCommon HTTP headers are available as enum variants:
AuthorizationUserAgentRangeContentLengthreqwest FeatureEnables integration with the popular reqwest HTTP client library.
simulator FeatureEnables a simulated HTTP backend for testing and development.
json FeatureAdds JSON serialization/deserialization support using serde_json.
stream FeatureEnables streaming response support for handling large responses.
Error::Decode - Response decoding failuresError::Deserialize - JSON deserialization errors (with json feature)Error::Reqwest - Reqwest-specific errors (with reqwest feature)All client types implement Send + Sync for safe usage across async tasks and threads.