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