pub trait RequestModifiers: RequestInfo {
    // Provided methods
    fn authorization_header(
        request_builder: RequestBuilder,
        token: &str
    ) -> RequestBuilder { ... }
    fn create_endpoint(endpoint: &str) -> String { ... }
    fn resolve_error<T, E, F>(
        response: &Result<T, E>,
        error_resolver: F
    ) -> Option<&T>
       where F: Fn(&E) { ... }
}
Expand description

This trait provides methods for modifying the struct in a specific way:

Provided Methods§

source

fn authorization_header( request_builder: RequestBuilder, token: &str ) -> RequestBuilder

Adds an Authorization header to the given RequestBuilder with the provided token.

The Authorization header follows the format “Bearer TOKEN”, where TOKEN is the authentication token used for authorization.

Arguments
  • request_builder - The RequestBuilder to add the header to.
  • token - The authentication token to include in the Authorization header.
Returns

The modified RequestBuilder with the Authorization header added.

Example
use reqwest::RequestBuilder;
let request_builder = reqwest::Client::new().get("https://example.com"); 
let token = "YOUR_AUTH_TOKEN";
let modified_request_builder = authorization_header(&request_builder, token);
source

fn create_endpoint(endpoint: &str) -> String

Joins the given endpoint with the base URL.

Arguments
  • endpoint - The endpoint to join with the base URL.
Returns

The joined URL as a String.

Example
struct MyStruct;
impl RequestHandler for ... {
    const BASE_URL: &'static str = "https://api.example.com";
}
fn main(){
   let url =  MyStruct::create_endpoint("get");
   assert_eq!(url,"https://api.example.com/get"); // using the default implementation
}
source

fn resolve_error<T, E, F>( response: &Result<T, E>, error_resolver: F ) -> Option<&T>where F: Fn(&E),

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.

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.

Implementors§