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§
Sourcetype Successful
type Successful
The type which is returned to the caller of Client::request() when the response was successful.
Sourcetype Unsuccessful
type Unsuccessful
The type which is returned to the caller of Client::request() when the response was unsuccessful.
Sourcetype BuildError
type BuildError
The type that represents an error occurred in build_request().
Required Methods§
Sourcefn build_request(
&self,
builder: RequestBuilder,
request_body: &Option<B>,
attempt_count: u8,
) -> Result<Request, Self::BuildError>
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.
Sourcefn handle_response(
&self,
status: StatusCode,
headers: HeaderMap,
response_body: Bytes,
) -> Result<Self::Successful, Self::Unsuccessful>
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§
Sourcefn request_config(&self) -> RequestConfig
fn request_config(&self) -> RequestConfig
Returns a RequestConfig that will be used to send a HTTP reqeust.