tower_api_client/request.rs
1use hyper::{header::HeaderMap, Method};
2use serde::{Deserialize, Deserializer, Serialize};
3use std::borrow::Cow;
4
5/// Additional data to be sent along with the request.
6pub enum RequestData<T> {
7 /// No additional data.
8 Empty,
9 /// HTTP form data.
10 Form(T),
11 /// JSON data.
12 Json(T),
13 /// Query data.
14 Query(T),
15}
16
17impl<T> Default for RequestData<T> {
18 fn default() -> Self {
19 RequestData::Empty
20 }
21}
22
23/// The base-trait for requests sent by the client. The trait specifies the full life-cycle of the
24/// request, including the endpoint, headers, data, method and eventual response.
25pub trait Request: Send {
26 /// The type of additional data sent with the request. Usually, this will be `()` or `Self`.
27 type Data: Serialize;
28 /// The type of the response from the server.
29 type Response: for<'de> Deserialize<'de>;
30
31 /// The HTTP method for the request.
32 const METHOD: Method = Method::GET;
33
34 /// The endpoint to which the request will be sent. The base url is set in the client, and the
35 /// endpoint method returns the specific resource endpoint.
36 fn endpoint(&self) -> Cow<str>;
37
38 /// Any additional headers that should be sent with the request. Note that common headers such
39 /// as authorization headers should be set on the client directly.
40 fn headers(&self) -> HeaderMap {
41 Default::default()
42 }
43
44 /// The formatted request data.
45 fn data(&self) -> RequestData<&Self::Data> {
46 Default::default()
47 }
48}
49
50#[derive(Debug)]
51/// Struct symbolizing an empty response from the server.
52pub struct EmptyResponse;
53impl<'de> Deserialize<'de> for EmptyResponse {
54 fn deserialize<D>(_deserializer: D) -> Result<EmptyResponse, D::Error>
55 where
56 D: Deserializer<'de>,
57 {
58 Ok(EmptyResponse {})
59 }
60}