Skip to content

Commit

Permalink
Update atrium-xrpc, atrium-xrpc-client
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed Feb 26, 2024
1 parent b5254e7 commit c09153c
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 107 deletions.
228 changes: 141 additions & 87 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ tokio = { version = "1.33", default-features = false }

# HTTP client integrations
isahc = "1.7.2"
reqwest = { version = "0.11.22", default-features = false }
reqwest = { version = "0.11.24", default-features = false }
surf = { version = "2.3.2", default-features = false }

# Errors
Expand All @@ -66,3 +66,6 @@ dirs = "5.0.1"
# Testing
http-client = { version = "6.5.3", default-features = false }
mockito = "1.2"

# WebAssembly
wasm-bindgen-test = "0.3.41"
12 changes: 8 additions & 4 deletions atrium-xrpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ reqwest = { workspace = true, optional = true }
surf = { workspace = true, optional = true }

[features]
default = ["reqwest-native"]
default = ["reqwest-default-tls"]
isahc = ["dep:isahc"]
reqwest-native = ["reqwest/native-tls"]
reqwest-rustls = ["reqwest/rustls-tls"]
reqwest = ["dep:reqwest"]
reqwest-default-tls = ["reqwest/default-tls"]
surf = ["dep:surf"]

[dev-dependencies]
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
reqwest = { workspace = true, features = ["native-tls", "rustls-tls"] }
surf = { workspace = true, features = ["h1-client-rustls"] }
http-client = { workspace = true, features = ["h1_client", "rustls"] }
mockito.workspace = true
tokio = { workspace = true, features = ["macros"] }
serde = { workspace = true, features = ["derive"] }
futures.workspace = true

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test.workspace = true

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
9 changes: 3 additions & 6 deletions atrium-xrpc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
#[cfg_attr(docsrs, doc(cfg(feature = "isahc")))]
#[cfg(feature = "isahc")]
pub mod isahc;
#[cfg_attr(
docsrs,
doc(cfg(any(feature = "reqwest-native", feature = "reqwest-rustls")))
)]
#[cfg(any(feature = "reqwest-native", feature = "reqwest-rustls"))]
#[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
#[cfg(feature = "reqwest")]
pub mod reqwest;
#[cfg_attr(docsrs, doc(cfg(feature = "surf")))]
#[cfg(feature = "surf")]
pub mod surf;

#[cfg(test)]
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests;
11 changes: 10 additions & 1 deletion atrium-xrpc-client/src/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ impl ReqwestClientBuilder {
}
}

