pub trait RequestHandler<B> {
    type Successful;
    type Unsuccessful;
    type BuildError;

    // Required methods
    fn build_request(
        &self,
        builder: RequestBuilder,
        request_body: &Option<B>,
        attempt_count: u8
    ) -> Result<Request, Self::BuildError>;
    fn handle_response(
        &self,
        status: StatusCode,
        headers: HeaderMap,
        response_body: Bytes
    ) -> Result<Self::Successful, Self::Unsuccessful>;

    // Provided method
    fn request_config(&self) -> RequestConfig { ... }
}
Expand description

A trait which is used to process requests and responses for the Client.

Required Associated Types§

source

type Successful

The type which is returned to the caller of Client::request() when the response was successful.

source

type Unsuccessful

The type which is returned to the caller of Client::request() when the response was unsuccessful.

source

type BuildError

The type that represents an error occurred in build_request().

Required Methods§

source

fn build_request( &self, builder: RequestBuilder, request_body: &Option<B>, attempt_count: u8 ) -> Result<Request, Self::BuildError>

Build a HTTP request to be sent.

Implementors have to decide how to include the request_body into the builder. Implementors can also perform other operations (such as authorization) on the request.

source

fn handle_response( &self, status: StatusCode, headers: HeaderMap, response_body: Bytes ) -> Result<Self::Successful, Self::Unsuccessful>

Handle a HTTP response before it is returned to the caller of Client::request().

You can verify, parse, etc… the response here before it is returned to the caller.

Examples
fn handle_response(&self, status: StatusCode, _: HeaderMap, response_body: Bytes) -> Result<String, ()> {
    if status.is_success() {
        let body = std::str::from_utf8(&response_body).expect("body should be valid UTF-8").to_owned();
        Ok(body)
    } else {
        Err(())
    }
}

Provided Methods§

source

fn request_config(&self) -> RequestConfig

Returns a RequestConfig that will be used to send a HTTP reqeust.

Implementors§