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

feat: add custom middleware #3502

Open
wants to merge 3 commits into
base: main
Choose a base branch
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)

@@ -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

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

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.

None yet

4 participants