pub trait HttpClient: Send + Sync {
// Required method
fn request(
&self,
req: HttpRequest,
) -> impl Future<Output = Result<HttpResponse, HttpError>> + Send;
}Expand description
Trait for making HTTP requests.
§Design
This trait abstracts the HTTP client implementation, enabling:
- Dependency injection for testing with mock clients
- Swapping HTTP libraries without changing calling code
- Adding cross-cutting concerns (logging, metrics) via decorators
§Example
ⓘ
use ddns_a::webhook::{HttpClient, HttpRequest, HttpResponse, HttpError};
struct MockClient {
response: HttpResponse,
}
impl HttpClient for MockClient {
async fn request(&self, _req: HttpRequest) -> Result<HttpResponse, HttpError> {
Ok(self.response.clone())
}
}Required Methods§
Sourcefn request(
&self,
req: HttpRequest,
) -> impl Future<Output = Result<HttpResponse, HttpError>> + Send
fn request( &self, req: HttpRequest, ) -> impl Future<Output = Result<HttpResponse, HttpError>> + Send
Sends an HTTP request and returns the response.
§Arguments
req- The HTTP request to send
§Returns
The HTTP response on success, or an HttpError on failure.
§Errors
Returns HttpError when:
- Network connection fails (
HttpError::Connection) - Request times out (
HttpError::Timeout) - URL is invalid (
HttpError::InvalidUrl)
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.