macro_rules! client {
(
$(#[$client_meta:meta])*
$client_vis:vis struct $Client:ident<B, T: $($(hapic::)?transport::)?Transport<B>>;
$(#[$trait_meta:meta])*
$trait_vis:vis trait $ApiCall:ident;
) => { ... };
}Expand description
Macro for generating the client and ApiCall type.
If you’re defining JSON API calls at the same time, you can use json_api instead.
For example:
hapic::client!(
pub struct Client<B, T: Transport<B>>;
pub trait ApiCall;
);Creates:
use hapic::RawApiCall;
use hapic::transport::Transport;
pub struct Client<B, T: Transport<B>> {
pub client: hapic::Client<B, T>
}
pub trait ApiCall: RawApiCall {}The Client type has, by default, two impls:
use hapic::{Error, RawApiCall};
use hapic::transport::HttpsTransport;
pub trait ApiCall: RawApiCall {}
impl Client<hyper::Body, HttpsTransport> {
/// Create a new client to the provided endpoint, using `hyper` and `HttpsTransport`.
pub fn new(endpoint: std::borrow::Cow<'static, str>) -> Self {
Client {
client: hapic::Client::new_https_client(endpoint),
}
}
}
impl<B: Send + Sync, T: Transport<B>> Client<B, T> {
pub async fn call<C>(
&self,
api_call: C,
) -> std::result::Result<C::Output, Error>
where
C: ApiCall + Send,
B: From<<C as RawApiCall>::RequestBody>,
{
RawApiCall::request(api_call, &self.client).await
}
}