Skip to content

Commit b12cb23

Browse files
authored
make reqwest optional for azure_core (#777)
1 parent 6d4c466 commit b12cb23

File tree

6 files changed

+46
-41
lines changed

6 files changed

+46
-41
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ jobs:
5959
cargo fmt --manifest-path services/Cargo.toml --all -- --check
6060
if: matrix.rust == 'stable'
6161

62+
- name: check core with --no-default-features
63+
run: cargo check -p azure_core --no-default-features
64+
6265
- name: check core for wasm
6366
run: cargo check -p azure_core --target=wasm32-unknown-unknown
6467

65-
# - name: check core for hyper
66-
# run: cargo check -p azure_core --no-default-features --features enable_hyper
67-
6868
- name: sdk tests
6969
run: cargo test --all --features mock_transport_framework
7070

sdk/core/src/errors.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ impl From<super::error::Error> for Error {
5252
}
5353
}
5454

55-
#[cfg(feature = "enable_hyper")]
56-
type HttpClientError = hyper::Error;
57-
#[cfg(any(feature = "enable_reqwest", feature = "enable_reqwest_rustls"))]
58-
type HttpClientError = reqwest::Error;
55+
type HttpClientError = Box<dyn std::error::Error + Send + Sync + 'static>;
5956

6057
/// An error caused by a failure to parse data.
6158
#[non_exhaustive]

sdk/core/src/http_client.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@ use bytes::Bytes;
66
#[allow(unused_imports)]
77
use futures::TryStreamExt;
88
use http::{Request, Response, StatusCode};
9-
#[cfg(feature = "enable_hyper")]
10-
#[allow(unused_imports)]
11-
use hyper_rustls::HttpsConnector;
129
use serde::Serialize;
13-
use std::sync::Arc;
1410

