gitlab/api/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
7use std::error::Error;
8
9use async_trait::async_trait;
10use bytes::Bytes;
11use http::request::Builder as RequestBuilder;
12use http::Response;
13use url::Url;
14
15use crate::api::{ApiError, UrlBase};
16
17/// A trait representing a client which can communicate with a GitLab instance via REST.
18pub trait RestClient {
19 /// The errors which may occur for this client.
20 type Error: Error + Send + Sync + 'static;
21
22 /// Get the URL for a REST v4 endpoint for the client.
23 ///
24 /// This method adds the hostname for the client's target instance.
25 fn rest_endpoint(&self, endpoint: &str) -> Result<Url, ApiError<Self::Error>>;
26
27 /// Get the URL for an instance endpoint for the client.
28 fn instance_endpoint(&self, endpoint: &str) -> Result<Url, ApiError<Self::Error>> {
29 let _ = endpoint;
30 Err(ApiError::unsupported_url_base(UrlBase::Instance))
31 }
32}
33
34/// A trait representing a client which can communicate with a GitLab instance.
35pub trait Client: RestClient {
36 /// Send a REST query.
37 fn rest(
38 &self,
39 request: RequestBuilder,
40 body: Vec<u8>,
41 ) -> Result<Response<Bytes>, ApiError<Self::Error>>;
42}
43
44/// A trait representing an asynchronous client which can communicate with a GitLab instance.
45#[async_trait]
46pub trait AsyncClient: RestClient {
47 /// Send a REST query asynchronously.
48 async fn rest_async(
49 &self,
50 request: RequestBuilder,
51 body: Vec<u8>,
52 ) -> Result<Response<Bytes>, ApiError<Self::Error>>;
53}