From 5a494f351d78b79a91d39a25f6d10a1237f9f86b Mon Sep 17 00:00:00 2001 From: Ivan Ukhov Date: Thu, 3 Oct 2024 11:46:08 +0200 Subject: [PATCH] Introduce to_response --- google-apis-common/src/lib.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/google-apis-common/src/lib.rs b/google-apis-common/src/lib.rs index 01b762bd58..3ac8521a55 100644 --- a/google-apis-common/src/lib.rs +++ b/google-apis-common/src/lib.rs @@ -33,7 +33,10 @@ pub type Body = http_body_util::Full; pub type Response = hyper::Response; /// A client. -pub trait Client: hyper_util::client::legacy::connect::Connect + Clone + Send + Sync + 'static {} +pub trait Client: + hyper_util::client::legacy::connect::Connect + Clone + Send + Sync + 'static +{ +} pub enum Retry { /// Signal you don't want to retry @@ -716,7 +719,7 @@ where let success = parts.status.is_success(); let bytes = to_bytes(body).await.unwrap_or_default(); let error = if !success { to_json(&bytes) } else { None }; - let response = Response::from_parts(parts, to_body(bytes)); + let response = to_response(parts, bytes); if !success { if let Retry::After(d) = @@ -756,12 +759,18 @@ pub fn remove_json_null_values(value: &mut json::value::Value) { } #[doc(hidden)] -pub fn to_body>(bytes: T) -> Body { +pub fn to_body(bytes: T) -> Body +where + T: Into, +{ Body::new(bytes.into()) } #[doc(hidden)] -pub async fn to_bytes(body: T) -> Option { +pub async fn to_bytes(body: T) -> Option +where + T: hyper::body::Body, +{ use http_body_util::BodyExt; body.collect().await.ok().map(|value| value.to_bytes()) } @@ -771,6 +780,14 @@ pub fn to_json(bytes: &hyper::body::Bytes) -> Option { json::from_str(&String::from_utf8_lossy(&bytes)).ok() } +#[doc(hidden)] +pub fn to_response(parts: http::response::Parts, body: T) -> Response +where + T: Into, +{ + Response::from_parts(parts, to_body(body)) +} + #[cfg(test)] mod test_api { use super::*;