mirror of
https://github.com/tiffany352/rink-rs
synced 2024-11-10 05:34:14 +00:00
Replace hyper with reqwest
This commit is contained in:
parent
73d2b92a07
commit
9029d8478e
2 changed files with 8 additions and 29 deletions
|
@ -13,7 +13,7 @@ keywords = ["unit", "math", "conversion", "cli", "tool"]
|
|||
default = ["linefeed", "chrono-humanize", "gpl", "currency"]
|
||||
sandbox = ["libc", "ipc-channel"]
|
||||
gpl = []
|
||||
currency = ["hyper", "hyper-native-tls", "xml-rs", "json"]
|
||||
currency = ["reqwest", "xml-rs", "json"]
|
||||
nightly = ["serde", "serde_derive"]
|
||||
|
||||
[dependencies]
|
||||
|
@ -23,8 +23,7 @@ strsim = "0.5.1"
|
|||
chrono-tz = "0.2.2"
|
||||
chrono-humanize = { version = "0.0.6", optional = true }
|
||||
linefeed = { version = "0.4.0", optional = true }
|
||||
hyper = { version = "0.10.10", optional = true }
|
||||
hyper-native-tls = { version = "0.3", optional = true }
|
||||
reqwest = { version = "0.9.2", optional = true }
|
||||
libc = { version = "0.2.14", optional = true }
|
||||
ipc-channel = { version = "0.5.1", optional = true }
|
||||
xml-rs = { version = "0.3.4", optional = true }
|
||||
|
|
32
src/lib.rs
32
src/lib.rs
|
@ -43,9 +43,7 @@ extern crate libc;
|
|||
#[cfg(feature = "sandbox")]
|
||||
extern crate ipc_channel;
|
||||
#[cfg(feature = "currency")]
|
||||
extern crate hyper;
|
||||
#[cfg(feature = "currency")]
|
||||
extern crate hyper_native_tls;
|
||||
extern crate reqwest;
|
||||
#[cfg(feature = "currency")]
|
||||
extern crate xml;
|
||||
#[cfg(feature = "currency")]
|
||||
|
@ -364,16 +362,11 @@ fn btree_merge<K: ::std::cmp::Ord+Clone, V:Clone, F:Fn(&V, &V) -> Option<V>>(
|
|||
res
|
||||
}
|
||||
|
||||
#[cfg(feature = "hyper")]
|
||||
#[cfg(feature = "reqwest")]
|
||||
fn cached(file: &str, url: &str, expiration: Duration) -> Result<File, String> {
|
||||
use std::fmt::Display;
|
||||
use std::time::SystemTime;
|
||||
use std::fs;
|
||||
use std::io::{Read, Write};
|
||||
use hyper::Client;
|
||||
use hyper::status::StatusCode;
|
||||
use hyper::net::HttpsConnector;
|
||||
use hyper_native_tls::NativeTlsClient;
|
||||
|
||||
fn ts<T:Display>(x: T) -> String {
|
||||
format!("{}", x)
|
||||
|
@ -402,23 +395,10 @@ fn cached(file: &str, url: &str, expiration: Duration) -> Result<File, String> {
|
|||
try!(fs::create_dir_all(path.parent().unwrap()).map_err(|x| format!("{}", x)));
|
||||
let mut f = try!(File::create(tmppath.clone()).map_err(|x| format!("{}", x)));
|
||||
|
||||
let ssl = NativeTlsClient::new().unwrap();
|
||||
let connector = HttpsConnector::new(ssl);
|
||||
let client = Client::with_connector(connector);
|
||||
let mut res = try!(client.get(url).send().map_err(|x| format!("{}", x)));
|
||||
if res.status != StatusCode::Ok {
|
||||
return Err(format!("Request failed with status code {}", res.status))
|
||||
}
|
||||
let mut buf = vec![0; 8192];
|
||||
loop {
|
||||
match res.read(&mut buf) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
try!(f.write(&buf[..n]).map_err(|x| format!("{}", x)));
|
||||
},
|
||||
Err(e) => return Err(format!("{}", e))
|
||||
}
|
||||
}
|
||||
reqwest::get(url)
|
||||
.map_err(|err| format!("Request failed: {}", err))?
|
||||
.copy_to(&mut f).map_err(|err| format!("Request failed: {}", err))?;
|
||||
|
||||
try!(f.sync_all().map_err(|x| format!("{}", x)));
|
||||
drop(f);
|
||||
try!(fs::rename(tmppath.clone(), path.clone())
|
||||
|
|
Loading…
Reference in a new issue