generic_async_http_client/
response.rs1use crate::{imp, Error, HeaderName, HeaderValue};
2use serde::de::DeserializeOwned;
3use std::convert::TryInto;
4
5pub struct Response(pub(crate) imp::Resp);
11impl Response {
12 pub fn status_code(&self) -> u16 {
14 self.0.status()
15 }
16 pub fn status(&self) -> &str {
18 self.0.status_str()
19 }
20 pub async fn json<D: DeserializeOwned>(&mut self) -> Result<D, Error> {
22 self.0.json().await
23 }
24 pub async fn content(&mut self) -> Result<Vec<u8>, Error> {
26 self.0.bytes().await
27 }
28 pub async fn text(&mut self) -> Result<String, Error> {
30 self.0.string().await
31 }
32 pub fn header(
34 &self,
35 name: impl TryInto<HeaderName, Error = imp::Error>,
36 ) -> Option<&HeaderValue> {
37 match name.try_into() {
38 Err(_) => None,
39 Ok(name) => self.0.get_header(name.into()).map(|v| v.into()),
40 }
41 }
42 pub fn headers(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)> {
44 self.0.header_iter().map(|(n, v)| (n.into(), v.into()))
45 }
46 }
52
53impl std::fmt::Debug for Response {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 let h: Vec<(&HeaderName, &HeaderValue)> = self.headers().collect();
56 write!(f, "HTTP {} Header: {:?}", self.status_code(), h)
57 }
58}