src/client/http_client.rs - BackupClient: use async
This commit is contained in:
parent
d4a085e564
commit
2a1e6d7dea
@ -575,36 +575,38 @@ impl BackupClient {
|
|||||||
Arc::new(Self { h2, canceller })
|
Arc::new(Self { h2, canceller })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(
|
pub async fn get(
|
||||||
&self,
|
&self,
|
||||||
path: &str,
|
path: &str,
|
||||||
param: Option<Value>,
|
param: Option<Value>,
|
||||||
) -> impl Future<Output = Result<Value, Error>> {
|
) -> Result<Value, Error> {
|
||||||
self.h2.get(path, param)
|
self.h2.get(path, param).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn put(
|
pub async fn put(
|
||||||
&self,
|
&self,
|
||||||
path: &str,
|
path: &str,
|
||||||
param: Option<Value>,
|
param: Option<Value>,
|
||||||
) -> impl Future<Output = Result<Value, Error>> {
|
) -> Result<Value, Error> {
|
||||||
self.h2.put(path, param)
|
self.h2.put(path, param).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post(
|
pub async fn post(
|
||||||
&self,
|
&self,
|
||||||
path: &str,
|
path: &str,
|
||||||
param: Option<Value>,
|
param: Option<Value>,
|
||||||
) -> impl Future<Output = Result<Value, Error>> {
|
) -> Result<Value, Error> {
|
||||||
self.h2.post(path, param)
|
self.h2.post(path, param).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn finish(self: Arc<Self>) -> impl Future<Output = Result<(), Error>> {
|
pub async fn finish(self: Arc<Self>) -> Result<(), Error> {
|
||||||
self.h2.clone()
|
let h2 = self.h2.clone();
|
||||||
.post("finish", None)
|
|
||||||
|
h2.post("finish", None)
|
||||||
.map_ok(move |_| {
|
.map_ok(move |_| {
|
||||||
self.canceller.cancel();
|
self.canceller.cancel();
|
||||||
})
|
})
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn force_close(self) {
|
pub fn force_close(self) {
|
||||||
@ -1090,88 +1092,88 @@ impl H2Client {
|
|||||||
Self { h2 }
|
Self { h2 }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Output = Result<Value, Error>> {
|
pub async fn get(
|
||||||
|
&self,
|
||||||
|
path: &str,
|
||||||
|
param: Option<Value>
|
||||||
|
) -> Result<Value, Error> {
|
||||||
let req = Self::request_builder("localhost", "GET", path, param).unwrap();
|
let req = Self::request_builder("localhost", "GET", path, param).unwrap();
|
||||||
self.request(req)
|
self.request(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn put(&self, path: &str, param: Option<Value>) -> impl Future<Output = Result<Value, Error>> {
|
pub async fn put(
|
||||||
|
&self,
|
||||||
|
path: &str,
|
||||||
|
param: Option<Value>
|
||||||
|
) -> Result<Value, Error> {
|
||||||
let req = Self::request_builder("localhost", "PUT", path, param).unwrap();
|
let req = Self::request_builder("localhost", "PUT", path, param).unwrap();
|
||||||
self.request(req)
|
self.request(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post(&self, path: &str, param: Option<Value>) -> impl Future<Output = Result<Value, Error>> {
|
pub async fn post(
|
||||||
|
&self,
|
||||||
|
path: &str,
|
||||||
|
param: Option<Value>
|
||||||
|
) -> Result<Value, Error> {
|
||||||
let req = Self::request_builder("localhost", "POST", path, param).unwrap();
|
let req = Self::request_builder("localhost", "POST", path, param).unwrap();
|
||||||
self.request(req)
|
self.request(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn download<W: Write + Send>(
|
pub async fn download<W: Write + Send>(
|
||||||
&self,
|
&self,
|
||||||
path: &str,
|
path: &str,
|
||||||
param: Option<Value>,
|
param: Option<Value>,
|
||||||
output: W,
|
mut output: W,
|
||||||
) -> Result<W, Error> {
|
) -> Result<W, Error> {
|
||||||
let request = Self::request_builder("localhost", "GET", path, param).unwrap();
|
let request = Self::request_builder("localhost", "GET", path, param).unwrap();
|
||||||
|
|
||||||
self.send_request(request, None)
|
let response_future = self.send_request(request, None).await?;
|
||||||
.and_then(move |response| {
|
|
||||||
response
|
|
||||||
.map_err(Error::from)
|
|
||||||
.and_then(move |resp| {
|
|
||||||
let status = resp.status();
|
|
||||||
if !status.is_success() {
|
|
||||||
future::Either::Left(
|
|
||||||
H2Client::h2api_response(resp)
|
|
||||||
.map(|_| Err(format_err!("unknown error")))
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
let mut body = resp.into_body();
|
|
||||||
let release_capacity = body.release_capacity().clone();
|
|
||||||
|
|
||||||
future::Either::Right(
|
let resp = response_future.await?;
|
||||||
body
|
|
||||||
.map_err(Error::from)
|
let status = resp.status();
|
||||||
.try_fold(output, move |mut acc, chunk| {
|
if !status.is_success() {
|
||||||
let mut release_capacity = release_capacity.clone();
|
H2Client::h2api_response(resp)
|
||||||
async move {
|
.map(|_| Err(format_err!("unknown error")))
|
||||||
let _ = release_capacity.release_capacity(chunk.len());
|
.await?;
|
||||||
acc.write_all(&chunk)?;
|
unreachable!();
|
||||||
Ok::<_, Error>(acc)
|
}
|
||||||
}
|
|
||||||
})
|
let mut body = resp.into_body();
|
||||||
)
|
let mut release_capacity = body.release_capacity().clone();
|
||||||
}
|
|
||||||
})
|
while let Some(chunk) = body.try_next().await? {
|
||||||
})
|
let _ = release_capacity.release_capacity(chunk.len());
|
||||||
.await
|
output.write_all(&chunk)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn upload(
|
pub async fn upload(
|
||||||
&self,
|
&self,
|
||||||
path: &str,
|
path: &str,
|
||||||
param: Option<Value>,
|
param: Option<Value>,
|
||||||
data: Vec<u8>,
|
data: Vec<u8>,
|
||||||
) -> impl Future<Output = Result<Value, Error>> {
|
) -> Result<Value, Error> {
|
||||||
let request = Self::request_builder("localhost", "POST", path, param).unwrap();
|
let request = Self::request_builder("localhost", "POST", path, param).unwrap();
|
||||||
|
|
||||||
self.h2.clone()
|
let mut send_request = self.h2.clone().ready().await?;
|
||||||
.ready()
|
|
||||||
.map_err(Error::from)
|
let (response, stream) = send_request.send_request(request, false).unwrap();
|
||||||
.and_then(move |mut send_request| {
|
PipeToSendStream::new(bytes::Bytes::from(data), stream)
|
||||||
let (response, stream) = send_request.send_request(request, false).unwrap();
|
.and_then(|_| {
|
||||||
PipeToSendStream::new(bytes::Bytes::from(data), stream)
|
response
|
||||||
.and_then(|_| {
|
.map_err(Error::from)
|
||||||
response
|
.and_then(Self::h2api_response)
|
||||||
.map_err(Error::from)
|
|
||||||
.and_then(Self::h2api_response)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn request(
|
async fn request(
|
||||||
&self,
|
&self,
|
||||||
request: Request<()>,
|
request: Request<()>,
|
||||||
) -> impl Future<Output = Result<Value, Error>> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
self.send_request(request, None)
|
self.send_request(request, None)
|
||||||
.and_then(move |response| {
|
.and_then(move |response| {
|
||||||
@ -1179,6 +1181,7 @@ impl H2Client {
|
|||||||
.map_err(Error::from)
|
.map_err(Error::from)
|
||||||
.and_then(Self::h2api_response)
|
.and_then(Self::h2api_response)
|
||||||
})
|
})
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_request(
|
fn send_request(
|
||||||
|
Loading…
Reference in New Issue
Block a user