forked from eladbash/gitlab-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignore.rs
235 lines (208 loc) · 6.77 KB
/
ignore.rs
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use async_trait::async_trait;
use http::{header, Request};
use crate::api::{query, ApiError, AsyncClient, AsyncQuery, Client, Endpoint, Query};
/// A query modifier that ignores the data returned from an endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Ignore<E> {
endpoint: E,
}
/// Ignore the resulting data from an endpoint.
pub fn ignore<E>(endpoint: E) -> Ignore<E> {
Ignore {
endpoint,
}
}
impl<E, C> Query<(), C> for Ignore<E>
where
E: Endpoint,
C: Client,
{
fn query(&self, client: &C) -> Result<(), ApiError<C::Error>> {
let mut url = self
.endpoint
.url_base()
.endpoint_for(client, &self.endpoint.endpoint())?;
self.endpoint.parameters().add_to_url(&mut url);
let req = Request::builder()
.method(self.endpoint.method())
.uri(query::url_to_http_uri(url));
let (req, data) = if let Some((mime, data)) = self.endpoint.body()? {
let req = req.header(header::CONTENT_TYPE, mime);
(req, data)
} else {
(req, Vec::new())
};
let rsp = client.rest(req, data)?;
let status = rsp.status();
if !status.is_success() {
let v = if let Ok(v) = serde_json::from_slice(rsp.body()) {
v
} else {
return Err(ApiError::server_error(status, rsp.body()));
};
return Err(ApiError::from_gitlab(v));
} else if status == http::StatusCode::MOVED_PERMANENTLY {
return Err(ApiError::moved_permanently(
rsp.headers().get(http::header::LOCATION),
));
}
Ok(())
}
}
#[async_trait]
impl<E, C> AsyncQuery<(), C> for Ignore<E>
where
E: Endpoint + Sync,
C: AsyncClient + Sync,
{
async fn query_async(&self, client: &C) -> Result<(), ApiError<C::Error>> {
let mut url = self
.endpoint
.url_base()
.endpoint_for(client, &self.endpoint.endpoint())?;
self.endpoint.parameters().add_to_url(&mut url);
let req = Request::builder()
.method(self.endpoint.method())
.uri(query::url_to_http_uri(url));
let (req, data) = if let Some((mime, data)) = self.endpoint.body()? {
let req = req.header(header::CONTENT_TYPE, mime);
(req, data)
} else {
(req, Vec::new())
};
let rsp = client.rest_async(req, data).await?;
let status = rsp.status();
if !status.is_success() {
let v = if let Ok(v) = serde_json::from_slice(rsp.body()) {
v
} else {
return Err(ApiError::server_error(status, rsp.body()));
};
return Err(ApiError::from_gitlab(v));
} else if status == http::StatusCode::MOVED_PERMANENTLY {
return Err(ApiError::moved_permanently(
rsp.headers().get(http::header::LOCATION),
));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use http::StatusCode;
use serde_json::json;
use crate::api::endpoint_prelude::*;
use crate::api::{self, ApiError, AsyncQuery, Query};
use crate::test::client::{ExpectedUrl, SingleTestClient};
struct Dummy;
impl Endpoint for Dummy {
fn method(&self) -> Method {
Method::GET
}
fn endpoint(&self) -> Cow<'static, str> {
"dummy".into()
}
}
#[test]
fn test_gitlab_non_json_response() {
let endpoint = ExpectedUrl::builder().endpoint("dummy").build().unwrap();
let client = SingleTestClient::new_raw(endpoint, "not json");
api::ignore(Dummy).query(&client).unwrap()
}
#[tokio::test]
async fn test_gitlab_non_json_response_async() {
let endpoint = ExpectedUrl::builder().endpoint("dummy").build().unwrap();
let client = SingleTestClient::new_raw(endpoint, "not json");
api::ignore(Dummy).query_async(&client).await.unwrap()
}
#[test]
fn test_gitlab_error_bad_json() {
let endpoint = ExpectedUrl::builder()
.endpoint("dummy")
.status(StatusCode::NOT_FOUND)
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let err = api::ignore(Dummy).query(&client).unwrap_err();
if let ApiError::GitlabService {
status, ..
} = err
{
assert_eq!(status, http::StatusCode::NOT_FOUND);
} else {
panic!("unexpected error: {}", err);
}
}
#[test]
fn test_gitlab_error_detection() {
let endpoint = ExpectedUrl::builder()
.endpoint("dummy")
.status(StatusCode::NOT_FOUND)
.build()
.unwrap();
let client = SingleTestClient::new_json(
endpoint,
&json!({
"message": "dummy error message",
}),
);
let err = api::ignore(Dummy).query(&client).unwrap_err();
if let ApiError::Gitlab {
msg,
} = err
{
assert_eq!(msg, "dummy error message");
} else {
panic!("unexpected error: {}", err);
}
}
#[test]
fn test_gitlab_error_detection_legacy() {
let endpoint = ExpectedUrl::builder()
.endpoint("dummy")
.status(StatusCode::NOT_FOUND)
.build()
.unwrap();
let client = SingleTestClient::new_json(
endpoint,
&json!({
"error": "dummy error message",
}),
);
let err = api::ignore(Dummy).query(&client).unwrap_err();
if let ApiError::Gitlab {
msg,
} = err
{
assert_eq!(msg, "dummy error message");
} else {
panic!("unexpected error: {}", err);
}
}
#[test]
fn test_gitlab_error_detection_unknown() {
let endpoint = ExpectedUrl::builder()
.endpoint("dummy")
.status(StatusCode::NOT_FOUND)
.build()
.unwrap();
let err_obj = json!({
"bogus": "dummy error message",
});
let client = SingleTestClient::new_json(endpoint, &err_obj);
let err = api::ignore(Dummy).query(&client).unwrap_err();
if let ApiError::GitlabUnrecognized {
obj,
} = err
{
assert_eq!(obj, err_obj);
} else {
panic!("unexpected error: {}", err);
}
}
}