-
Notifications
You must be signed in to change notification settings - Fork 51
/
progenitor_client.rs
403 lines (356 loc) · 10.6 KB
/
progenitor_client.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2022 Oxide Computer Company
#![allow(dead_code)]
//! Support code for generated clients.
use std::ops::{Deref, DerefMut};
use bytes::Bytes;
use futures_core::Stream;
use reqwest::RequestBuilder;
use serde::{de::DeserializeOwned, Serialize};
type InnerByteStream =
std::pin::Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send + Sync>>;
/// Untyped byte stream used for both success and error responses.
pub struct ByteStream(InnerByteStream);
impl ByteStream {
/// Creates a new ByteStream
///
/// Useful for generating test fixtures.
pub fn new(inner: InnerByteStream) -> Self {
Self(inner)
}
/// Consumes the [`ByteStream`] and return its inner [`Stream`].
pub fn into_inner(self) -> InnerByteStream {
self.0
}
}
impl Deref for ByteStream {
type Target = InnerByteStream;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for ByteStream {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
/// Typed value returned by generated client methods.
///
/// This is used for successful responses and may appear in error responses
/// generated from the server (see [`Error::ErrorResponse`])
pub struct ResponseValue<T> {
inner: T,
status: reqwest::StatusCode,
headers: reqwest::header::HeaderMap,
// TODO cookies?
}
impl<T: DeserializeOwned> ResponseValue<T> {
#[doc(hidden)]
pub async fn from_response<E: std::fmt::Debug>(
response: reqwest::Response,
) -> Result<Self, Error<E>> {
let status = response.status();
let headers = response.headers().clone();
let inner = response
.json()
.await
.map_err(Error::InvalidResponsePayload)?;
Ok(Self {
inner,
status,
headers,
})
}
}
impl ResponseValue<reqwest::Upgraded> {
#[doc(hidden)]
pub async fn upgrade<E: std::fmt::Debug>(
response: reqwest::Response,
) -> Result<Self, Error<E>> {
let status = response.status();
let headers = response.headers().clone();
if status == reqwest::StatusCode::SWITCHING_PROTOCOLS {
let inner = response
.upgrade()
.await
.map_err(Error::InvalidResponsePayload)?;
Ok(Self {
inner,
status,
headers,
})
} else {
Err(Error::UnexpectedResponse(response))
}
}
}
impl ResponseValue<ByteStream> {
#[doc(hidden)]
pub fn stream(response: reqwest::Response) -> Self {
let status = response.status();
let headers = response.headers().clone();
Self {
inner: ByteStream(Box::pin(response.bytes_stream())),
status,
headers,
}
}
}
impl ResponseValue<()> {
#[doc(hidden)]
pub fn empty(response: reqwest::Response) -> Self {
let status = response.status();
let headers = response.headers().clone();
// TODO is there anything we want to do to confirm that there is no
// content?
Self {
inner: (),
status,
headers,
}
}
}
impl<T> ResponseValue<T> {
/// Creates a [`ResponseValue`] from the inner type, status, and headers.
///
/// Useful for generating test fixtures.
pub fn new(
inner: T,
status: reqwest::StatusCode,
headers: reqwest::header::HeaderMap,
) -> Self {
Self {
inner,
status,
headers,
}
}
/// Consumes the ResponseValue, returning the wrapped value.
pub fn into_inner(self) -> T {
self.inner
}
/// Gets the status from this response.
pub fn status(&self) -> reqwest::StatusCode {
self.status
}
/// Gets the headers from this response.
pub fn headers(&self) -> &reqwest::header::HeaderMap {
&self.headers
}
/// Gets the parsed value of the Content-Length header, if present and
/// valid.
pub fn content_length(&self) -> Option<u64> {
self.headers
.get(reqwest::header::CONTENT_LENGTH)?
.to_str()
.ok()?
.parse::<u64>()
.ok()
}
#[doc(hidden)]
pub fn map<U: std::fmt::Debug, F, E>(
self,
f: F,
) -> Result<ResponseValue<U>, E>
where
F: FnOnce(T) -> U,
{
let Self {
inner,
status,
headers,
} = self;
Ok(ResponseValue {
inner: f(inner),
status,
headers,
})
}
}
impl ResponseValue<ByteStream> {
/// Consumes the `ResponseValue`, returning the wrapped [`Stream`].
pub fn into_inner_stream(self) -> InnerByteStream {
self.into_inner().into_inner()
}
}
impl<T> Deref for ResponseValue<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T> DerefMut for ResponseValue<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for ResponseValue<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}
/// Error produced by generated client methods.
///
/// The type parameter may be a struct if there's a single expected error type
/// or an enum if there are multiple valid error types. It can be the unit type
/// if there are no structured returns expected.
pub enum Error<E = ()> {
/// The request did not conform to API requirements.
InvalidRequest(String),
/// A server error either due to the data, or with the connection.
CommunicationError(reqwest::Error),
/// A documented, expected error response.
ErrorResponse(ResponseValue<E>),
/// An expected response code whose deserialization failed.
// TODO we have stuff from the response; should we include it?
InvalidResponsePayload(reqwest::Error),
/// A response not listed in the API description. This may represent a
/// success or failure response; check `status().is_success()`.
UnexpectedResponse(reqwest::Response),
}
impl<E> Error<E> {
/// Returns the status code, if the error was generated from a response.
pub fn status(&self) -> Option<reqwest::StatusCode> {
match self {
Error::InvalidRequest(_) => None,
Error::CommunicationError(e) => e.status(),
Error::ErrorResponse(rv) => Some(rv.status()),
Error::InvalidResponsePayload(e) => e.status(),
Error::UnexpectedResponse(r) => Some(r.status()),
}
}
/// Converts this error into one without a typed body.
///
/// This is useful for unified error handling with APIs that distinguish
/// various error response bodies.
pub fn into_untyped(self) -> Error {
match self {
Error::InvalidRequest(s) => Error::InvalidRequest(s),
Error::CommunicationError(e) => Error::CommunicationError(e),
Error::ErrorResponse(ResponseValue {
inner: _,
status,
headers,
}) => Error::ErrorResponse(ResponseValue {
inner: (),
status,
headers,
}),
Error::InvalidResponsePayload(e) => {
Error::InvalidResponsePayload(e)
}
Error::UnexpectedResponse(r) => Error::UnexpectedResponse(r),
}
}
}
impl<E> From<reqwest::Error> for Error<E> {
fn from(e: reqwest::Error) -> Self {
Self::CommunicationError(e)
}
}
impl<E> std::fmt::Display for Error<E>
where
ResponseValue<E>: ErrorFormat,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::InvalidRequest(s) => {
write!(f, "Invalid Request: {}", s)
}
Error::CommunicationError(e) => {
write!(f, "Communication Error: {}", e)
}
Error::ErrorResponse(rve) => {
write!(f, "Error Response: ")?;
rve.fmt_info(f)
}
Error::InvalidResponsePayload(e) => {
write!(f, "Invalid Response Payload: {}", e)
}
Error::UnexpectedResponse(r) => {
write!(f, "Unexpected Response: {:?}", r)
}
}
}
}
trait ErrorFormat {
fn fmt_info(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;
}
impl<E> ErrorFormat for ResponseValue<E>
where
E: std::fmt::Debug,
{
fn fmt_info(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"status: {}; headers: {:?}; value: {:?}",
self.status, self.headers, self.inner,
)
}
}
impl ErrorFormat for ResponseValue<ByteStream> {
fn fmt_info(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"status: {}; headers: {:?}; value: <stream>",
self.status, self.headers,
)
}
}
impl<E> std::fmt::Debug for Error<E>
where
ResponseValue<E>: ErrorFormat,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
impl<E> std::error::Error for Error<E>
where
ResponseValue<E>: ErrorFormat,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::CommunicationError(e) => Some(e),
Error::InvalidResponsePayload(e) => Some(e),
_ => None,
}
}
}
const PATH_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
.add(b' ')
.add(b'"')
.add(b'#')
.add(b'<')
.add(b'>')
.add(b'?')
.add(b'`')
.add(b'{')
.add(b'}');
#[doc(hidden)]
pub fn encode_path(pc: &str) -> String {
percent_encoding::utf8_percent_encode(pc, PATH_SET).to_string()
}
#[doc(hidden)]
pub trait RequestBuilderExt<E> {
fn form_urlencoded<T: Serialize + ?Sized>(
self,
body: &T,
) -> Result<RequestBuilder, Error<E>>;
}
impl<E> RequestBuilderExt<E> for RequestBuilder {
fn form_urlencoded<T: Serialize + ?Sized>(
self,
body: &T,
) -> Result<Self, Error<E>> {
Ok(self
.header(
reqwest::header::CONTENT_TYPE,
reqwest::header::HeaderValue::from_static(
"application/x-www-form-urlencoded",
),
)
.body(serde_urlencoded::to_string(&body).map_err(|_| {
Error::InvalidRequest("failed to serialize body".to_string())
})?))
}
}