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;
12use std::future::Future;
13
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.
43pub trait AsyncClient: RestClient {
44 /// Send a REST query asynchronously.
45 fn rest_async(
46 &self,
47 request: RequestBuilder,
48 body: Vec<u8>,
49 ) -> impl Future<Output = Result<Response<Bytes>, ApiError<Self::Error>>> + Send;
50}