1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! The various states of a request.
//!
//! A request has a specific lifecycle:
//! - a [`RequestBuilder`] is created using one of [`Request::get`],
//! [`Request::post`] and so on,
//! - a [`Request`] is created by calling
//! [`with_body`](RequestBuilder::with_body) on the [`RequestBuilder`],
//! - the [`Request`] is passed as argument of
//! [`Context::run`](crate::Context), returning a [`RequestResult`],
//! - the final request body is constructed by calling
//! [`expect_status`](RequestResult::expect_status).
//!
//! The documentation for [`Request`] provide more specific description.
use HashMap;
use StatusCode;
use Response;
use ;
use crateIntoUrl;
/// An HTTP request we're about to run.
///
/// # Creating a request
///
/// First a [`RequestBuilder`] must be created. This object will allow to
/// encode the request information. It can be created with [`Request::get`]
/// or [`Request::post`], depending on the kind of request needed.
///
/// Then, various request metadata can be encoded in the builder once it is
/// created. For instance, one can use the
/// [`with_header`](RequestBuilder::with_header) method to specify a header key
/// to the request.
///
/// Once the metadata is encoded, the [`with_body`](RequestBuilder::with_body)
/// method allows to specify a body and create the final [`Request`] object.
///
/// The following code snippet shows all these three steps:
///
/// ```rust
/// use restest::Request;
///
/// use serde::Serialize;
///
/// let request = Request::get("users") // Creating the builder...
/// .with_header("token", "mom-said-yes") // ... Adding metadata to the builder
/// .with_body(GetUsersFilter::All); // ... Adding a body, building the final Request.
///
/// #[derive(Serialize)]
/// enum GetUsersFilter {
/// All,
/// YoungerThan(u8),
/// OlderThan(u8),
/// }
/// ```
///
/// # Running a request
///
/// Once the [`Request`] has been successfully created, it can be run by using
/// the [`Context::run`](crate::Context::run) method.
/// Allows encode metadata in order to create a [`Request`].
///
/// This type can be created by calling either [`Request::get`] or
/// [`Request::post`]. Specifically, this type allows to encode the request
/// header with [`RequestBuilder::with_header`], and to create the final
/// [`Request`] type by calling [`RequestBuilder::with_body`].
///
/// This allows to create [`Request`] types, as shown in the following example:
///
/// ```rust
/// use restest::Request;
///
/// use serde::Serialize;
///
/// let request = Request::get("user")
/// .with_header("token", "mom-said-yes")
/// .with_body(GetUserRequest {
/// login: String::from("jdoe")
/// });
///
/// #[derive(Serialize)]
/// struct GetUserRequest {
/// login: String,
/// }
/// ```
pub
/// The data returned by the server once the request is performed.
///
/// This datatype is meant for intermediary representation. It can be converted
/// to a concrete type by calling [`RequestResult::expect_status`].