Switchy HTTP
A generic HTTP client abstraction library providing unified interfaces for HTTP operations with pluggable backend implementations.
Features
- Generic Client Interface: Unified traits for HTTP clients, request builders, and responses
- Multiple Backend Support: Abstraction over different HTTP implementations (reqwest, simulator)
- Request Building: Fluent API for building HTTP requests with headers, query parameters, and body
- Response Handling: Unified response interface for status, headers, text, bytes, and streaming
- JSON Support: Built-in JSON serialization/deserialization support
- Streaming Support: Byte stream responses for large data handling
- Method Support: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS methods with dedicated functions; CONNECT and TRACE via
request()method - Error Handling: Unified error types across different backends
Installation
Add this to your Cargo.toml:
[]
# Default enables: reqwest + simulator + json + stream
= "0.1.4"
# Real network backend
# switchy_http = { version = "0.1.4", default-features = false, features = ["reqwest", "json", "stream"] }
# Simulated backend (no network requests)
# switchy_http = { version = "0.1.4", default-features = false, features = ["simulator", "json", "stream"] }
When both reqwest and simulator are enabled, switchy_http::Client uses the simulator backend. Enable only one backend if you need deterministic backend selection.
Usage
Basic HTTP Requests
use ;
async
POST with JSON Body
use Client;
use json;
async
Handling Different Response Types
use Client;
use ;
async
Streaming Large Responses
use Client;
use StreamExt;
async
Custom Headers and Query Parameters
use ;
async
Error Handling
use ;
async
Architecture
Core Traits
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 interface
Wrapper Types
ClientWrapper: Wraps backend-specific clients with unified interfaceRequestBuilderWrapper: Wraps backend-specific request buildersResponseWrapper: Wraps backend-specific responses
Header Enumeration
Common HTTP headers are available as enum variants:
AuthorizationUserAgentRangeContentLength
Backend Features
reqwest Feature
Enables integration with the popular reqwest HTTP client library.
simulator Feature
Enables a simulated HTTP backend for testing and development.
json Feature
Adds JSON serialization/deserialization support using serde_json.
stream Feature
Enables streaming response support for handling large responses.
Compression Features
Enable automatic decompression of HTTP responses (requires reqwest feature):
brotli- Brotli decompressiondeflate- Deflate decompressiongzip- Gzip decompressionzstd- Zstandard decompression
Error Types
Error::Decode- Response decoding failuresError::Deserialize- JSON deserialization errors (withjsonfeature)Error::Reqwest- Reqwest-specific errors (withreqwestfeature)
Thread Safety
All client types implement Send + Sync for safe usage across async tasks and threads.