1
1
//! HTTP response components
2
2
3
3
use crate :: error:: Error ;
4
- use reqwest:: header:: HeaderMap ;
5
- use reqwest:: StatusCode ;
4
+ use crate :: http:: { headers:: HeaderMap , Method , StatusCode , Url } ;
6
5
use serde:: de:: DeserializeOwned ;
7
6
8
7
/// A response from Elasticsearch
9
- pub struct Response ( reqwest:: Response ) ;
8
+ pub struct Response ( reqwest:: Response , Method ) ;
10
9
11
10
impl Response {
12
11
/// Creates a new instance of an Elasticsearch response
13
- pub fn new ( response : reqwest:: Response ) -> Self {
14
- Self ( response)
12
+ pub fn new ( response : reqwest:: Response , method : Method ) -> Self {
13
+ Self ( response, method )
15
14
}
16
15
17
- /// The HTTP status code of the response
18
- pub fn status_code ( & self ) -> StatusCode {
19
- self . 0 . status ( )
16
+ /// Get the response content-length, if known.
17
+ ///
18
+ /// Reasons it may not be known:
19
+ ///
20
+ /// - The server didn't send a `content-length` header.
21
+ /// - The response is compressed and automatically decoded (thus changing
22
+ /// the actual decoded length).
23
+ pub fn content_length ( & self ) -> Option < u64 > {
24
+ self . 0 . content_length ( )
25
+ }
26
+
27
+ /// Gets the response content-type.
28
+ pub fn content_type ( & self ) -> & str {
29
+ self . 0
30
+ . headers ( )
31
+ . get ( crate :: http:: headers:: CONTENT_TYPE )
32
+ . and_then ( |value| value. to_str ( ) . ok ( ) )
33
+ . unwrap ( )
20
34
}
21
35
22
- /// Turn the response into an ` Error` if Elasticsearch returned an error.
36
+ /// Turn the response into an [ Error] if Elasticsearch returned an error.
23
37
pub fn error_for_status_code ( self ) -> Result < Self , Error > {
24
38
match self . 0 . error_for_status_ref ( ) {
25
39
Ok ( _) => Ok ( self ) ,
26
40
Err ( err) => Err ( err. into ( ) ) ,
27
41
}
28
42
}
29
43
30
- /// Turn the response into an ` Error` if Elasticsearch returned an error.
44
+ /// Turn the response into an [ Error] if Elasticsearch returned an error.
31
45
pub fn error_for_status_code_ref ( & self ) -> Result < & Self , Error > {
32
46
match self . 0 . error_for_status_ref ( ) {
33
47
Ok ( _) => Ok ( self ) ,
34
48
Err ( err) => Err ( err. into ( ) ) ,
35
49
}
36
50
}
37
51
38
- /// The response headers
39
- pub fn headers ( & self ) -> & HeaderMap {
40
- self . 0 . headers ( )
41
- }
42
-
43
- /// Gets the Deprecation warning response headers
44
- ///
45
- /// Deprecation headers signal the use of Elasticsearch functionality
46
- /// or features that are deprecated and will be removed in a future release.
47
- pub fn warning_headers ( & self ) -> impl Iterator < Item = & str > {
48
- self . 0
49
- . headers ( )
50
- . get_all ( "Warning" )
51
- . iter ( )
52
- . map ( |w| w. to_str ( ) . unwrap ( ) )
53
- }
54
-
55
52
/// Asynchronously reads the response body as JSON
56
53
///
57
54
/// Reading the response body consumes `self`
@@ -63,11 +60,43 @@ impl Response {
63
60
Ok ( body)
64
61
}
65
62
63
+ /// Gets the response headers.
64
+ pub fn headers ( & self ) -> & HeaderMap {
65
+ self . 0 . headers ( )
66
+ }
67
+
68
+ /// Gets the request method.
69
+ pub fn method ( & self ) -> Method {
70
+ self . 1
71
+ }
72
+
73
+ /// Get the HTTP status code of the response
74
+ pub fn status_code ( & self ) -> StatusCode {
75
+ self . 0 . status ( )
76
+ }
77
+
66
78
/// Asynchronously reads the response body as plain text
67
79
///
68
80
/// Reading the response body consumes `self`
69
81
pub async fn text ( self ) -> Result < String , Error > {
70
82
let body = self . 0 . text ( ) . await ?;
71
83
Ok ( body)
72
84
}
85
+
86
+ /// Gets the request URL
87
+ pub fn url ( & self ) -> & Url {
88
+ self . 0 . url ( )
89
+ }
90
+
91
+ /// Gets the Deprecation warning response headers
92
+ ///
93
+ /// Deprecation headers signal the use of Elasticsearch functionality
94
+ /// or features that are deprecated and will be removed in a future release.
95
+ pub fn warning_headers ( & self ) -> impl Iterator < Item = & str > {
96
+ self . 0
97
+ . headers ( )
98
+ . get_all ( "Warning" )
99
+ . iter ( )
100
+ . map ( |w| w. to_str ( ) . unwrap ( ) )
101
+ }
73
102
}
0 commit comments