tower_api_client/
request.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
use hyper::{header::HeaderMap, Method};
use serde::{Deserialize, Deserializer, Serialize};
use std::borrow::Cow;

/// Additional data to be sent along with the request.
pub enum RequestData<T> {
    /// No additional data.
    Empty,
    /// HTTP form data.
    Form(T),
    /// JSON data.
    Json(T),
    /// Query data.
    Query(T),
}

impl<T> Default for RequestData<T> {
    fn default() -> Self {
        RequestData::Empty
    }
}

/// The base-trait for requests sent by the client. The trait specifies the full life-cycle of the
/// request, including the endpoint, headers, data, method and eventual response.
pub trait Request: Send {
    /// The type of additional data sent with the request. Usually, this will be `()` or `Self`.
    type Data: Serialize;
    /// The type of the response from the server.
    type Response: for<'de> Deserialize<'de>;

    /// The HTTP method for the request.
    const METHOD: Method = Method::GET;

    /// The endpoint to which the request will be sent. The base url is set in the client, and the
    /// endpoint method returns the specific resource endpoint.
    fn endpoint(&self) -> Cow<str>;

    /// Any additional headers that should be sent with the request. Note that common headers such
    /// as authorization headers should be set on the client directly.
    fn headers(&self) -> HeaderMap {
        Default::default()
    }

    /// The formatted request data.
    fn data(&self) -> RequestData<&Self::Data> {
        Default::default()
    }
}

#[derive(Debug)]
/// Struct symbolizing an empty response from the server.
pub struct EmptyResponse;
impl<'de> Deserialize<'de> for EmptyResponse {
    fn deserialize<D>(_deserializer: D) -> Result<EmptyResponse, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(EmptyResponse {})
    }
}