Skip to content

Commit e1a0d62

Browse files
authored
check core for wasm (Azure#228)
1 parent 3ce95cf commit e1a0d62

File tree

15 files changed

+81
-48
lines changed

15 files changed

+81
-48
lines changed

.github/workflows/build.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Build and Test
22

3-
on:
3+
on:
44
pull_request:
55
push:
66

@@ -11,8 +11,8 @@ env:
1111
jobs:
1212
test:
1313
runs-on: ubuntu-latest
14-
strategy:
15-
matrix:
14+
strategy:
15+
matrix:
1616
rust: [stable]
1717
steps:
1818
- uses: actions/checkout@v2
@@ -22,6 +22,7 @@ jobs:
2222
profile: minimal
2323
override: true
2424
components: rustfmt
25+
target: wasm32-unknown-unknown
2526

2627
- name: fmt
2728
run: |
@@ -30,11 +31,17 @@ jobs:
3031
cargo fmt --manifest-path services/Cargo.toml --all -- --check
3132
if: matrix.rust == 'stable'
3233

33-
- name: sdk tests
34+
- name: check core for wasm
35+
run: cargo check -p azure_core --target=wasm32-unknown-unknown
36+
37+
- name: check core for hyper
38+
run: cargo check -p azure_core --no-default-features --features enable_hyper
39+
40+
- name: sdk tests
3441
run: cargo test --all
3542

3643
- name: services tests
37-
run: cargo build --manifest-path services/Cargo.toml --all
44+
run: cargo build --manifest-path services/Cargo.toml --all
3845

3946
- name: e2e tests build
4047
run: |

sdk/core/Cargo.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ base64 = "0.13"
1818
chrono = "0.4"
1919
http = "0.2"
2020
futures = "0.3"
21-
hyper = "0.14"
21+
hyper = { version = "0.14", optional = true }
2222
log = "0.4"
2323
quick-error = "1.2"
2424
serde = "1.0"
2525
serde_derive = "1.0"
2626
serde_json = "1.0"
2727
serde-xml-rs = "0.4"
2828
url = "2.2"
29-
uuid = { version = "0.8", features = ["v4"] }
29+
uuid = { version = "0.8" }
3030
bytes = "1.0"
31-
hyper-rustls = "0.22"
31+
hyper-rustls = { version = "0.22", optional = true }
3232
failure = "0.1"
3333
async-trait = "0.1"
34-
oauth2 = "=4.0.0-alpha.3"
35-
reqwest = "0.11"
34+
oauth2 = "=4.0.0-beta.1"
35+
reqwest = { version = "0.11", optional = true }
3636
paste = "1.0"
3737
md5 = "0.7"
3838

@@ -41,5 +41,8 @@ tokio = "1.0"
4141
env_logger = "0.8"
4242

4343
[features]
44+
default = ["enable_reqwest"]
45+
enable_reqwest = ["reqwest"]
46+
enable_hyper = ["hyper", "hyper-rustls"]
4447
test_e2e = []
4548
azurite_workaround = []

sdk/core/src/errors.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use http::header::ToStrError;
2-
use hyper::{self, body, Body, StatusCode};
2+
use http::StatusCode;
3+
#[cfg(feature = "enable_hyper")]
4+
use hyper::{self, body, Body};
35
use std::io::Error as IOError;
46
use std::num;
57
use std::num::ParseIntError;
@@ -191,10 +193,10 @@ quick_error! {
191193
display("json error: {}", err)
192194
cause(err)
193195
}
194-
HyperError(err: hyper::Error) {
196+
HyperError(err: Box<dyn std::error::Error + Sync + Send>) {
195197
from()
196198
display("Hyper error: {}", err)
197-
cause(err)
199+
cause(&**err)
198200
}
199201
PermissionError(err: PermissionError) {
200202
from()
@@ -281,7 +283,7 @@ quick_error! {
281283
display("uuid error: {}", err)
282284
cause(err)
283285
}
284-
ChronoParserError(err: chrono::ParseError) {
286+
ChronoParserError(err: chrono::ParseError) {
285287
from()
286288
display("Chrono parser error: {}", err)
287289
cause(err)
@@ -373,6 +375,14 @@ impl From<failure::Error> for AzureError {
373375
}
374376
}
375377

378+
#[cfg(feature = "enable_hyper")]
379+
impl From<hyper::Error> for AzureError {
380+
fn from(error: hyper::Error) -> AzureError {
381+
AzureError::HyperError(error.into())
382+
}
383+
}
384+
385+
#[cfg(feature = "enable_hyper")]
376386
#[inline]
377387
pub async fn extract_status_headers_and_body(
378388
resp: hyper::client::ResponseFuture,
@@ -386,6 +396,7 @@ pub async fn extract_status_headers_and_body(
386396
Ok((status, headers, body))
387397
}
388398

399+
#[cfg(feature = "enable_hyper")]
389400
#[inline]
390401
pub async fn extract_status_and_body(
391402
resp: hyper::client::ResponseFuture,
@@ -396,6 +407,7 @@ pub async fn extract_status_and_body(
396407
Ok((status, str::from_utf8(&body)?.to_owned()))
397408
}
398409

410+
#[cfg(feature = "enable_hyper")]
399411
#[inline]
400412
pub async fn extract_location_status_and_body(
401413
resp: hyper::client::ResponseFuture,
@@ -410,6 +422,7 @@ pub async fn extract_location_status_and_body(
410422
Ok((status, location, str::from_utf8(&body)?.to_owned()))
411423
}
412424

425+
#[cfg(feature = "enable_hyper")]
413426
#[inline]
414427
pub async fn check_status_extract_body(
415428
resp: hyper::client::ResponseFuture,
@@ -427,6 +440,7 @@ pub async fn check_status_extract_body(
427440
}
428441
}
429442

443+
#[cfg(feature = "enable_hyper")]
430444
pub async fn check_status_extract_body_2(
431445
resp: hyper::Response<Body>,
432446
expected_status: StatusCode,

sdk/core/src/headers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod utilities;
22

33
use http::request::Builder;
44

5-
pub use hyper::header::{IF_MODIFIED_SINCE, USER_AGENT};
5+
pub use http::header::{IF_MODIFIED_SINCE, USER_AGENT};
66
pub use utilities::*;
77

88
pub const MS_DATE: &str = "x-ms-date";

sdk/core/src/headers/utilities.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use super::*;
2-
use crate::errors::{check_status_extract_body_2, AzureError};
2+
use crate::errors::*;
33
use crate::request_options::LeaseId;
44
use crate::util::HeaderMapExt;
55
use crate::{Consistency, RequestId, SessionToken};
66
use chrono::{DateTime, Utc};
7+
use http::header::{HeaderName, DATE, ETAG, LAST_MODIFIED};
8+
#[cfg(feature = "enable_hyper")]
79
use http::status::StatusCode;
810
use http::HeaderMap;
9-
use hyper::header::{HeaderName, DATE, ETAG, LAST_MODIFIED};
11+
#[cfg(feature = "enable_hyper")]
1012
use hyper::{Body, Client, Request};
1113
use std::str::FromStr;
1214
use uuid::Uuid;
@@ -317,6 +319,7 @@ pub fn content_type_from_headers(headers: &HeaderMap) -> Result<&str, AzureError
317319
.to_str()?)
318320
}
319321

322+
#[cfg(feature = "enable_hyper")]
320323
pub async fn perform_http_request(
321324
client: &Client<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>>,
322325
req: Request<Body>,

sdk/core/src/http_client.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use crate::errors::{AzureError, UnexpectedHTTPResult};
1+
use crate::errors::*;
22
use async_trait::async_trait;
33
use bytes::Bytes;
44
use http::{Request, Response, StatusCode};
5+
#[cfg(feature = "enable_hyper")]
56
use hyper_rustls::HttpsConnector;
67
use serde::Serialize;
78

8-
#[async_trait]
9+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
10+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
911
pub trait HttpClient: Send + Sync + std::fmt::Debug {
1012
async fn execute_request(
1113
&self,
@@ -60,7 +62,9 @@ pub trait HttpClient: Send + Sync + std::fmt::Debug {
6062
}
6163
}
6264

63-
#[async_trait]
65+
#[cfg(feature = "enable_hyper")]
66+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
67+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
6468
impl HttpClient for hyper::Client<HttpsConnector<hyper::client::HttpConnector>> {
6569
async fn execute_request(
6670
&self,
@@ -92,7 +96,9 @@ impl HttpClient for hyper::Client<HttpsConnector<hyper::client::HttpConnector>>
9296
}
9397
}
9498

95-
#[async_trait]
99+
#[cfg(feature = "enable_reqwest")]
100+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
101+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
96102
impl HttpClient for reqwest::Client {
97103
async fn execute_request(
98104
&self,
@@ -108,9 +114,11 @@ impl HttpClient for reqwest::Client {
108114

109115
let reqwest_response = self.execute(reqwest_request).await?;
110116

111-
let mut response = Response::builder()
112-
.status(reqwest_response.status())
113-
.version(reqwest_response.version());
117+
let mut response = Response::builder().status(reqwest_response.status());
118+
119+
if let Some(version) = get_version(&reqwest_response) {
120+
response = response.version(version);
121+
}
114122

115123
for (key, value) in reqwest_response.headers() {
116124
response = response.header(key, value);
@@ -122,6 +130,19 @@ impl HttpClient for reqwest::Client {
122130
}
123131
}
124132

133+
// wasm can not get the http version
134+
#[cfg(feature = "enable_reqwest")]
135+
#[cfg(target_arch = "wasm32")]
136+
fn get_version(_response: &reqwest::Response) -> Option<http::Version> {
137+
None
138+
}
139+
140+
#[cfg(feature = "enable_reqwest")]
141+
#[cfg(not(target_arch = "wasm32"))]
142+
fn get_version(response: &reqwest::Response) -> Option<http::Version> {
143+
Some(response.version())
144+
}
145+
125146
/// Serialize to json
126147
pub fn to_json<T>(value: &T) -> Result<Bytes, Box<dyn std::error::Error + Sync + Send>>
127148
where

sdk/core/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bytes::Bytes;
2+
use http::header::{AsHeaderName, HeaderMap, HeaderName, HeaderValue};
23
use http::{self, request::Builder};
3-
use hyper::header::{AsHeaderName, HeaderMap, HeaderName, HeaderValue};
44
use std::{
55
convert::TryFrom,
66
fmt::Display,
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use azure_core::HttpClient;
22
use azure_cosmos::prelude::*;
3-
use hyper_rustls::HttpsConnector;
43
use std::error::Error;
54
use std::sync::Arc;
65

@@ -18,27 +17,13 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
1817

1918
let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;
2019

21-
// use reqwest
2220
let http_client: Arc<Box<dyn HttpClient>> = Arc::new(Box::new(reqwest::Client::new()));
2321
let client = CosmosClient::new(http_client, account.clone(), authorization_token);
2422

2523
let database_client = client.into_database_client(database_name.clone());
2624

2725
let response = database_client.get_database().execute().await?;
28-
println!("from reqwest == {:?}", response);
29-
30-
// use hyper
31-
let authorization_token = AuthorizationToken::primary_from_base64(&master_key)?;
32-
33-
let http_client: Box<dyn HttpClient> =
34-
Box::new(hyper::Client::builder().build(HttpsConnector::with_native_roots()));
35-
let http_client = Arc::new(http_client);
36-
37-
let client = CosmosClient::new(http_client, account, authorization_token);
38-
let database_client = client.into_database_client(database_name);
39-
40-
let response = database_client.get_database().execute().await?;
41-
println!("from hyper == {:?}", response);
26+
println!("response == {:?}", response);
4227

4328
Ok(())
4429
}

sdk/event_grid/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ categories = ["api-bindings"]
1313
edition = "2018"
1414

1515
[dependencies]
16-
azure_core = { path = "../core", version = "0.1.0" }
16+
azure_core = { path = "../core", version = "0.1.0", features = ["enable_hyper"] }
1717
chrono = { version = "0.4", features = ["serde"] }
1818
http = "0.2"
1919
hyper = "0.14"

sdk/event_grid/src/event_grid_response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use azure_core::errors::{AzureError, UnexpectedHTTPResult};
1+
use azure_core::errors::*;
22
use hyper::{
33
body::{self, Bytes},
44
client::ResponseFuture,

0 commit comments

Comments
 (0)