Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/algod_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ snafu = { workspace = true }
base64 = "^0.22"
uuid = { version = "^1.0", features = ["v4"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.15", features = ["js"] }

[dev-dependencies]
tokio = { version = "1.0", features = ["full"] }
tokio-test = "^0.4"
3 changes: 3 additions & 0 deletions crates/algokit_crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ zeroize = { version = "1.8", features = ["derive"] }
getrandom = "0.4.1"
async-trait = "0.1.89"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.4.1", features = ["wasm_js"] }

[dev-dependencies]
tokio = { version = "1.49.0", features = ["macros", "rt"] }
47 changes: 47 additions & 0 deletions crates/algokit_http_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct HttpResponse {
pub headers: HashMap<String, String>,
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg_attr(feature = "ffi_uniffi", uniffi::export(with_foreign))]
#[async_trait]
/// This trait must be implemented by any HTTP client that is used by our Rust crates.
Expand All @@ -65,6 +66,21 @@ pub trait HttpClient: Send + Sync {
) -> Result<HttpResponse, HttpError>;
}

#[cfg(target_arch = "wasm32")]
#[async_trait(?Send)]
/// This trait must be implemented by any HTTP client that is used by our Rust crates.
/// It is assumed the implementing type will provide the hostname, port, headers, etc. as needed for each request.
pub trait HttpClient {
async fn request(
&self,
http_method: HttpMethod,
path: String,
query: Option<HashMap<String, String>>,
body: Option<Vec<u8>>,
headers: Option<HashMap<String, String>>,
) -> Result<HttpResponse, HttpError>;
}

#[cfg(feature = "default_client")]
pub struct DefaultHttpClient {
client: reqwest::Client,
Expand Down Expand Up @@ -112,6 +128,7 @@ impl DefaultHttpClient {
}

#[cfg(feature = "default_client")]
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
impl HttpClient for DefaultHttpClient {
async fn request(
Expand All @@ -121,6 +138,36 @@ impl HttpClient for DefaultHttpClient {
query: Option<HashMap<String, String>>,
body: Option<Vec<u8>>,
headers: Option<HashMap<String, String>>,
) -> Result<HttpResponse, HttpError> {
self.perform_request(method, path, query, body, headers).await
}
}

#[cfg(feature = "default_client")]
#[cfg(target_arch = "wasm32")]
#[async_trait(?Send)]
impl HttpClient for DefaultHttpClient {
async fn request(
&self,
method: HttpMethod,
path: String,
query: Option<HashMap<String, String>>,
body: Option<Vec<u8>>,
headers: Option<HashMap<String, String>>,
) -> Result<HttpResponse, HttpError> {
self.perform_request(method, path, query, body, headers).await
}
}

#[cfg(feature = "default_client")]
impl DefaultHttpClient {
async fn perform_request(
&self,
method: HttpMethod,
path: String,
query: Option<HashMap<String, String>>,
body: Option<Vec<u8>>,
headers: Option<HashMap<String, String>>,
) -> Result<HttpResponse, HttpError> {
let url = format!("{}{}", self.base_url, path);
let method = reqwest::Method::from_bytes(method.as_str().as_bytes()).map_err(|e| {
Expand Down
Loading