Skip to content

Instantly share code, notes, and snippets.

@yaymukund
Created August 27, 2018 16:24
Show Gist options
  • Save yaymukund/37494a1a98094a01a387fd282b2f1a88 to your computer and use it in GitHub Desktop.
Save yaymukund/37494a1a98094a01a387fd282b2f1a88 to your computer and use it in GitHub Desktop.
use super::download::Download;
use hyper::client::HttpConnector;
use hyper::rt::Future;
use hyper::{Body, Client};
use hyper_tls::HttpsConnector;
use native_tls::TlsConnector;
use tokio::runtime::Runtime;
pub type HttpClient = Client<HttpsConnector<HttpConnector>>;
pub struct Downloader {
client: HttpClient,
runtime: Runtime,
downloads: Vec<Download>,
}
impl Downloader {
pub fn new() -> Downloader {
let runtime = Runtime::new().expect("Could not create tokio runtime");
let tls = TlsConnector::new().unwrap();
let http_connector = HttpConnector::new_with_handle(4, runtime.reactor().clone());
let https_connector = HttpsConnector::from((http_connector, tls));
let client = Client::builder()
.http1_writev(false)
.build::<_, Body>(https_connector);
Downloader {
client,
runtime,
downloads: Vec::new(),
}
}
pub fn download(&mut self, uri: &str) {
let download = Download::new(uri);
self.downloads.push(download);
let download = self.downloads.last_mut().unwrap();
// ^^^^^^^^^^^^^^^
// cannot infer an appropriate lifetime for lifetime
// parameter in function call due to conflicting
// requirements
let future = download.make_future(&self.client);
self.runtime.spawn(future);
}
pub fn shutdown(self) {
self.runtime.shutdown_on_idle().wait().unwrap()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment