lient/http_client.rs: simplify code
This commit is contained in:
parent
41c039e143
commit
1adb353d6f
|
@ -25,11 +25,7 @@ impl HttpClient {
|
||||||
fn run_request(request: Request<Body>) -> Result<Value, Error> {
|
fn run_request(request: Request<Body>) -> Result<Value, Error> {
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
|
|
||||||
use std::sync::{Arc, Mutex};
|
let (tx, rx) = std::sync::mpsc::channel();
|
||||||
|
|
||||||
let mutex = Arc::new(Mutex::new(Err(format_err!("no data"))));
|
|
||||||
let mutex_c1 = mutex.clone();
|
|
||||||
let mutex_c2 = mutex.clone();
|
|
||||||
|
|
||||||
let future = client
|
let future = client
|
||||||
.request(request)
|
.request(request)
|
||||||
|
@ -54,11 +50,9 @@ impl HttpClient {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.map(move |res| {
|
.then(move |res| {
|
||||||
*mutex_c1.lock().unwrap() = Ok(res);
|
tx.send(res).unwrap();
|
||||||
})
|
Ok(())
|
||||||
.map_err(move |err| {
|
|
||||||
*mutex_c2.lock().unwrap() = Err(err);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// drop client, else client keeps connectioon open (keep-alive feature)
|
// drop client, else client keeps connectioon open (keep-alive feature)
|
||||||
|
@ -66,8 +60,7 @@ impl HttpClient {
|
||||||
|
|
||||||
rt::run(future);
|
rt::run(future);
|
||||||
|
|
||||||
Arc::try_unwrap(mutex).unwrap()
|
rx.recv().unwrap()
|
||||||
.into_inner().unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, path: &str) -> Result<Value, Error> {
|
pub fn get(&self, path: &str) -> Result<Value, Error> {
|
||||||
|
|
Loading…
Reference in New Issue