marketstack/api/
client.rs

1use std::error::Error;
2
3use async_trait::async_trait;
4use bytes::Bytes;
5use http::request::Builder as RequestBuilder;
6use http::Response;
7use url::Url;
8
9use crate::api::ApiError;
10use crate::auth::Auth;
11
12/// A trait representing a client which can communicate with a Marketstack instance via REST.
13pub trait RestClient {
14    /// The errors which may occur for this client.
15    type Error: Error + Send + Sync + 'static;
16
17    /// Get the URL for the endpoint for the client.
18    ///
19    /// This method adds the hostname for the client's target instance.
20    fn rest_endpoint(&self, endpoint: &str) -> Result<Url, ApiError<Self::Error>>;
21
22    /// Get the Auth token from the client.
23    fn get_auth(&self) -> Option<Auth>;
24}
25
26/// A trait representing a client which can communicate with a Marketstack instance.
27pub trait Client: RestClient {
28    /// Send a REST query.
29    fn rest(
30        &self,
31        request: RequestBuilder,
32        body: Vec<u8>,
33    ) -> Result<Response<Bytes>, ApiError<Self::Error>>;
34}
35
36/// A trait representing an asynchronous client which can communicate with a Marketstack instance.
37#[async_trait]
38pub trait AsyncClient: RestClient {
39    /// Send a REST query asynchronously.
40    async fn rest_async(
41        &self,
42        request: RequestBuilder,
43        body: Vec<u8>,
44    ) -> Result<Response<Bytes>, ApiError<Self::Error>>;
45}