pretend/
client.rs

1//! Client traits
2//!
3//! This module contains traits to be implemented by a `pretend` client. `pretend` clients
4//! implementations are responsible of executing the actual HTTP requests. Implementations
5//! delegates to other crates.
6//!
7//! `pretend` support 3 kind of clients:
8//!
9//! - [`Client`] for asynchronous clients
10//! - [`LocalClient`] for asynchronous local clients (clients that are not `Send` + `Sync`)
11//! - [`BlockingClient`] for blocking clients
12//!
13//! These traits wrap the underlying HTTP client and are responsible of executing the request
14//! via `execute`. This method takes a method, url, header and body (as raw bytes) and should
15//! return a response with raw bytes as body.
16//!
17//! Since this crate uses `async_trait` to support futures in trait, `Client`
18//! implementations should be marked with `#[client::async_trait]` and
19//! `LocalClient` should use `#[client::async_trait(?Send)]`.
20
21pub use async_trait::async_trait;
22pub use bytes::Bytes;
23pub use http::Method;
24
25use crate::{HeaderMap, Response, Result, Url};
26
27/// `pretend` client
28///
29/// See module level documentation for more information.
30#[async_trait]
31pub trait Client {
32    /// Execute a request
33    async fn execute(
34        &self,
35        method: Method,
36        url: Url,
37        headers: HeaderMap,
38        body: Option<Bytes>,
39    ) -> Result<Response<Bytes>>;
40}
41
42/// `pretend` local client
43///
44/// See module level documentation for more information.
45#[async_trait(?Send)]
46pub trait LocalClient {
47    /// Execute a request
48    async fn execute(
49        &self,
50        method: Method,
51        url: Url,
52        headers: HeaderMap,
53        body: Option<Bytes>,
54    ) -> Result<Response<Bytes>>;
55}
56
57/// `pretend` blocking client
58///
59/// See module level documentation for more information.
60pub trait BlockingClient {
61    /// Execute a request
62    fn execute(
63        &self,
64        method: Method,
65        url: Url,
66        headers: HeaderMap,
67        body: Option<Bytes>,
68    ) -> Result<Response<Bytes>>;
69}
70
71/// `pretend` local client
72///
73/// See module level documentation for more information.
74#[async_trait(?Send)]
75impl<C> LocalClient for C
76where
77    C: Client,
78{
79    async fn execute(
80        &self,
81        method: Method,
82        url: Url,
83        headers: HeaderMap,
84        body: Option<Bytes>,
85    ) -> Result<Response<Bytes>> {
86        Client::execute(self, method, url, headers, body).await
87    }
88}