Skip to content
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

Support Reqwest Middleware #8

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

Conversation

Zizico2
Copy link

@Zizico2 Zizico2 commented May 25, 2022

I needed middleware support for a project of mine. My use case is a website that doesn't properly set the content-type charset, so I wanted to set it manually using a reqwest-middleware.
I had to add a Box and a .clone(), this may not be optimal, I'm not that well versed in your code. It's working fine for me. Would this be something you consider including? Maybe behind a middleware feature?

For reference:
https://github.com/TrueLayer/reqwest-middleware

@Zizico2
Copy link
Author

Zizico2 commented May 25, 2022

There are still comments that need to be updated and I may have left some unused imports. I just wanted to know if this is of interest to you before polishing it.

Copy link
Owner

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see why this would be valuable.

but I'd rather put this behind a feature flag and make this an alternative via a cfg, that should be possible

Cargo.toml Outdated Show resolved Hide resolved
src/domain.rs Outdated Show resolved Hide resolved
let mut headers = HeaderMap::new();
std::mem::swap(&mut headers, resp.headers_mut());
(resp.status(), resp.url().clone(), headers)
(resp.status(), resp.url().clone(), resp.headers_mut().clone())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this swap just for performance reasons? It needs to return headers_mut() response's directly since it will be mutated and isn't just a view "stuck in time".

@@ -47,6 +47,7 @@ impl<T> RequestQueue<T> {
/// Set a delay to be applied between requests
pub fn set_delay(&mut self, mut delay: RequestDelay) -> Option<RequestDelay> {
if let Some((_, d)) = self.delay.as_mut() {
//TODO: take a look. decide if this swap is necessary
std::mem::swap(&mut delay, d);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My I ask why this swap? Just curious

@Zizico2
Copy link
Author

Zizico2 commented Jun 1, 2022

If that's all I'll make this a feature.

@LesterKim
Copy link

LesterKim commented Aug 27, 2023

I can see why this would be valuable.

but I'd rather put this behind a feature flag and make this an alternative via a cfg, that should be possible

I suggest abstracting the client through a generic parameter or even a trait object. That way, the user can decide which version of the reqwest client he wants. This also helps with unit testing as we can mock the client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants