Skip to content

Commit

Permalink
feat: make reqwest optional but enabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Nov 13, 2021
1 parent 8f9c47d commit badbef6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 4 additions & 1 deletion ethers-contract/ethers-contract-abigen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ url = "2.1"
serde_json = "1.0.61"
serde = { version = "1.0.124", features = ["derive"] }
hex = { version = "0.4.2", default-features = false, features = ["std"] }
reqwest = { version = "0.11.3", features = ["blocking"] }
reqwest = { version = "0.11.3", features = ["blocking"] , optional = true }
once_cell = "1.8.0"
cfg-if = "1.0.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
# NOTE: this enables wasm compatibility for getrandom indirectly
getrandom = { version = "0.2", features = ["js"] }

[features]
default = ["reqwest"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
12 changes: 9 additions & 3 deletions ethers-contract/ethers-contract-abigen/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ethers_core::types::Address;

use anyhow::{anyhow, Result};
use cfg_if::cfg_if;
use inflector::Inflector;
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::quote;
Expand Down Expand Up @@ -69,10 +70,15 @@ where
Ok(address_str[2..].parse()?)
}

#[cfg(not(target_arch = "wasm32"))]
/// Perform an HTTP GET request and return the contents of the response.
pub fn http_get(url: &str) -> Result<String> {
Ok(reqwest::blocking::get(url)?.text()?)
pub fn http_get(_url: &str) -> Result<String> {
cfg_if! {
if #[cfg(any(not(target_arch = "wasm32"), not(features = "reqwest")))]{
Err(anyhow!("HTTP is unsupported"))
} else {
Ok(reqwest::blocking::get(_url)?.text()?)
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit badbef6

Please sign in to comment.