Skip to content

Commit

Permalink
Replace reqwest with ureq
Browse files Browse the repository at this point in the history
  • Loading branch information
leshow authored and atroche committed Mar 25, 2019
1 parent 7e99bb8 commit acf3367
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tempfile = "3"
base64 = "0.10"
derive_builder = "0.7.1"
which = "2.0"
reqwest = { version = "0.9", optional = true }
ureq = { version = "0.9", optional = true }
directories = { version = "1.0", optional = true }
zip = { version = "0.5", optional = true }

Expand All @@ -43,5 +43,5 @@ path = "src/lib.rs"

[features]
default = [ "fetch" ]
fetch = [ "reqwest", "directories", "zip" ]
fetch = [ "ureq", "directories", "zip" ]
nightly = []
23 changes: 11 additions & 12 deletions src/browser/fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use directories::ProjectDirs;
use failure::{format_err, Error};
use log::*;
use reqwest::{self, header::CONTENT_LENGTH};
use ureq;
use zip;

use std::{
Expand Down Expand Up @@ -104,14 +104,14 @@ impl<'a> Fetcher<'a> {
let url = dl_url(self.rev)?;
info!("Chrome download url: {}", url);
let total = get_size(&url)?;
info!("Total size of download: {}", total);
info!("Total size of download: {} MiB", total);
let path = self.base_path(self.rev).with_extension("zip");

info!("Creating file for download: {}", &path.display());
let mut file = OpenOptions::new().create(true).write(true).open(&path)?;

let mut resp = reqwest::get(&url)?;
io::copy(&mut resp, &mut file)?;
let resp = ureq::get(&url).call();
io::copy(&mut resp.into_reader(), &mut file)?;

self.unzip(&path)?;

Expand All @@ -123,7 +123,10 @@ impl<'a> Fetcher<'a> {
let extract_path = self.base_path(self.rev);
fs::create_dir_all(&extract_path)?;

info!("Extracting: {}", extract_path.display());
info!(
"Extracting (this can take a while): {}",
extract_path.display()
);

for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
Expand Down Expand Up @@ -179,13 +182,9 @@ impl<'a> Fetcher<'a> {
}

fn get_size<U: AsRef<str>>(url: U) -> Result<u64, Error> {
let client = reqwest::Client::new();
let response = client.head(url.as_ref()).send()?;
match response.headers().get(CONTENT_LENGTH) {
Some(len) => {
let length = u64::from_str(len.to_str()?)?;
Ok(length)
}
let resp = ureq::get(url.as_ref()).call();
match resp.header("Content-Length") {
Some(len) => Ok(u64::from_str(len)? / 2_u64.pow(20)),
None => Err(format_err!("response doesn't include the content length")),
}
}
Expand Down

0 comments on commit acf3367

Please sign in to comment.