Skip to content

feat: add custom middleware #3502

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

Closed
wants to merge 0 commits into from

Conversation

tdejager
Copy link
Contributor

Summary

This adds the ability to the RegistryClient and BaseClient for the consumer of the library to add custom middleware. The problem is that we have different authentication methods/requirements for pypi registries and would like to use the middleware provided by https://github.com/mamba-org/rattler

I've decided that if you do this, it overrides the normal middleware. This makes the retries option, and maybe some others, not really do anything anymore. Which is a bit weird, but couldn't directly think of a better way to do it, as you might end up with 'duplicated' middleware maybe.

This should close the issue/question I made: #3400

I'll make it a draft for now because I do not know how you feel about such a change and maybe there is a different design you have in mid.

Test Plan

Once, I can update to the latest uv version, I'll try and test things out directly in: https://github.com/prefix-dev/pixi. I'm happy to add rust test here if you know a good place for it.

@zanieb zanieb added the rustlib Related to our Rust library API label May 10, 2024
@konstin
Copy link
Member

konstin commented May 13, 2024

Sorry for not replying to the initial issue! Yes we can support. What do you think about the branch in https://github.com/astral-sh/uv/compare/konsti/custom-middleware ? It's similar to yours but encapsulates the stack selection into MiddlewareStack.

Am i correct to assume you don't care about middleware for the offline case?

@tdejager
Copy link
Contributor Author

Hi Konsti, that looks a lot better actually! Good question about the offline, our middleware does not account for it yet, but if you feel it's better to add it to the offline case as well, that seems totally valid!

@konstin
Copy link
Member

konstin commented May 14, 2024

(force-pushing because i had to rebase on main, your initial commit should be preserved)

@konstin konstin force-pushed the feat/custom-middleware branch from 95fec31 to 7b88899 Compare May 14, 2024 09:48
@@ -113,7 +115,7 @@ pub(crate) async fn pip_compile(
let client_builder = BaseClientBuilder::new()
.connectivity(connectivity)
.native_tls(native_tls)
.keyring(keyring_provider);
.middleware_stack(MiddlewareStack::new(3, keyring_provider));
Copy link
Member

Choose a reason for hiding this comment

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

Not sure about where to put the retries constant

@charliermarsh
Copy link
Member

@zanieb - do you want to review this?

@zanieb zanieb self-assigned this May 22, 2024
@zanieb
Copy link
Member

zanieb commented May 22, 2024

Sure

@konstin konstin force-pushed the feat/custom-middleware branch from 7b88899 to ec5f16f Compare May 22, 2024 09:37
Comment on lines 292 to 294
.keyring(keyring_provider)
.middleware_stack(MiddlewareStack::new(3, keyring_provider))
Copy link
Member

@zanieb zanieb May 23, 2024

Choose a reason for hiding this comment

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

Hey @tdejager sorry I've been slow to get to this.

I don't love this new API. Could we instead:

  • Only create the retry middleware if retries is non-zero
  • Replace the Keyring option with Option<AuthMiddleware>
  • Add a extra_middleware: Vec<Arc<dyn Middleware>> option

I think this should get you the same functionality, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like a good improvement to me, I can also make the changes early next week if you are busy @konstin :)

Copy link
Contributor Author

@tdejager tdejager May 23, 2024

Choose a reason for hiding this comment

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

Also @zanieb and @konstin thanks again for taking a look 🙂

Copy link
Member

Choose a reason for hiding this comment

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

I'll be out next week fyi.

I missed that @konstin introduced the MiddlewareStack — let's make sure he's onboard with my proposal :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah maybe having the AuthMiddleware in the stack is enough. I thought you might want to be more explicit about enabling/disabling the auth middleware. Let see what konsti thinks :)

Copy link
Member

Choose a reason for hiding this comment

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

I think we can just have the AuthMiddleware populated by default and ya'll can pass None to explicitly remove it.

Copy link
Member

Choose a reason for hiding this comment

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

I changed the API so it's now .with methods and checks for > 0 retries, but if we don't take a keyring it becomes even more verbose because we have a number of callsites, and also KeyringProviderType lives in uv-configuration so it can't be Into<AuthMiddleware>.

image

@konstin konstin force-pushed the feat/custom-middleware branch from ec5f16f to 8869efc Compare June 3, 2024 11:01
@olivier-lacroix
Copy link

Hi all, just wondering if you guys are happy with the current design, and if rebasing is the only requirement for this PR to be merged?

@konstin konstin requested a review from zanieb July 10, 2024 07:35
@zanieb
Copy link
Member

zanieb commented Jul 19, 2024

Sorry, I need to review the latest implementation but am prioritizing things that impact more users right now.

@olivier-lacroix
Copy link

No worries. Thanks @zanieb !

@zanieb
Copy link
Member

zanieb commented Sep 16, 2024

Is this still needed?

@tdejager
Copy link
Contributor Author

Would still be very useful for us, however, as reqwest-middleware is now a forked version, I'm unsure regarding the compatibilities with the open-source version. However, we could re-vendor the middleware if needed :)

@zanieb
Copy link
Member

zanieb commented Oct 1, 2024

It looks like @konstin added AuthIntegration::NoAuthMiddleware allowing the auth middleware to be disabled so maybe we just need the ability to attach extra middleware now?

@tdejager
Copy link
Contributor Author

tdejager commented Oct 1, 2024

Sounds good, is there something you like me to change other than a rebase?

@zanieb
Copy link
Member

zanieb commented Oct 1, 2024

Can we drop all the MiddlewareStack stuff and just add a extra_middleware: Vec<Arc<dyn Middleware>> option? Or is there more that you need?

@tdejager
Copy link
Contributor Author

tdejager commented Oct 1, 2024

Not at all computer currently but sounds good to me, let me give it a go at the end of the week!

@tdejager tdejager closed this Nov 4, 2024
@tdejager tdejager force-pushed the feat/custom-middleware branch from 8869efc to a052418 Compare November 4, 2024 08:36
@tdejager tdejager deleted the feat/custom-middleware branch November 4, 2024 08:38
@tdejager
Copy link
Contributor Author

tdejager commented Nov 4, 2024

Closing this as there were so many changes it make sense to make a different PR. Will reference this when I get to it :)

@tdejager tdejager restored the feat/custom-middleware branch November 4, 2024 09:18
zanieb pushed a commit that referenced this pull request Nov 4, 2024
This PR is a revival of #3502, albeit in a much simpler form.

This would allow for different middlewares like authentication and such,
useful for if you want to deviate from the keychain authentication
methods when using uv as a library.

@zanieb I hope I made the changes as you noted you wanted to see them :)

Happy to add/change anything you need.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rustlib Related to our Rust library API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants