1- use crate :: error:: { ErrorKind , ResultExt } ;
2- use crate :: headers:: Headers ;
3- use crate :: StatusCode ;
1+ use crate :: {
2+ error:: { ErrorKind , ResultExt } ,
3+ from_json,
4+ headers:: Headers ,
5+ StatusCode ,
6+ } ;
47use bytes:: Bytes ;
58use futures:: { Stream , StreamExt } ;
6- use std :: fmt :: Debug ;
7- use std:: pin:: Pin ;
9+ use serde :: de :: DeserializeOwned ;
10+ use std:: { fmt :: Debug , pin:: Pin } ;
811
912#[ cfg( not( target_arch = "wasm32" ) ) ]
1013pub ( crate ) type PinnedStream = Pin < Box < dyn Stream < Item = crate :: Result < Bytes > > + Send + Sync > > ;
@@ -46,6 +49,21 @@ impl Response {
4649 pub fn into_body ( self ) -> ResponseBody {
4750 self . body
4851 }
52+
53+ pub async fn json < T > ( self ) -> crate :: Result < T >
54+ where
55+ T : DeserializeOwned ,
56+ {
57+ self . into_body ( ) . json ( ) . await
58+ }
59+
60+ #[ cfg( feature = "xml" ) ]
61+ pub async fn xml < T > ( self ) -> crate :: Result < T >
62+ where
63+ T : DeserializeOwned ,
64+ {
65+ self . into_body ( ) . xml ( ) . await
66+ }
4967}
5068
5169impl std:: fmt:: Debug for Response {
@@ -66,6 +84,12 @@ pub struct CollectedResponse {
6684 body : Bytes ,
6785}
6886
87+ impl AsRef < [ u8 ] > for CollectedResponse {
88+ fn as_ref ( & self ) -> & [ u8 ] {
89+ self . body . as_ref ( )
90+ }
91+ }
92+
6993impl CollectedResponse {
7094 /// Create a new instance
7195 pub fn new ( status : StatusCode , headers : Headers , body : Bytes ) -> Self {
@@ -97,6 +121,21 @@ impl CollectedResponse {
97121 let body = body. collect ( ) . await ?;
98122 Ok ( Self :: new ( status, headers, body) )
99123 }
124+
125+ pub fn json < T > ( & self ) -> crate :: Result < T >
126+ where
127+ T : DeserializeOwned ,
128+ {
129+ from_json ( & self . body )
130+ }
131+
132+ #[ cfg( feature = "xml" ) ]
133+ pub async fn xml < T > ( & self ) -> crate :: Result < T >
134+ where
135+ T : DeserializeOwned ,
136+ {
137+ crate :: xml:: read_xml ( & self . body )
138+ }
100139}
101140
102141/// A response body stream
@@ -130,6 +169,23 @@ impl ResponseBody {
130169 )
131170 . map ( ToOwned :: to_owned)
132171 }
172+
173+ pub async fn json < T > ( self ) -> crate :: Result < T >
174+ where
175+ T : DeserializeOwned ,
176+ {
177+ let body = self . collect ( ) . await ?;
178+ from_json ( body)
179+ }
180+
181+ #[ cfg( feature = "xml" ) ]
182+ pub async fn xml < T > ( self ) -> crate :: Result < T >
183+ where
184+ T : DeserializeOwned ,
185+ {
186+ let body = self . collect ( ) . await ?;
187+ crate :: xml:: read_xml ( & body)
188+ }
133189}
134190
135191impl Stream for ResponseBody {
0 commit comments