#[async_trait]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl HttpClient for ReqwestClient {
async fn send_http(
&self,
Expand All @@ -80,22 +81,30 @@ impl XrpcClient for ReqwestClient {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Duration;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn new() -> Result<(), Box<dyn std::error::Error>> {
let client = ReqwestClient::new("http://localhost:8080");
assert_eq!(client.base_uri(), "http://localhost:8080");
Ok(())
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn builder_without_client() -> Result<(), Box<dyn std::error::Error>> {
let client = ReqwestClientBuilder::new("http://localhost:8080").build();
assert_eq!(client.base_uri(), "http://localhost:8080");
Ok(())
}

// TODO: Reqwest::Client doesn't have a `timeout` in wasm module
// https://github.com/seanmonstar/reqwest/pull/1760
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn builder_with_client() -> Result<(), Box<dyn std::error::Error>> {
let client = ReqwestClientBuilder::new("http://localhost:8080")
Expand Down
8 changes: 4 additions & 4 deletions atrium-xrpc-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async fn send_query() -> Result<(), Box<dyn std::error::Error>> {
.build(),
path.to_string(),
)),
#[cfg(feature = "reqwest-native")]
#[cfg(feature = "reqwest")]
tokio::spawn(run_query(
crate::reqwest::ReqwestClientBuilder::new(base_uri)
.client(
Expand All @@ -119,7 +119,7 @@ async fn send_query() -> Result<(), Box<dyn std::error::Error>> {
.build(),
path.to_string(),
)),
#[cfg(feature = "reqwest-rustls")]
#[cfg(feature = "reqwest")]
tokio::spawn(run_query(
crate::reqwest::ReqwestClientBuilder::new(base_uri)
.client(
Expand Down Expand Up @@ -232,7 +232,7 @@ async fn send_procedure() -> Result<(), Box<dyn std::error::Error>> {
.build(),
path.to_string(),
)),
#[cfg(feature = "reqwest-native")]
#[cfg(feature = "reqwest")]
tokio::spawn(run_procedure(
crate::reqwest::ReqwestClientBuilder::new(base_uri)
.client(
Expand All @@ -244,7 +244,7 @@ async fn send_procedure() -> Result<(), Box<dyn std::error::Error>> {
.build(),
path.to_string(),
)),
#[cfg(feature = "reqwest-rustls")]
#[cfg(feature = "reqwest")]
tokio::spawn(run_procedure(
crate::reqwest::ReqwestClientBuilder::new(base_uri)
.client(
Expand Down
3 changes: 3 additions & 0 deletions atrium-xrpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ thiserror.workspace = true

[dev-dependencies]
tokio = { workspace = true, features = ["macros", "rt"] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test.workspace = true
22 changes: 18 additions & 4 deletions atrium-xrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ where
}

/// An abstract HTTP client.
#[async_trait]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait HttpClient {
async fn send_http(
&self,
Expand All @@ -55,7 +56,8 @@ pub type XrpcResult<O, E> = Result<OutputDataOrBytes<O>, self::Error<E>>;
///
/// [`send_xrpc()`](XrpcClient::send_xrpc) method has a default implementation,
/// which wraps the [`HttpClient::send_http()`]` method to handle input and output as an XRPC Request.
#[async_trait]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait XrpcClient: HttpClient {
fn base_uri(&self) -> String;
#[allow(unused_variables)]
Expand Down Expand Up @@ -127,14 +129,17 @@ pub trait XrpcClient: HttpClient {
#[cfg(test)]
mod tests {
use super::*;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;

struct DummyClient {
status: http::StatusCode,
json: bool,
body: Vec<u8>,
}

#[async_trait]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl HttpClient for DummyClient {
async fn send_http(
&self,
Expand All @@ -148,7 +153,6 @@ mod tests {
}
}

#[async_trait]
impl XrpcClient for DummyClient {
fn base_uri(&self) -> String {
"https://example.com".into()
Expand Down Expand Up @@ -195,6 +199,7 @@ mod tests {
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn deserialize_xrpc_error() {
{
let body = r#"{"error":"InvalidToken","message":"Invalid token"}"#;
Expand Down Expand Up @@ -223,6 +228,7 @@ mod tests {
}

#[tokio::test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn response_ok() {
let client = DummyClient {
status: http::StatusCode::OK,
Expand All @@ -236,6 +242,7 @@ mod tests {
}

#[tokio::test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn response_custom_error() {
let client = DummyClient {
status: http::StatusCode::BAD_REQUEST,
Expand All @@ -261,6 +268,7 @@ mod tests {
}

#[tokio::test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn response_undefined_error() {
let client = DummyClient {
status: http::StatusCode::INTERNAL_SERVER_ERROR,
Expand Down Expand Up @@ -330,6 +338,7 @@ mod tests {
enum Error {}

#[tokio::test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn response_ok() {
let body = r"data".as_bytes().to_vec();
let client = DummyClient {
Expand All @@ -349,6 +358,7 @@ mod tests {
}

#[tokio::test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn response_unexpected() {
let client = DummyClient {
status: http::StatusCode::OK,
Expand Down Expand Up @@ -407,6 +417,7 @@ mod tests {
enum Error {}

#[tokio::test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn response_ok() {
let client = DummyClient {
status: http::StatusCode::OK,
Expand All @@ -419,6 +430,7 @@ mod tests {
}

#[tokio::test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn response_unexpected() {
let client = DummyClient {
status: http::StatusCode::OK,
Expand Down Expand Up @@ -467,6 +479,7 @@ mod tests {
enum Error {}

#[tokio::test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn response_ok() {
let client = DummyClient {
status: http::StatusCode::OK,
Expand All @@ -479,6 +492,7 @@ mod tests {
}

#[tokio::test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
async fn response_unexpected() {
let client = DummyClient {
status: http::StatusCode::OK,
Expand Down

0 comments on commit c09153c

Please sign in to comment.