Skip to content

Commit 7334120

Browse files
committed
chore: move new() to Backend trait
1 parent 9aa6273 commit 7334120

3 files changed

Lines changed: 23 additions & 22 deletions

File tree

libdd-http-client/src/backend/hyper_backend.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ impl std::fmt::Debug for HyperBackend {
2525

2626
#[cfg(feature = "hyper-backend")]
2727
impl HyperBackend {
28-
pub(crate) fn new(
29-
_timeout: std::time::Duration,
30-
transport: TransportConfig,
31-
) -> Result<Self, HttpClientError> {
32-
let client = http_common::client_builder().build(Connector::default());
33-
Ok(Self { client, transport })
34-
}
35-
3628
/// Rewrite the request URL for UDS/Named Pipe transports.
3729
fn rewrite_url(&self, url: &str) -> Result<hyper::Uri, HttpClientError> {
3830
match &self.transport {
@@ -156,6 +148,14 @@ fn map_hyper_error(e: hyper_util::client::legacy::Error) -> HttpClientError {
156148

157149
#[cfg(feature = "hyper-backend")]
158150
impl super::Backend for HyperBackend {
151+
fn new(
152+
_timeout: std::time::Duration,
153+
transport: TransportConfig,
154+
) -> Result<Self, HttpClientError> {
155+
let client = http_common::client_builder().build(Connector::default());
156+
Ok(Self { client, transport })
157+
}
158+
159159
async fn send(
160160
&self,
161161
mut request: HttpRequest,
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use crate::{config, HttpClientError, HttpRequest, HttpResponse};
5+
use std::time::Duration;
6+
47
#[cfg(all(feature = "hyper-backend", not(feature = "reqwest-backend")))]
58
pub(crate) mod hyper_backend;
69
#[cfg(feature = "reqwest-backend")]
@@ -9,13 +12,16 @@ pub(crate) mod reqwest_backend;
912
/// The internal async transport backend.
1013
///
1114
/// This trait uses native AFIT (stable since Rust 1.75, MSRV is 1.84.1).
12-
/// It is intentionally not object-safe — `HttpClient` holds a concrete backend
13-
/// type, never a `dyn Backend`.
14-
pub(crate) trait Backend {
15+
/// It is intentionally not object-safe — [HttpClient] holds a concrete backend type, never a `dyn
16+
/// Backend`.
17+
pub(crate) trait Backend: Sized {
18+
/// Construct a new backend with the given timeout and transport.
19+
fn new(timeout: Duration, transport: config::TransportConfig) -> Result<Self, HttpClientError>;
20+
1521
/// Send an HTTP request and return the response.
1622
async fn send(
1723
&self,
18-
request: crate::HttpRequest,
19-
config: &crate::config::HttpClientConfig,
20-
) -> Result<crate::HttpResponse, crate::HttpClientError>;
24+
request: HttpRequest,
25+
config: &config::HttpClientConfig,
26+
) -> Result<HttpResponse, HttpClientError>;
2127
}

libdd-http-client/src/backend/reqwest_backend.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ pub(crate) struct ReqwestBackend {
1717
}
1818

1919
#[cfg(feature = "reqwest-backend")]
20-
impl ReqwestBackend {
21-
/// Construct a new backend with the given timeout and transport.
22-
///
23-
/// Creates a `reqwest::Client` with connection pooling enabled.
24-
pub(crate) fn new(
20+
impl super::Backend for ReqwestBackend {
21+
// Creates a `reqwest::Client` with connection pooling enabled.
22+
fn new(
2523
timeout: std::time::Duration,
2624
transport: TransportConfig,
2725
) -> Result<Self, HttpClientError> {
@@ -44,10 +42,7 @@ impl ReqwestBackend {
4442
.map_err(|e| HttpClientError::InvalidConfig(e.to_string()))?;
4543
Ok(Self { client })
4644
}
47-
}
4845

49-
#[cfg(feature = "reqwest-backend")]
50-
impl super::Backend for ReqwestBackend {
5146
async fn send(
5247
&self,
5348
request: HttpRequest,

0 commit comments

Comments
 (0)