gen_api_wrapper/query.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//
11
12use async_trait::async_trait;
13use http::Uri;
14use url::Url;
15
16use crate::{
17 client::{AsyncClient, Client},
18 error::ApiError,
19};
20
21pub fn url_to_http_uri(url: Url) -> Uri {
22 url.as_str()
23 .parse::<Uri>()
24 .expect("failed to parse a url::Url as an http::Uri")
25}
26
27/// A trait which represents a query which may be made to a client.
28pub trait Query<T, C>
29where
30 C: Client,
31{
32 /// Perform the query against the client.
33 fn query(&self, client: &C) -> Result<T, ApiError<C::Error>>;
34}
35
36/// A trait which represents an asynchronous query which may be made to a client.
37#[async_trait]
38pub trait AsyncQuery<T, C>
39where
40 C: AsyncClient,
41{
42 /// Perform the query asynchronously against the client.
43 async fn query_async(&self, client: &C) -> Result<T, ApiError<C::Error>>;
44}