Bug fix: It was not possible to use FileGetCall to download binary files

This commit is contained in:
Dennis Vestergaard Værum
2021-03-31 16:43:34 +02:00
committed by Dennis Vestergaard Værum
parent d71efba45b
commit 1f776ddc54
2 changed files with 18 additions and 18 deletions

View File

@@ -811,25 +811,15 @@ else {
return Err(client::Error::HttpError(err))
}
Ok(mut res) => {
let (res_parts, res_body) = res.into_parts();
let res_body_string: String = String::from_utf8(
hyper::body::to_bytes(res_body)
.await
.unwrap()
.into_iter()
.collect(),
)
.unwrap();
let reconstructed_result =
hyper::Response::from_parts(res_parts, res_body_string.clone().into());
if !res.status().is_success() {
let res_body_string = client::get_body_as_string(res.body_mut()).await;
if !reconstructed_result.status().is_success() {
let json_server_error = json::from_str::<client::JsonServerError>(&res_body_string).ok();
let server_error = json::from_str::<client::ServerError>(&res_body_string)
.or_else(|_| json::from_str::<client::ErrorResponse>(&res_body_string).map(|r| r.error))
.ok();
if let client::Retry::After(d) = dlg.http_failure(&reconstructed_result,
if let client::Retry::After(d) = dlg.http_failure(&res,
json_server_error,
server_error) {
sleep(d);
@@ -837,7 +827,7 @@ else {
}
${delegate_finish}(false);
return match json::from_str::<client::ErrorResponse>(&res_body_string){
Err(_) => Err(client::Error::Failure(reconstructed_result)),
Err(_) => Err(client::Error::Failure(res)),
Ok(serr) => Err(client::Error::BadRequest(serr))
}
}
@@ -846,7 +836,7 @@ else {
${READER_SEEK | indent_all_but_first_by(6)}
let mut client = &mut *self.hub.client.borrow_mut();
let upload_result = {
let url_str = &reconstructed_result.headers().get("Location").expect("LOCATION header is part of protocol").to_str().unwrap();
let url_str = &res.headers().get("Location").expect("LOCATION header is part of protocol").to_str().unwrap();
if upload_url_from_server {
dlg.store_upload_url(Some(url_str));
}
@@ -895,8 +885,10 @@ else {
if enable_resource_parsing \
% endif
{
let res_body_string = client::get_body_as_string(res.body_mut()).await;
match json::from_str(&res_body_string) {
Ok(decoded) => (reconstructed_result, decoded),
Ok(decoded) => (res, decoded),
Err(err) => {
dlg.response_json_decode_error(&res_body_string, &err);
return Err(client::Error::JsonDecodeError(res_body_string, err));
@@ -904,11 +896,11 @@ if enable_resource_parsing \
}
}\
% if supports_download:
else { (reconstructed_result, Default::default()) }\
else { (res, Default::default()) }\
% endif
;
% else:
let result_value = reconstructed_result;
let result_value = res;
% endif
${delegate_finish}(true);

View File

@@ -12,6 +12,7 @@ use hyper;
use hyper::header::{HeaderMap, AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE, USER_AGENT};
use hyper::Method;
use hyper::StatusCode;
use hyper::body::Buf;
use mime::{Attr, Mime, SubLevel, TopLevel, Value};
use oauth2;
@@ -804,3 +805,10 @@ pub fn remove_json_null_values(value: &mut json::value::Value) {
_ => {}
}
}
// Borrowing the body object as mutable and converts it to a string
pub async fn get_body_as_string(res_body: &mut hyper::Body) -> String {
let res_body_buf = hyper::body::aggregate(res_body).await.unwrap();
let res_body_string = String::from_utf8_lossy(res_body_buf.chunk());
res_body_string.to_string()
}