gen_api_wrapper/
client.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6//
7// Originally from https://gitlab.kitware.com/utils/rust-gitlab
8//
9// Modified in an attempt to make it general beyond just gitlab
10
11use std::error::Error;
12
13use async_trait::async_trait;
14use bytes::Bytes;
15use http::request::Builder as RequestBuilder;
16use http::Response;
17use url::Url;
18
19use crate::error::ApiError;
20
21/// A trait representing a client which can communicate with a generic instance via REST.
22pub trait RestClient {
23    /// The errors which may occur for this client.
24    type Error: Error + Send + Sync + 'static;
25
26    /// Get the URL for the endpoint for the client.
27    ///
28    /// This method adds the hostname for the client's target instance.
29    fn rest_endpoint(&self, endpoint: &str) -> Result<Url, ApiError<Self::Error>>;
30}
31
32/// A trait representing a client which can communicate with a generic instance.
33pub trait Client: RestClient {
34    /// Send a REST query.
35    fn rest(
36        &self,
37        request: RequestBuilder,
38        body: Vec<u8>,
39    ) -> Result<Response<Bytes>, ApiError<Self::Error>>;
40}
41
42/// A trait representing an asynchronous client which can communicate with a generic instance.
43#[async_trait]
44pub trait AsyncClient: RestClient {
45    /// Send a REST query asynchronously.
46    async fn rest_async(
47        &self,
48        request: RequestBuilder,
49        body: Vec<u8>,
50    ) -> Result<Response<Bytes>, ApiError<Self::Error>>;
51}