From 1f776ddc54166b8d3cd59489a92e5bba429d004d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Vestergaard=20V=C3=A6rum?= Date: Wed, 31 Mar 2021 16:43:34 +0200 Subject: [PATCH] Bug fix: It was not possible to use FileGetCall to download binary files --- src/mako/api/lib/mbuild.mako | 28 ++++++++++------------------ src/rust/api/client.rs | 8 ++++++++ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/mako/api/lib/mbuild.mako b/src/mako/api/lib/mbuild.mako index 3b4cd32bfc..bd48ce45a2 100644 --- a/src/mako/api/lib/mbuild.mako +++ b/src/mako/api/lib/mbuild.mako @@ -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::(&res_body_string).ok(); let server_error = json::from_str::(&res_body_string) .or_else(|_| json::from_str::(&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::(&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); diff --git a/src/rust/api/client.rs b/src/rust/api/client.rs index 405f6838fb..4960b38642 100644 --- a/src/rust/api/client.rs +++ b/src/rust/api/client.rs @@ -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() +}