Centralize body extraction

This commit is contained in:
Dirkjan Ochtman 2024-07-16 11:32:02 +02:00
parent 45102409d5
commit 2047a9d579
3 changed files with 9 additions and 15 deletions

View File

@ -20,6 +20,7 @@ ring = ["dep:ring", "hyper-rustls?/ring", "rcgen/ring"]
[dependencies] [dependencies]
aws-lc-rs = { version = "1.8.0", optional = true } aws-lc-rs = { version = "1.8.0", optional = true }
base64 = "0.21.0" base64 = "0.21.0"
bytes = "1"
http = "1" http = "1"
http-body-util = "0.1.2" http-body-util = "0.1.2"
hyper = { version = "1.3.1", features = ["client", "http1", "http2"] } hyper = { version = "1.3.1", features = ["client", "http1", "http2"] }

View File

@ -133,11 +133,7 @@ impl Order {
.await?; .await?;
self.nonce = nonce_from_response(&rsp); self.nonce = nonce_from_response(&rsp);
let body = Problem::from_response(rsp) let body = Problem::from_response(rsp).await?;
.await?
.collect()
.await?
.to_bytes();
Ok(Some( Ok(Some(
String::from_utf8(body.to_vec()) String::from_utf8(body.to_vec())
.map_err(|_| "unable to decode certificate as UTF-8")?, .map_err(|_| "unable to decode certificate as UTF-8")?,

View File

@ -1,6 +1,7 @@
use std::fmt; use std::fmt;
use base64::prelude::{Engine, BASE64_URL_SAFE_NO_PAD}; use base64::prelude::{Engine, BASE64_URL_SAFE_NO_PAD};
use bytes::Bytes;
use http_body_util::BodyExt; use http_body_util::BodyExt;
use hyper::body::Incoming; use hyper::body::Incoming;
use hyper::Response; use hyper::Response;
@ -134,20 +135,16 @@ pub struct Problem {
impl Problem { impl Problem {
pub(crate) async fn check<T: DeserializeOwned>(rsp: Response<Incoming>) -> Result<T, Error> { pub(crate) async fn check<T: DeserializeOwned>(rsp: Response<Incoming>) -> Result<T, Error> {
Ok(serde_json::from_slice( Ok(serde_json::from_slice(&Self::from_response(rsp).await?)?)
&Self::from_response(rsp).await?.collect().await?.to_bytes(),
)?)
} }
pub(crate) async fn from_response(rsp: Response<Incoming>) -> Result<Incoming, Error> { pub(crate) async fn from_response(rsp: Response<Incoming>) -> Result<Bytes, Error> {
let status = rsp.status(); let status = rsp.status();
let body = rsp.into_body(); let body = rsp.into_body().collect().await?.to_bytes();
if status.is_informational() || status.is_success() || status.is_redirection() { match status.is_informational() || status.is_success() || status.is_redirection() {
return Ok(body); true => Ok(body),
false => Err(serde_json::from_slice::<Problem>(&body)?.into()),
} }
let body = body.collect().await?.to_bytes();
Err(serde_json::from_slice::<Problem>(&body)?.into())
} }
} }