Trait RequestHandler

Source
pub trait RequestHandler<T: DeserializeOwned, O: DeserializeOwned, E: DeserializeOwned>: RequestDefaults {
    // Provided methods
    fn request_map<'async_trait>(
        request: RequestBuilder,
        map: impl 'async_trait + FnOnce(T) -> O + Send + Sync,
    ) -> Pin<Box<dyn Future<Output = Result<O, RequestError<E>>> + Send + 'async_trait>> { ... }
    fn resolve_error(
        &self,
        response: Result<O, RequestError<E>>,
        error_handler: impl Fn(RequestError<E>) + Sync,
    ) -> Option<O> { ... }
    fn get_request_handler<'a, 'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        endpoint: &'life1 str,
        parameters: &'life2 HashMap<&'a str, Value>,
        map: impl 'async_trait + FnOnce(T) -> O + Send + Sync,
        error_handler: impl 'async_trait + Fn(RequestError<E>) + Sync + Send,
    ) -> Pin<Box<dyn Future<Output = Option<O>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'a: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn post_request_handler<'life0, 'life1, 'async_trait>(
        &'life0 self,
        endpoint: &'life1 str,
        json: String,
        map: impl 'async_trait + FnOnce(T) -> O + Send + Sync,
        error_handler: impl 'async_trait + Fn(RequestError<E>) + Sync + Send,
    ) -> Pin<Box<dyn Future<Output = Option<O>> + Send + 'async_trait>>
       where Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

A trait for handling HTTP requests.

Provided Methods§

Source

fn request_map<'async_trait>( request: RequestBuilder, map: impl 'async_trait + FnOnce(T) -> O + Send + Sync, ) -> Pin<Box<dyn Future<Output = Result<O, RequestError<E>>> + Send + 'async_trait>>

Sends an HTTP request, processes the response, and maps it using the provided closure.

This asynchronous function sends an HTTP request using the given reqwest::RequestBuilder, processes the response, and maps it using the provided closure. It returns the mapped result if the request is successful, or an RequestError::ErrorPayload variant if the request fails.

§Arguments
  • self - A reference to the struct implementing this trait.
  • request - The reqwest::RequestBuilder representing the request to be sent.
  • map - A closure that maps the successful response JSON into the desired output type. Just write |x| x if the not mapping is required.
§Returns

A Result containing the mapped output type or an RequestError variant.

Source

fn resolve_error( &self, response: Result<O, RequestError<E>>, error_handler: impl Fn(RequestError<E>) + Sync, ) -> Option<O>

Resolves the error in the response and returns an option containing the value or None.

§Arguments
  • response - The response as a Result 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.

Source

fn get_request_handler<'a, 'life0, 'life1, 'life2, 'async_trait>( &'life0 self, endpoint: &'life1 str, parameters: &'life2 HashMap<&'a str, Value>, map: impl 'async_trait + FnOnce(T) -> O + Send + Sync, error_handler: impl 'async_trait + Fn(RequestError<E>) + Sync + Send, ) -> Pin<Box<dyn Future<Output = Option<O>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'a: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

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.
  • map - A closure that maps the successful response JSON into the desired output type.
  • error_handler - A closure that handles the error case if an RequestError occurs.
§Returns

An Option<O> representing the response data if successful, or None if an error occurred.

Source

fn post_request_handler<'life0, 'life1, 'async_trait>( &'life0 self, endpoint: &'life1 str, json: String, map: impl 'async_trait + FnOnce(T) -> O + Send + Sync, error_handler: impl 'async_trait + Fn(RequestError<E>) + Sync + Send, ) -> Pin<Box<dyn Future<Output = Option<O>> + Send + 'async_trait>>
where Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: '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.
  • map - A closure that maps the successful response JSON into the desired output type.
  • error_handler - A closure that handles the error case if an RequestError occurs.
§Returns

An Option<O> representing the response data if successful, or None if an error occurred.

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§