Commit 7fb170ef authored by boxdot's avatar boxdot Committed by Ben Boeckel
Browse files

Add traits for async query and client.

parent 695a470b
......@@ -15,7 +15,14 @@ edition = "2018"
[features]
default = ["client_api"]
client_api = ["itertools", "percent-encoding", "reqwest", "thiserror", "graphql_client"]
client_api = [
"itertools",
"percent-encoding",
"reqwest",
"thiserror",
"graphql_client",
"async-trait",
]
[dependencies]
base64 = "~0.12"
......@@ -25,6 +32,7 @@ log = "~0.4"
percent-encoding = { version = "^2.0", optional = true }
reqwest = { version = "~0.10.5", features = ["blocking", "json"], optional = true }
thiserror = { version = "^1.0.2", optional = true }
async-trait = { version = "~0.1", optional = true }
bytes = "~0.5"
chrono = { version = "~0.4", features = ["serde"] }
......
......@@ -76,6 +76,7 @@ pub mod users;
pub(crate) mod helpers;
pub use self::client::AsyncClient;
pub use self::client::Client;
pub use self::endpoint::Endpoint;
......@@ -97,6 +98,7 @@ pub use self::params::FormParams;
pub use self::params::ParamValue;
pub use self::params::QueryParams;
pub use self::query::AsyncQuery;
pub use self::query::Query;
pub use self::raw::raw;
......
......@@ -6,6 +6,7 @@
use std::error::Error;
use async_trait::async_trait;
use bytes::Bytes;
use http::request::Builder as RequestBuilder;
use http::Response;
......@@ -30,3 +31,22 @@ pub trait Client {
body: Vec<u8>,
) -> Result<Response<Bytes>, ApiError<Self::Error>>;
}
/// A trait representing an asynchronous client which can communicate with a GitLab instance.
#[async_trait]
pub trait AsyncClient {
/// The errors which may occur for this client.
type Error: Error + Send + Sync + 'static;
/// Get the URL for the endpoint for the client.
///
/// This method adds the hostname for the client's target instance.
fn rest_endpoint(&self, endpoint: &str) -> Result<Url, ApiError<Self::Error>>;
/// Send a REST query asynchronously.
async fn rest_async(
&self,
request: RequestBuilder,
body: Vec<u8>,
) -> Result<Response<Bytes>, ApiError<Self::Error>>;
}
......@@ -4,10 +4,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use async_trait::async_trait;
use http::Uri;
use url::Url;
use crate::api::{ApiError, Client};
use crate::api::{ApiError, AsyncClient, Client};
pub fn url_to_http_uri(url: Url) -> Uri {
url.as_str()
......@@ -23,3 +24,13 @@ where
/// Perform the query against the client.
fn query(&self, client: &C) -> Result<T, ApiError<C::Error>>;
}
/// A trait which represents an asynchronous query which may be made to a GitLab client.
#[async_trait]
pub trait AsyncQuery<T, C>
where
C: AsyncClient,
{
/// Perform the query asynchronously against the client.
async fn query_async(&self, client: &C) -> Result<T, ApiError<C::Error>>;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment