Skip to main content

api_builder/client/
mod.rs

1use core::future::Future;
2
3use bytes::Bytes;
4use http::{Request, Response};
5use url::Url;
6
7use crate::APIError;
8
9#[cfg(feature = "reqwest")]
10import!(reqwest);
11#[cfg(target_family = "wasm")]
12#[cfg(not(feature = "reqwest"))]
13import!(wasm);
14
15/// A trait representing a client which can communicate with an instance via REST.
16pub trait RestClient {
17    /// The errors which may occur for this client.
18    type Error;
19
20    /// Get the URL for the endpoint for the client.
21    ///
22    /// This method adds the hostname for the client's target instance.
23    fn rest_endpoint(&self, path: &str) -> Result<Url, APIError<Self::Error>>;
24}
25
26/// A trait representing a client.
27pub trait Client: RestClient {
28    /// Send a REST query.
29    fn rest(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, APIError<Self::Error>>;
30}
31
32/// A trait representing an asynchronous client.
33pub trait AsyncClient: RestClient {
34    #[cfg(not(target_arch = "wasm32"))]
35    /// Send a REST query asynchronously.
36    fn rest_async(
37        &self,
38        request: Request<Vec<u8>>,
39    ) -> impl Future<Output = Result<Response<Bytes>, APIError<Self::Error>>> + Send;
40
41    #[cfg(target_arch = "wasm32")]
42    /// Send a REST query asynchronously.
43    fn rest_async(
44        &self,
45        request: Request<Vec<u8>>,
46    ) -> impl Future<Output = Result<Response<Bytes>, APIError<Self::Error>>>;
47}