Skip to content

Commit

Permalink
create client builder functions
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej committed May 7, 2024
1 parent edd4df0 commit 9a12331
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
14 changes: 4 additions & 10 deletions lib/cli/src/commands/package/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use indicatif::{ProgressBar, ProgressStyle};
use tempfile::NamedTempFile;
use wasmer_config::package::{PackageIdent, PackageSource};
use wasmer_registry::wasmer_env::WasmerEnv;
use wasmer_wasix::http::reqwest::client_blocking_builder;

/// Download a package from the registry.
#[derive(clap::Parser, Debug)]
Expand Down Expand Up @@ -138,16 +139,9 @@ impl PackageDownload {
PackageSource::Url(url) => bail!("cannot download a package from a URL: '{}'", url),
};

let client = {
let mut builder = reqwest::blocking::ClientBuilder::new();

let proxy = std::env::var("http_proxy").or_else(|_| std::env::var("HTTP_PROXY"));
if let Ok(scheme) = proxy {
builder = builder.proxy(reqwest::Proxy::all(scheme)?);
}

builder.build().context("Could not create reqwest client")?
};
let client = client_blocking_builder()?
.build()
.context("failed to create reqwest client")?;

let mut b = client
.get(download_url)
Expand Down
34 changes: 25 additions & 9 deletions lib/wasix/src/http/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,11 @@ impl ReqwestHttpClient {
.with_context(|| format!("Invalid http method {}", request.method))?;

// TODO: use persistent client?
let client = {
let builder = {
let _guard = Handle::try_current().map_err(|_| self.handle.enter());
let mut builder = reqwest::Client::builder().connect_timeout(self.connect_timeout);

let proxy = env::var("http_proxy").or_else(|_| env::var("HTTP_PROXY"));
if let Ok(scheme) = proxy {
builder = builder.proxy(reqwest::Proxy::all(scheme)?);
}

builder.build().context("Could not create reqwest client")?
client_builder()?.connect_timeout(self.connect_timeout)
};
let client = builder.build().context("failed to create reqwest client")?;

let mut builder = client.request(method, request.url.as_str());
for (header, val) in &request.headers {
Expand Down Expand Up @@ -98,3 +92,25 @@ impl super::HttpClient for ReqwestHttpClient {
Box::pin(f)
}
}

pub fn client_builder() -> Result<reqwest::ClientBuilder, anyhow::Error> {
let mut builder = reqwest::Client::builder();

let proxy = env::var("http_proxy").or_else(|_| env::var("HTTP_PROXY"));
if let Ok(scheme) = proxy {
builder = builder.proxy(reqwest::Proxy::all(scheme)?);
}

Ok(builder)
}

pub fn client_blocking_builder() -> Result<reqwest::blocking::ClientBuilder, anyhow::Error> {
let mut builder = reqwest::blocking::Client::builder();

let proxy = env::var("http_proxy").or_else(|_| env::var("HTTP_PROXY"));
if let Ok(scheme) = proxy {
builder = builder.proxy(reqwest::Proxy::all(scheme)?);
}

Ok(builder)
}

0 comments on commit 9a12331

Please sign in to comment.