HttpClient

Trait HttpClient 

Source
pub trait HttpClient {
    // Required methods
    fn get_http_client(&self) -> &Client;
    fn get_base_url(&self) -> &str;

    // Provided methods
    fn request(
        &self,
        method: Method,
        path: &str,
        body: impl Into<Body> + Send,
        options: Vec<HttpRequestOption>,
    ) -> impl Future<Output = Result<Response, ClientError>> + Send
       where Self: Sync { ... }
    fn request_json<T>(
        &self,
        method: Method,
        path: &str,
        body: impl Into<Body> + Send,
        options: Vec<HttpRequestOption>,
    ) -> impl Future<Output = Result<T, ClientError>> + Send
       where Self: Sync,
             T: DeserializeOwned + Debug { ... }
    fn before_request(
        &self,
        req_builder: RequestBuilder,
        _path: &str,
        _options: Vec<HttpRequestOption>,
    ) -> impl Future<Output = Result<RequestBuilder, ClientError>> + Send { ... }
    fn after_request(
        &self,
        response: Response,
    ) -> impl Future<Output = Result<Response, ClientError>> + Send { ... }
}
Expand description

Provides basic HTTP Client capabilities.

Implement this in your struct when building a custom HTTP client.

The methods before_request and after_request can be used to alter the request / response of the request method.

Required Methods§

Source

fn get_http_client(&self) -> &Client

Return the underlying instance of reqwest::Client.

§Example
use drupal_kit::http_client::HttpClient;

struct MyHttpClient {
    http_client: reqwest::Client,
}

impl MyHttpClient {
    pub fn new() -> Self {
        let http_client = reqwest::Client::new();

        Self {
            http_client,
        }
    }
}

impl HttpClient for MyHttpClient {
    fn get_http_client(&self) -> &reqwest::Client {
        &self.http_client
    }

    fn get_base_url(&self) -> &str {
        todo!()
    }
}
Source

fn get_base_url(&self) -> &str

Returns the baseurl used for every request, unless explicitly set with HttpRequestOption::BaseUrl.

Provided Methods§

Source

fn request( &self, method: Method, path: &str, body: impl Into<Body> + Send, options: Vec<HttpRequestOption>, ) -> impl Future<Output = Result<Response, ClientError>> + Send
where Self: Sync,

Make an HTTP request.

The URL is constructed using the base url from HttpRequestOption::BaseUrlor self.get_base_url(). The given path is appended to the base url to produce the full request URL.

Source

fn request_json<T>( &self, method: Method, path: &str, body: impl Into<Body> + Send, options: Vec<HttpRequestOption>, ) -> impl Future<Output = Result<T, ClientError>> + Send
where Self: Sync, T: DeserializeOwned + Debug,

The same as request but deserializes json response body into a struct.

Source

fn before_request( &self, req_builder: RequestBuilder, _path: &str, _options: Vec<HttpRequestOption>, ) -> impl Future<Output = Result<RequestBuilder, ClientError>> + Send

Modify the request before being sent.

Using the given reqwest::RequestBuilder you can add headers and do other stuff to the request.

Source

fn after_request( &self, response: Response, ) -> impl Future<Output = Result<Response, ClientError>> + Send

Modify a successful response.

This method alters the reqwest::Response returned from the request method.

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§