Use HTTP client with timeout

This commit is contained in:
James 2018-08-07 22:26:53 +10:00
parent 1578c4e284
commit e3047c1bc9
2 changed files with 16 additions and 8 deletions

View file

@ -4,14 +4,22 @@
use hyper::header::ContentLength;
use reqwest;
use std::io::Read;
use std::time::Duration;
use reqwest::Client;
/// Builds a customised HTTP client.
pub fn build_client() -> Result<Client, String> {
Client::builder()
.timeout(Duration::from_secs(5))
.build()
.map_err(|x| format!("Unable to build cient: {:?}", x))
}
/// Downloads a text file from the specified URL.
pub fn download_text(url: &str) -> Result<String, String> {
// TODO: Decrease check time
let mut client = match reqwest::get(url) {
let mut client = match build_client()?.get(url).send() {
Ok(v) => v,
Err(v) => return Err(format!("Failed to GET resource: {:?}", v)),
};
@ -26,8 +34,7 @@ pub fn stream_file<F>(url: &str, mut callback: F) -> Result<(), String>
where
F: FnMut(Vec<u8>, u64) -> (),
{
// TODO: Decrease check time
let mut client = match reqwest::get(url) {
let mut client = match build_client()?.get(url).send() {
Ok(v) => v,
Err(v) => return Err(format!("Failed to GET resource: {:?}", v)),
};

View file

@ -2,7 +2,6 @@
//!
//! Contains the Github API implementation of a release source.
use reqwest;
use reqwest::header::UserAgent;
use reqwest::StatusCode;
@ -10,6 +9,8 @@ use serde_json;
use sources::types::*;
use http::build_client;
pub struct GithubReleases {}
/// The configuration for this release.
@ -35,7 +36,7 @@ impl ReleaseSource for GithubReleases {
let mut results: Vec<Release> = Vec::new();
// Build the HTTP client up
let client = reqwest::Client::new();
let client = build_client()?;
let mut response = client
.get(&format!(
"https://api.github.com/repos/{}/releases",