Trait api_client::Api

source ·
pub trait Api {
    fn client(&self) -> &Client;

    fn pre_request(&self, request: RequestBuilder) -> Result<RequestBuilder> { ... }
}
Expand description

The main API trait.

If you need custom behavior, such as authentication, you should implement this trait on your custom struct. See the Api::pre_request method for more details.

Otherwise, you can use the api macro to generate a struct with a proper implementation of this trait.

Required Methods§

Returns a reference to a reqwest Client to create requests.

Provided Methods§

You can use this method to modify the request before sending it.

Some good examples of usage are:

  • Authentication
  • Custom headers (can also be done with a method on Client)
Authentication
use api_client::{api, Api};
use reqwest::{Client, RequestBuilder};

struct ExampleApi {
    client: Client,
    username: String,
    password: String
}

impl Api for ExampleApi {
    fn client(&self) -> &Client {
        &self.client
    }

    fn pre_request(&self, request: RequestBuilder) -> reqwest::Result<RequestBuilder> {
        Ok(request.basic_auth(&self.username, Some(&self.password)))
    }
}

impl ExampleApi {
    api! {
        fn example() -> String {
           GET "https://example.com"
        }
    }
}

Implementors§