Centralize body extraction
This commit is contained in:
parent
45102409d5
commit
2047a9d579
|
@ -20,6 +20,7 @@ ring = ["dep:ring", "hyper-rustls?/ring", "rcgen/ring"]
|
|||
[dependencies]
|
||||
aws-lc-rs = { version = "1.8.0", optional = true }
|
||||
base64 = "0.21.0"
|
||||
bytes = "1"
|
||||
http = "1"
|
||||
http-body-util = "0.1.2"
|
||||
hyper = { version = "1.3.1", features = ["client", "http1", "http2"] }
|
||||
|
|
|
@ -133,11 +133,7 @@ impl Order {
|
|||
.await?;
|
||||
|
||||
self.nonce = nonce_from_response(&rsp);
|
||||
let body = Problem::from_response(rsp)
|
||||
.await?
|
||||
.collect()
|
||||
.await?
|
||||
.to_bytes();
|
||||
let body = Problem::from_response(rsp).await?;
|
||||
Ok(Some(
|
||||
String::from_utf8(body.to_vec())
|
||||
.map_err(|_| "unable to decode certificate as UTF-8")?,
|
||||
|
|
17
src/types.rs
17
src/types.rs
|
@ -1,6 +1,7 @@
|
|||
use std::fmt;
|
||||
|
||||
use base64::prelude::{Engine, BASE64_URL_SAFE_NO_PAD};
|
||||
use bytes::Bytes;
|
||||
use http_body_util::BodyExt;
|
||||
use hyper::body::Incoming;
|
||||
use hyper::Response;
|
||||
|
@ -134,20 +135,16 @@ pub struct Problem {
|
|||
|
||||
impl Problem {
|
||||
pub(crate) async fn check<T: DeserializeOwned>(rsp: Response<Incoming>) -> Result<T, Error> {
|
||||
Ok(serde_json::from_slice(
|
||||
&Self::from_response(rsp).await?.collect().await?.to_bytes(),
|
||||
)?)
|
||||
Ok(serde_json::from_slice(&Self::from_response(rsp).await?)?)
|
||||
}
|
||||
|
||||
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 body = rsp.into_body();
|
||||
if status.is_informational() || status.is_success() || status.is_redirection() {
|
||||
return Ok(body);
|
||||
let body = rsp.into_body().collect().await?.to_bytes();
|
||||
match status.is_informational() || status.is_success() || status.is_redirection() {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue