Trait ApiCaller

Source
pub trait ApiCaller {
    const BASE_URL: &'static str;

    // Provided methods
    fn request<P, O>(payload: P) -> Request<P, O, ()> 
       where P: Payload,
             O: DeserializeOwned { ... }
    fn stream<P>(payload: P) -> Request<P, RespStream, ((),)> 
       where P: Payload { ... }
    fn client() -> Client { ... }
}
Expand description

Define a API caller

§Example

use api_req::{ApiCaller, RedirectPolicy};
use reqwest::header;

#[derive(ApiCaller)]
#[api_req(
    base_url = "http://example.com",
    default_headers = (("k1", "v1"), (header::ORIGIN, "v2")),
    default_headers_env = (("k3", "API_KEY"),),  // header value from env; `,` is essential in tuple
    redirect = RedirectPolicy::none()
)]
struct ExampleApi;

Provide the root URL in protocol://domain[:port]/api/ format (e.g., https://example.com/api/) as base_url.

Valid: https://api.service.com, http://localhost:8080/api/.

Invalid: https://example.com/api will be treated as https://example.com, use https://example.com/api/ instead.

Required Associated Constants§

Source

const BASE_URL: &'static str

The baseurl of the API

Provided Methods§

Source

fn request<P, O>(payload: P) -> Request<P, O, ()>

return a request future that can be awaited

Source

fn stream<P>(payload: P) -> Request<P, RespStream, ((),)>
where P: Payload,

Available on crate feature stream only.

return a stream future that can be awaited

Source

fn client() -> Client

return a client

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§