Skip to content

Add final initialization to ClientBuilder #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

fivlao
Copy link

@fivlao fivlao commented Apr 5, 2025

Add feature ClientBuilder::with_final_init and ClientBuilder::with_arc_final_init

This allows you to access extensions that are added using RequestBuilder::with_extension when creating a request.

Example:

use reqwest_middleware::{ClientBuilder, RequestBuilder, RequestInitialiser};

#[derive(Clone)]
struct Mark;

struct Init;

impl RequestInitialiser for Init {
    fn init(&self, mut req: RequestBuilder) -> RequestBuilder {
        let exists = req.extensions().get::<Mark>().is_some();
        assert!(!exists);
        req
    }
}

struct FinalInit;

impl RequestInitialiser for FinalInit {
    fn init(&self, mut req: RequestBuilder) -> RequestBuilder {
        let exists = req.extensions().get::<Mark>().is_some();
        assert!(exists);
        req
    }
}

#[tokio::main]
async fn main() {
    let request_client = reqwest::ClientBuilder::new()
        .build()
        .unwrap();
    let client = ClientBuilder::new(request_client)
        .with_init(Init)
        .with_final_init(FinalInit)
        .build();
    let resp = client.get("https://truelayer.com")
        .with_extension(Mark)
        .send().await
        .unwrap();
    println!("TrueLayer page HTML: {}", resp.text().await.unwrap());
}

@fivlao fivlao requested a review from a team as a code owner April 5, 2025 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant