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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2019 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The Conjure HTTP client API.

use crate::private::{self, APPLICATION_JSON};
use async_trait::async_trait;
use bytes::Bytes;
use conjure_error::Error;
use conjure_serde::json;
use futures_core::Stream;
use http::header::CONTENT_TYPE;
use http::{HeaderValue, Request, Response};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::convert::TryFrom;
use std::fmt::Display;
use std::io::Write;
use std::pin::Pin;

#[allow(missing_docs)]
#[deprecated(note = "renamed to RequestBody", since = "3.5.0")]
pub type Body<'a, T> = RequestBody<'a, T>;

#[allow(missing_docs)]
#[deprecated(note = "renamed to AsyncRequestBody", since = "3.5.0")]
pub type AsyncBody<'a, T> = AsyncRequestBody<'a, T>;

/// A trait implemented by generated blocking client interfaces for a Conjure service.
pub trait Service<C> {
    /// Creates a new service wrapping an HTTP client.
    fn new(client: C) -> Self;
}

/// A trait implemented by generated async client interfaces for a Conjure service.
pub trait AsyncService<C> {
    /// Creates a new service wrapping an async HTTP client.
    fn new(client: C) -> Self;
}

/// Conjure-specific metadata about an endpoint.
///
/// This is included as an extension in all `Request`s passed to blocking and async Conjure clients.
#[derive(Clone)]
pub struct Endpoint {
    service: &'static str,
    version: Option<&'static str>,
    name: &'static str,
    path: &'static str,
}

impl Endpoint {
    /// Creates a new `Endpoint`.
    #[inline]
    pub fn new(
        service: &'static str,
        version: Option<&'static str>,
        name: &'static str,
        path: &'static str,
    ) -> Self {
        Endpoint {
            service,
            version,
            name,
            path,
        }
    }

    /// Returns the name of the service the endpoint is part of.
    #[inline]
    pub fn service(&self) -> &'static str {
        self.service
    }

    /// Returns the version of the Conjure definition defining the service, if known.
    #[inline]
    pub fn version(&self) -> Option<&'static str> {
        self.version
    }

    /// Returns the name of the endpoint.
    #[inline]
    pub fn name(&self) -> &'static str {
        self.name
    }

    /// Returns the templated URI path of the endpoint.
    #[inline]
    pub fn path(&self) -> &'static str {
        self.path
    }
}

/// The body of a blocking Conjure request.
pub enum RequestBody<'a, W> {
    /// No body.
    Empty,
    /// A body already buffered in memory.
    Fixed(Bytes),
    /// A streaming body.
    Streaming(&'a mut dyn WriteBody<W>),
}

/// The body of an async Conjure request.
pub enum AsyncRequestBody<'a, W> {
    /// No body.
    Empty,
    /// A body already buffered in memory.
    Fixed(Bytes),
    /// A streaming body.
    Streaming(Pin<&'a mut (dyn AsyncWriteBody<W> + Send)>),
}

/// A trait implemented by HTTP client implementations.
pub trait Client {
    /// The client's binary request write type.
    type BodyWriter;
    /// The client's binary response body type.
    type ResponseBody: Iterator<Item = Result<Bytes, Error>>;

    /// Makes an HTTP request.
    ///
    /// The request's URI will be in absolute-form and it will always contain an `Endpoint` object in its extensions.
    ///
    /// A response must only be returned if it has a 2xx status code. The client is responsible for handling all other
    /// status codes (for example, converting a 5xx response into a service error). The client is also responsible for
    /// decoding the response body if necessary.
    fn send(
        &self,
        req: Request<RequestBody<'_, Self::BodyWriter>>,
    ) -> Result<Response<Self::ResponseBody>, Error>;
}

/// A trait implemented by async HTTP client implementations.
///
/// This trait can most easily be implemented with the [async-trait crate](https://docs.rs/async-trait).
#[async_trait]
pub trait AsyncClient {
    /// The client's binary request body write type.
    type BodyWriter;
    /// The client's binary response body type.
    type ResponseBody: Stream<Item = Result<Bytes, Error>>;

    /// Makes an HTTP request.
    ///
    /// The client is responsible for assembling the request URI. It is provided with the path template, unencoded path
    /// parameters, unencoded query parameters, header parameters, and request body.
    ///
    /// A response must only be returned if it has a 2xx status code. The client is responsible for handling all other
    /// status codes (for example, converting a 5xx response into a service error). The client is also responsible for
    /// decoding the response body if necessary.
    async fn send(
        &self,
        req: Request<AsyncRequestBody<'_, Self::BodyWriter>>,
    ) -> Result<Response<Self::ResponseBody>, Error>;
}

/// A trait implemented by streaming bodies.
pub trait WriteBody<W> {
    /// Writes the body out, in its entirety.
    ///
    /// Behavior is unspecified if this method is called twice without a successful call to `reset` in between.
    fn write_body(&mut self, w: &mut W) -> Result<(), Error>;

    /// Attempts to reset the body so that it can be written out again.
    ///
    /// Returns `true` if successful. Behavior is unspecified if this is not called after a call to `write_body`.
    fn reset(&mut self) -> bool;
}

impl<W> WriteBody<W> for &[u8]
where
    W: Write,
{
    fn write_body(&mut self, w: &mut W) -> Result<(), Error> {
        w.write_all(self).map_err(Error::internal_safe)
    }

    fn reset(&mut self) -> bool {
        true
    }
}

/// A trait implemented by async streaming bodies.
///
/// This trait can most easily be implemented with the [async-trait crate](https://docs.rs/async-trait).
///
/// # Examples
///
/// ```ignore
/// use async_trait::async_trait;
/// use conjure_error::Error;
/// use conjure_http::client::AsyncWriteBody;
/// use std::pin::Pin;
/// use tokio_io::{AsyncWrite, AsyncWriteExt};
///
/// pub struct SimpleBodyWriter;
///
/// #[async_trait]
/// impl<W> AsyncWriteBody<W> for SimpleBodyWriter
/// where
///     W: AsyncWrite + Send,
/// {
///     async fn write_body(self: Pin<&mut Self>, mut w: Pin<&mut W>) -> Result<(), Error> {
///         w.write_all(b"hello world").await.map_err(Error::internal_safe)
///     }
///
///     async fn reset(self: Pin<&mut Self>) -> bool
///     where
///         W: 'async_trait,
///     {
///         true
///     }
/// }
/// ```
#[async_trait]
pub trait AsyncWriteBody<W> {
    /// Writes the body out, in its entirety.
    ///
    /// Behavior is unspecified if this method is called twice without a successful call to `reset` in between.
    async fn write_body(self: Pin<&mut Self>, w: Pin<&mut W>) -> Result<(), Error>;

    /// Attempts to reset the body so that it can be written out again.
    ///
    /// Returns `true` if successful. Behavior is unspecified if this is not called after a call to `write_body`.
    async fn reset(self: Pin<&mut Self>) -> bool
    where
        W: 'async_trait;
}

/// A trait implemented by request body serializers used by custom Conjure client trait
/// implementations.
pub trait SerializeRequest<'a, T, W> {
    /// Returns the body's content type.
    fn content_type(value: &T) -> HeaderValue;

    /// Returns the body's length, if known.
    ///
    /// Empty and fixed size bodies will have their content length filled in automatically.
    ///
    /// The default implementation returns `None`.
    fn content_length(value: &T) -> Option<u64> {
        let _value = value;
        None
    }

    /// Serializes the body.
    fn serialize(value: T) -> Result<RequestBody<'a, W>, Error>;
}

/// A trait implemented by request body serializers used by custom async Conjure client trait
/// implementations.
pub trait AsyncSerializeRequest<'a, T, W> {
    /// Returns the body's content type.
    fn content_type(value: &T) -> HeaderValue;

    /// Returns the body's length, if known.
    ///
    /// Empty and fixed size bodies will have their content length filled in automatically.
    ///
    /// The default implementation returns `None`.
    fn content_length(value: &T) -> Option<u64> {
        let _value = value;
        None
    }

    /// Serializes the body.
    fn serialize(value: T) -> Result<AsyncRequestBody<'a, W>, Error>;
}

/// A body serializer which acts like a Conjure-generated client would.
pub enum ConjureRequestSerializer {}

impl<'a, T, W> SerializeRequest<'a, T, W> for ConjureRequestSerializer
where
    T: Serialize,
{
    fn content_type(_: &T) -> HeaderValue {
        APPLICATION_JSON
    }

    fn serialize(value: T) -> Result<RequestBody<'a, W>, Error> {
        let body = json::to_vec(&value).map_err(Error::internal)?;
        Ok(RequestBody::Fixed(body.into()))
    }
}

impl<'a, T, W> AsyncSerializeRequest<'a, T, W> for ConjureRequestSerializer
where
    T: Serialize,
{
    fn content_type(_: &T) -> HeaderValue {
        APPLICATION_JSON
    }

    fn serialize(value: T) -> Result<AsyncRequestBody<'a, W>, Error> {
        let buf = json::to_vec(&value).map_err(Error::internal)?;
        Ok(AsyncRequestBody::Fixed(Bytes::from(buf)))
    }
}

/// A trait implemented by response deserializers used by custom Conjure client trait
/// implementations.
pub trait DeserializeResponse<T, R> {
    /// Returns the value of the `Accept` header to be included in the request.
    fn accept() -> Option<HeaderValue>;

    /// Deserializes the response.
    fn deserialize(response: Response<R>) -> Result<T, Error>;
}

/// A trait implemented by response deserializers used by custom async Conjure client trait
/// implementations.
#[async_trait]
pub trait AsyncDeserializeResponse<T, R> {
    /// Returns the value of the `Accept` header to be included in the request.
    fn accept() -> Option<HeaderValue>;

    /// Deserializes the response.
    async fn deserialize(response: Response<R>) -> Result<T, Error>;
}

/// A response deserializer which acts like a Conjure-generated client would.
pub enum ConjureResponseDeserializer {}

impl<T, R> DeserializeResponse<T, R> for ConjureResponseDeserializer
where
    T: DeserializeOwned,
    R: Iterator<Item = Result<Bytes, Error>>,
{
    fn accept() -> Option<HeaderValue> {
        Some(APPLICATION_JSON)
    }

    fn deserialize(response: Response<R>) -> Result<T, Error> {
        if response.headers().get(CONTENT_TYPE) != Some(&APPLICATION_JSON) {
            return Err(Error::internal_safe("invalid response Content-Type"));
        }
        let buf = private::read_body(response.into_body(), None)?;
        json::client_from_slice(&buf).map_err(Error::internal)
    }
}

#[async_trait]
impl<T, R> AsyncDeserializeResponse<T, R> for ConjureResponseDeserializer
where
    T: DeserializeOwned,
    R: Stream<Item = Result<Bytes, Error>> + 'static + Send,
{
    fn accept() -> Option<HeaderValue> {
        Some(APPLICATION_JSON)
    }

    async fn deserialize(response: Response<R>) -> Result<T, Error> {
        if response.headers().get(CONTENT_TYPE) != Some(&APPLICATION_JSON) {
            return Err(Error::internal_safe("invalid response Content-Type"));
        }
        let buf = private::async_read_body(response.into_body(), None).await?;
        json::client_from_slice(&buf).map_err(Error::internal)
    }
}

/// A trait implemented by header encoders used by custom Conjure client trait implementations.
pub trait EncodeHeader<T> {
    /// Encodes the value into headers.
    ///
    /// In almost all cases a single `HeaderValue` should be returned.
    fn encode(value: T) -> Result<Vec<HeaderValue>, Error>;
}

/// A header encoder which converts values via their `Display` implementation.
pub enum DisplayHeaderEncoder {}

impl<T> EncodeHeader<T> for DisplayHeaderEncoder
where
    T: Display,
{
    fn encode(value: T) -> Result<Vec<HeaderValue>, Error> {
        HeaderValue::try_from(value.to_string())
            .map_err(Error::internal_safe)
            .map(|v| vec![v])
    }
}

/// A header encoder which converts a sequence of values via their individual `Display`
/// implementations.
pub enum DisplaySeqHeaderEncoder {}

impl<T, U> EncodeHeader<T> for DisplaySeqHeaderEncoder
where
    T: IntoIterator<Item = U>,
    U: Display,
{
    fn encode(value: T) -> Result<Vec<HeaderValue>, Error> {
        value
            .into_iter()
            .map(|v| HeaderValue::try_from(v.to_string()).map_err(Error::internal_safe))
            .collect()
    }
}

/// A trait implemented by URL parameter encoders used by custom Conjure client trait
/// implementations.
pub trait EncodeParam<T> {
    /// Encodes the value into a sequence of parameters.
    ///
    /// When used with a path parameter, each returned string will be a separate path component.
    /// When used with a query parameter, each returned string will be the value of a separate query
    /// entry.
    fn encode(value: T) -> Result<Vec<String>, Error>;
}

/// A param encoder which converts values via their `Display` implementations.
pub enum DisplayParamEncoder {}

impl<T> EncodeParam<T> for DisplayParamEncoder
where
    T: Display,
{
    fn encode(value: T) -> Result<Vec<String>, Error> {
        Ok(vec![value.to_string()])
    }
}

/// A param encoder which converts a sequence of values via their individual `Display`
/// implementations.
pub enum DisplaySeqParamEncoder {}

impl<T, U> EncodeParam<T> for DisplaySeqParamEncoder
where
    T: IntoIterator<Item = U>,
    U: Display,
{
    fn encode(value: T) -> Result<Vec<String>, Error> {
        Ok(value.into_iter().map(|v| v.to_string()).collect())
    }
}