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
use std::{borrow::Cow, fmt::Debug};
use nyquest_interface::{Method as MethodImpl, Request as RequestImpl};
use crate::body::Body;
/// The Request Method (VERB)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Method {
inner: MethodImpl,
}
/// A request generic over async or blocking stream.
pub struct Request<S> {
pub(crate) inner: RequestImpl<S>,
}
impl Method {
/// Constructs a method from a string.
pub fn custom(method: impl Into<Cow<'static, str>>) -> Self {
Self {
inner: MethodImpl::Other(method.into()),
}
}
/// Constructs a `GET` method.
pub fn get() -> Self {
Self {
inner: MethodImpl::Get,
}
}
/// Constructs a `POST` method.
pub fn post() -> Self {
Self {
inner: MethodImpl::Post,
}
}
/// Constructs a `PUT` method.
pub fn put() -> Self {
Self {
inner: MethodImpl::Put,
}
}
/// Constructs a `DELETE` method.
pub fn delete() -> Self {
Self {
inner: MethodImpl::Delete,
}
}
/// Constructs a `PATCH` method.
pub fn patch() -> Self {
Self {
inner: MethodImpl::Patch,
}
}
/// Constructs a `HEAD` method.
pub fn head() -> Self {
Self {
inner: MethodImpl::Head,
}
}
}
impl<S> Request<S> {
/// Constructs a request with the given method and a relative or absolute URI.
///
/// If the URI is relative, it will be resolved against the [`crate::ClientBuilder::base_url`]
/// option that was used to create the client, if specified.
pub fn new(method: Method, relative_uri: impl Into<Cow<'static, str>>) -> Self {
Self {
inner: RequestImpl {
method: method.inner,
relative_uri: relative_uri.into(),
additional_headers: vec![],
body: None,
},
}
}
/// Constructs a GET request with a relative or absolute URI.
///
/// See [`Request::new`] for more details.
pub fn get(uri: impl Into<Cow<'static, str>>) -> Self {
Self::new(Method::get(), uri)
}
/// Constructs a POST request with a relative or absolute URI.
///
/// See [`Request::new`] for more details.
pub fn post(uri: impl Into<Cow<'static, str>>) -> Self {
Self::new(Method::post(), uri)
}
/// Constructs a PUT request with a relative or absolute URI.
///
/// See [`Request::new`] for more details.
pub fn put(uri: impl Into<Cow<'static, str>>) -> Self {
Self::new(Method::put(), uri)
}
/// Constructs a DELETE request with a relative or absolute URI.
///
/// See [`Request::new`] for more details.
pub fn delete(uri: impl Into<Cow<'static, str>>) -> Self {
Self::new(Method::delete(), uri)
}
/// Constructs a PATCH request with a relative or absolute URI.
///
/// See [`Request::new`] for more details.
pub fn patch(uri: impl Into<Cow<'static, str>>) -> Self {
Self::new(Method::patch(), uri)
}
/// Constructs a HEAD request with a relative or absolute URI.
///
/// See [`Request::new`] for more details.
pub fn head(uri: impl Into<Cow<'static, str>>) -> Self {
Self::new(Method::head(), uri)
}
/// Attach a request header to the request.
pub fn with_header(
mut self,
name: impl Into<Cow<'static, str>>,
value: impl Into<Cow<'static, str>>,
) -> Self {
self.inner
.additional_headers
.push((name.into(), value.into()));
self
}
/// Set the request body of the request.
///
/// When called multiple times, the last call will override any previous body.
pub fn with_body(mut self, body: Body<S>) -> Self {
self.inner.body = Some(body.inner);
self
}
}
impl<S> Debug for Request<S>
where
RequestImpl<S>: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
impl<S> Clone for Request<S>
where
RequestImpl<S>: Clone,
{
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}