15-
/// Construct a new HTTP client with the `reqwest` backend.
11+
/// Construct a new `HttpClient` with the `reqwest` backend.
1612
#[cfg(any(feature = "enable_reqwest", feature = "enable_reqwest_rustls"))]
17-
pub fn new_http_client() -> Arc<dyn HttpClient> {
18-
Arc::new(reqwest::Client::new())
19-
}
20-
21-
/// Construct a new HTTP client with the `hyper` backend.
22-
#[cfg(feature = "enable_hyper")]
23-
pub fn new_http_client() -> Arc<dyn HttpClient> {
24-
Arc::new(hyper::Client::builder().build(hyper_rustls::HttpsConnector::with_native_roots()))
13+
pub fn new_http_client() -> std::sync::Arc<dyn HttpClient> {
14+
std::sync::Arc::new(reqwest::Client::new())
2515
}
2616

2717
/// An HTTP client which can send requests.
@@ -82,12 +72,12 @@ impl HttpClient for reqwest::Client {
8272
let reqwest_request = reqwest_request
8373
.body(request.into_body())
8474
.build()
85-
.map_err(HttpError::BuildClientRequest)?;
75+
.map_err(|error| HttpError::BuildClientRequest(error.into()))?;
8676

8777
let reqwest_response = self
8878
.execute(reqwest_request)
8979
.await
90-
.map_err(HttpError::ExecuteRequest)?;
80+
.map_err(|error| HttpError::ExecuteRequest(error.into()))?;
9181

9282
let mut response = Response::builder().status(reqwest_response.status());
9383

@@ -100,7 +90,7 @@ impl HttpClient for reqwest::Client {
10090
reqwest_response
10191
.bytes()
10292
.await
103-
.map_err(HttpError::ReadBytes)?,
93+
.map_err(|error| HttpError::ReadBytes(error.into()))?,
10494
)
10595
.map_err(HttpError::BuildResponse)?;
10696

@@ -125,21 +115,21 @@ impl HttpClient for reqwest::Client {
125115
Body::Bytes(bytes) => reqwest_request
126116
.body(bytes)
127117
.build()
128-
.map_err(HttpError::BuildClientRequest)?,
118+
.map_err(|error| HttpError::BuildClientRequest(error.into()))?,
129119
Body::SeekableStream(mut seekable_stream) => {
130120
seekable_stream.reset().await.unwrap(); // TODO: remove unwrap when `HttpError` has been removed
131121

132122
reqwest_request
133123
.body(reqwest::Body::wrap_stream(seekable_stream))
134124
.build()
135-
.map_err(HttpError::BuildClientRequest)?
125+
.map_err(|error| HttpError::BuildClientRequest(error.into()))?
136126
}
137127
};
138128

139129
let reqwest_response = self
140130
.execute(reqwest_request)
141131
.await
142-
.map_err(HttpError::ExecuteRequest)?;
132+
.map_err(|error| HttpError::ExecuteRequest(error.into()))?;
143133
let mut response = crate::ResponseBuilder::new(reqwest_response.status());
144134

145135
for (key, value) in reqwest_response.headers() {

sdk/core/src/options.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::policies::{ExponentialRetryPolicy, FixedRetryPolicy, NoRetryPolicy, Policy};
2-
use crate::{new_http_client, HttpClient};
2+
use crate::HttpClient;
33
use std::sync::Arc;
44
use std::time::Duration;
55

@@ -15,7 +15,11 @@ use std::time::Duration;
1515
/// .retry(RetryOptions::default().max_retries(10u32))
1616
/// .telemetry(TelemetryOptions::default().application_id("my-application"));
1717
/// ```
18-
#[derive(Clone, Debug, Default)]
18+
#[derive(Clone, Debug)]
19+
#[cfg_attr(
20+
any(feature = "enable_reqwest", feature = "enable_reqwest_rustls"),
21+
derive(Default)
22+
)]
1923
pub struct ClientOptions {
2024
/// Policies called per call.
2125
pub(crate) per_call_policies: Vec<Arc<dyn Policy>>,
@@ -30,8 +34,14 @@ pub struct ClientOptions {
3034
}
3135

3236
impl ClientOptions {
33-
pub fn new() -> Self {
34-
Self::default()
37+
pub fn new(transport: TransportOptions) -> Self {
38+
Self {
39+
per_call_policies: Vec::new(),
40+
per_retry_policies: Vec::new(),
41+
retry: RetryOptions::default(),
42+
telemetry: TelemetryOptions::default(),
43+
transport,
44+
}
3545
}
3646

3747
#[cfg(feature = "mock_transport_framework")]
@@ -185,17 +195,19 @@ impl TransportOptions {
185195
}
186196

187197
#[cfg(feature = "mock_transport_framework")]
198+
#[cfg(any(feature = "enable_reqwest", feature = "enable_reqwest_rustls"))]
188199
pub fn new_with_transaction_name(transaction_name: String) -> Self {
189200
Self {
190-
http_client: new_http_client(),
201+
http_client: crate::http_client::new_http_client(),
191202
transaction_name,
192203
}
193204
}
194205
}
195206

207+
#[cfg(any(feature = "enable_reqwest", feature = "enable_reqwest_rustls"))]
196208
impl Default for TransportOptions {
197209
/// Creates an instance of the `TransportOptions` using the default `HttpClient`.
198210
fn default() -> Self {
199-
Self::new(new_http_client())
211+
Self::new(crate::http_client::new_http_client())
200212
}
201213
}

sdk/core/src/response.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
use bytes::Bytes;
22
use futures::Stream;
33
use futures::StreamExt;
4-
use http::{header::HeaderName, HeaderMap, HeaderValue, StatusCode};
4+
use http::{HeaderMap, StatusCode};
55
use std::pin::Pin;
66

77
type PinnedStream = Pin<Box<dyn Stream<Item = crate::error::Result<Bytes>> + Send + Sync>>;
88

9-
#[allow(dead_code)]
9+
#[cfg(any(feature = "enable_reqwest", feature = "enable_reqwest_rustls"))]
1010
pub(crate) struct ResponseBuilder {
1111
status: StatusCode,
1212
headers: HeaderMap,
1313
}
1414

15+
#[cfg(any(feature = "enable_reqwest", feature = "enable_reqwest_rustls"))]
1516
impl ResponseBuilder {
1617
pub fn new(status: StatusCode) -> Self {
1718
Self {
@@ -20,8 +21,12 @@ impl ResponseBuilder {
2021
}
2122
}
2223

23-
#[allow(dead_code)]
24-
pub fn with_header(&mut self, key: &HeaderName, value: HeaderValue) -> &mut Self {
24+
#[cfg(not(target_arch = "wasm32"))]
25+
pub fn with_header(
26+
&mut self,
27+
key: &http::header::HeaderName,
28+
value: http::HeaderValue,
29+
) -> &mut Self {
2530
self.headers.append(key, value);
2631
self
2732
}
@@ -39,6 +44,7 @@ pub struct Response {
3944
}
4045

4146
impl Response {
47+
#[cfg(any(feature = "enable_reqwest", feature = "enable_reqwest_rustls"))]
4248
pub(crate) fn new(status: StatusCode, headers: HeaderMap, body: PinnedStream) -> Self {
4349
Self {
4450
status,

sdk/device_update/src/client.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ impl DeviceUpdateClient {
7474
.bearer_auth(self.get_token().await?.token.secret())
7575
.send()
7676
.await
77-
.map_err(|e| Error::Core(CoreError::Http(HttpError::ExecuteRequest(e))))?;
77+
.map_err(|e| Error::Core(CoreError::Http(HttpError::ExecuteRequest(e.into()))))?;
7878

7979
let body = resp
8080
.bytes()
8181
.await
82-
.map_err(|e| Error::Core(CoreError::Http(HttpError::ReadBytes(e))))?;
82+
.map_err(|e| Error::Core(CoreError::Http(HttpError::ReadBytes(e.into()))))?;
8383
serde_json::from_slice(&body).map_err(|e| Error::Core(CoreError::Json(e)))
8484
}
8585

@@ -97,7 +97,7 @@ impl DeviceUpdateClient {
9797
let resp = req
9898
.send()
9999
.await
100-
.map_err(|e| Error::Core(CoreError::Http(HttpError::ExecuteRequest(e))))?;
100+
.map_err(|e| Error::Core(CoreError::Http(HttpError::ExecuteRequest(e.into()))))?;
101101

102102
if resp.status() == 202u16 {
103103
let headers = resp.headers();
@@ -120,11 +120,11 @@ impl DeviceUpdateClient {
120120
.header("Content-Type", "application/json")
121121
.send()
122122
.await
123-
.map_err(|e| Error::Core(CoreError::Http(HttpError::ExecuteRequest(e))))?;
123+
.map_err(|e| Error::Core(CoreError::Http(HttpError::ExecuteRequest(e.into()))))?;
124124
let body = resp
125125
.text()
126126
.await
127-
.map_err(|e| Error::Core(CoreError::Http(HttpError::ReadBytes(e))))?;
127+
.map_err(|e| Error::Core(CoreError::Http(HttpError::ReadBytes(e.into()))))?;
128128
Ok(body)
129129
}
130130
}

0 commit comments

Comments
 (0)