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 Ok(self.0.json().await?)
23 }
24 pub async fn content(&mut self) -> Result<Vec<u8>, Error> {
26 Ok(self.0.bytes().await?)
27 }
28 pub async fn text(&mut self) -> Result<String, Error> {
30 Ok(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 all_header(
44 &self,
45 name: impl TryInto<HeaderName, Error = imp::Error>,
46 ) -> Result<impl Iterator<Item = &HeaderValue>, Error> {
47 let name: HeaderName = name.try_into()?;
48 Ok(self.0.get_headers(name.into()).map(|v| v.into()))
49 }
50 pub fn headers(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)> {
52 self.0.header_iter().map(|(n, v)| (n.into(), v.into()))
53 }
54 }
60
61impl std::fmt::Debug for Response {
62 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63 let h: Vec<(&HeaderName, &HeaderValue)> = self.headers().collect();
64 write!(f, "HTTP {} Header: {:?}", self.status_code(), h)
65 }
66}
67
68pub trait Responses {
69 fn status(&self) -> u16;
70 fn status_str(&self) -> &'static str;
71 async fn json<D: DeserializeOwned>(&mut self) -> Result<D, imp::Error>;
72 async fn bytes(&mut self) -> Result<Vec<u8>, imp::Error>;
73 async fn string(&mut self) -> Result<String, imp::Error>;
74 fn get_header(&self, name: imp::HeaderName) -> Option<&imp::HeaderValue>;
75 fn get_headers(&self, name: imp::HeaderName) -> impl Iterator<Item = &imp::HeaderValue>;
76 fn header_iter(&self) -> impl Iterator<Item = (&imp::HeaderName, &imp::HeaderValue)>;
77}