use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
#[cfg(any(feature = "response", feature = "response_std"))]
pub use product_os_http::{response::Parts, Response, StatusCode};
#[cfg(any(feature = "response", feature = "response_std"))]
pub struct ProductOSResponse<DRes: product_os_http_body::Body> {
response_url: String,
pub(crate) response_async: Option<Response<DRes>>,
}
#[cfg(any(feature = "response", feature = "response_std"))]
impl<DRes: product_os_http_body::Body> ProductOSResponse<DRes> {
#[deprecated(
since = "0.0.54",
note = "Use ProductOSResponse::from_parts() or ProductOSResponse::from_response() instead, which preserve status codes and headers"
)]
pub fn new<D>(r: DRes, url: String) -> Self {
Self {
response_url: url,
response_async: Some(Response::new(r)),
}
}
pub fn from_body(body: DRes, url: String) -> Self {
Self {
response_url: url,
response_async: Some(Response::new(body)),
}
}
pub fn from_response(response: Response<DRes>, url: String) -> Self {
Self {
response_url: url,
response_async: Some(response),
}
}
pub fn from_parts(parts: Parts, body: DRes, url: String) -> Self {
Self {
response_url: url,
response_async: Some(Response::from_parts(parts, body)),
}
}
#[deprecated(
since = "0.0.54",
note = "Use try_parts() instead which returns Option"
)]
#[allow(clippy::unwrap_used)]
pub fn parts(self) -> (Parts, DRes) {
self.response_async.unwrap().into_parts()
}
pub fn try_parts(self) -> Option<(Parts, DRes)> {
self.response_async.map(|r| r.into_parts())
}
#[deprecated(
since = "0.0.54",
note = "Use try_status() instead which returns Option"
)]
#[allow(clippy::unwrap_used)]
pub fn status(&self) -> StatusCode {
self.response_async.as_ref().unwrap().status()
}
pub fn try_status(&self) -> Option<StatusCode> {
self.response_async.as_ref().map(|r| r.status())
}
pub fn status_code(&self) -> u16 {
self.try_status().map(|s| s.as_u16()).unwrap_or(0)
}
#[deprecated(
since = "0.0.54",
note = "Use try_get_headers() instead which returns Option"
)]
#[allow(clippy::unwrap_used)]
pub fn get_headers(&self) -> BTreeMap<String, String> {
let mut map = BTreeMap::new();
let headers = self.response_async.as_ref().unwrap().headers();
for (name, value) in headers {
map.insert(name.to_string(), value.to_str().unwrap().to_string());
}
map
}
pub fn try_get_headers(&self) -> Option<BTreeMap<String, String>> {
self.response_async.as_ref().map(|r| {
let mut map = BTreeMap::new();
for (name, value) in r.headers() {
if let Ok(v) = value.to_str() {
map.insert(name.to_string(), v.to_string());
}
}
map
})
}
pub fn response_url(&self) -> &str {
&self.response_url
}
pub fn into_body(self) -> Option<DRes> {
self.response_async.map(|response| response.into_body())
}
}