Trait api_request_utils::RequestHandler
source · pub trait RequestHandler: RequestDefaults {
// Provided methods
fn request<'async_trait, T, E>(
request: RequestBuilder
) -> Pin<Box<dyn Future<Output = Result<T, RequestError<E>>> + Send + 'async_trait>>
where T: DeserializeOwned + 'async_trait,
E: DeserializeOwned + 'async_trait { ... }
fn resolve_error<T, E, O>(
&self,
response: Result<T, RequestError<E>>,
mapper: &CustomMapper<T, O>,
error_handler: &ErrorHandler<E>
) -> Option<O> { ... }
fn get_request_handler<'l, 'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait, T, E, O>(
&'life0 self,
endpoint: &'life1 str,
parameters: &'life2 ParameterHashMap<'l>,
error_handler: &'life3 ErrorHandler<E>,
mapper: &'life4 CustomMapper<T, O>
) -> Pin<Box<dyn Future<Output = Option<O>> + Send + 'async_trait>>
where T: DeserializeOwned + 'async_trait,
E: DeserializeOwned + 'async_trait,
O: 'async_trait,
Self: Sync + 'async_trait,
'l: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait { ... }
fn post_request_handler<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait, T, E, O>(
&'life0 self,
endpoint: &'life1 str,
json: &'life2 str,
error_handler: &'life3 ErrorHandler<E>,
mapper: &'life4 CustomMapper<T, O>
) -> Pin<Box<dyn Future<Output = Option<O>> + Send + 'async_trait>>
where T: DeserializeOwned + 'async_trait,
E: DeserializeOwned + 'async_trait,
O: 'async_trait,
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait { ... }
}
Expand description
A trait for handling HTTP requests.
Provided Methods§
sourcefn request<'async_trait, T, E>(
request: RequestBuilder
) -> Pin<Box<dyn Future<Output = Result<T, RequestError<E>>> + Send + 'async_trait>>where
T: DeserializeOwned + 'async_trait,
E: DeserializeOwned + 'async_trait,
fn request<'async_trait, T, E>( request: RequestBuilder ) -> Pin<Box<dyn Future<Output = Result<T, RequestError<E>>> + Send + 'async_trait>>where T: DeserializeOwned + 'async_trait, E: DeserializeOwned + 'async_trait,
Sends a request using the given RequestBuilder and handles the response.
Examples
#[tokio::main]
async fn main() {
let url = "https://api.example.com";
let request = reqwest::Client::new().get(url);
match request::<serde_json::Value, serde_json::Value>(request).await {
Ok(response) => {
println!("Response: {:?}", response);
}
Err(error) => {
match error {
RequestError::Internal(message) => {
eprintln!("Internal Error: {}", message);
}
RequestError::Json(error_data) => {
eprintln!("JSON Error: {:?}", error_data);
}
}
}
}
}
sourcefn resolve_error<T, E, O>(
&self,
response: Result<T, RequestError<E>>,
mapper: &CustomMapper<T, O>,
error_handler: &ErrorHandler<E>
) -> Option<O>
fn resolve_error<T, E, O>( &self, response: Result<T, RequestError<E>>, mapper: &CustomMapper<T, O>, error_handler: &ErrorHandler<E> ) -> Option<O>
Resolves the error in the response and returns an option containing the value or None
.
Arguments
response
- The response as aResult
type.error_resolver
- The closure that handles the error and performs custom error handling.
Returns
An option containing the value if the response is successful, otherwise None
.
Example
fn handle_error(error: &AuthErrorInfo) {
// Custom error handling logic
// ...
}
let response: Result<i32, AuthError> = /* Some API response */;
let result = resolve_error(&response, handle_error);
match result {
Some(value) => {
// Process the value
// ...
}
None => {
// Error occurred, handle accordingly
// ...
}
}
In the example above, the resolve_error
function takes a response
of type Result<T, E>
,
where T
represents the success type and E
represents the error type. It also accepts an
error_resolver
closure of type Fn(&E)
, which is responsible for handling the error and
performing custom error handling logic.
If the response is successful (Ok
variant), the function returns Some(value)
, containing
the value. If the response is an error (Err
variant), the error_resolver
closure is invoked
with the error as the argument, and None
is returned.
sourcefn get_request_handler<'l, 'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait, T, E, O>(
&'life0 self,
endpoint: &'life1 str,
parameters: &'life2 ParameterHashMap<'l>,
error_handler: &'life3 ErrorHandler<E>,
mapper: &'life4 CustomMapper<T, O>
) -> Pin<Box<dyn Future<Output = Option<O>> + Send + 'async_trait>>where
T: DeserializeOwned + 'async_trait,
E: DeserializeOwned + 'async_trait,
O: 'async_trait,
Self: Sync + 'async_trait,
'l: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
fn get_request_handler<'l, 'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait, T, E, O>( &'life0 self, endpoint: &'life1 str, parameters: &'life2 ParameterHashMap<'l>, error_handler: &'life3 ErrorHandler<E>, mapper: &'life4 CustomMapper<T, O> ) -> Pin<Box<dyn Future<Output = Option<O>> + Send + 'async_trait>>where T: DeserializeOwned + 'async_trait, E: DeserializeOwned + 'async_trait, O: 'async_trait, Self: Sync + 'async_trait, 'l: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait,
Handles a GET request to the specified endpoint with the provided parameters and returns the response data of type T
.
This asynchronous function constructs (by default) a GET request using the default_get_requestor
method with the given endpoint and parameters.
It then sends the request using the request method, expecting a response of type T
or an error of type E
.
The error is resolved using the resolve_error
method and returns an Option<T>
representing the response data if successful,
or None
if an error occurred.
Arguments
endpoint
- The endpoint URL to send the GET request to.parameters
- A hashmap containing any parameters to include in the request.
sourcefn post_request_handler<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait, T, E, O>(
&'life0 self,
endpoint: &'life1 str,
json: &'life2 str,
error_handler: &'life3 ErrorHandler<E>,
mapper: &'life4 CustomMapper<T, O>
) -> Pin<Box<dyn Future<Output = Option<O>> + Send + 'async_trait>>where
T: DeserializeOwned + 'async_trait,
E: DeserializeOwned + 'async_trait,
O: 'async_trait,
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
fn post_request_handler<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait, T, E, O>( &'life0 self, endpoint: &'life1 str, json: &'life2 str, error_handler: &'life3 ErrorHandler<E>, mapper: &'life4 CustomMapper<T, O> ) -> Pin<Box<dyn Future<Output = Option<O>> + Send + 'async_trait>>where T: DeserializeOwned + 'async_trait, E: DeserializeOwned + 'async_trait, O: 'async_trait, Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait,
Handles a POST request to the specified endpoint with the provided JSON payload and returns the response data of type T.
This asynchronous function constructs a POST request using the default_post_requestor
method with the given endpoint and json payload.
It then sends the request using the request method, expecting a response of type T
or an error of type E
The error is resolved using the resolve_error
method and returns an Option<T>
representing the response data if successful,
or None
if an error occurred.
Arguments
endpoint
- The endpoint URL to send the POST request to.json
- A string containing the JSON payload to include in the request.