-
Couldn't load subscription status.
- Fork 9
Default headers work around #263
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
Default headers work around #263
Conversation
715437e to
7fa4373
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements a workaround for an issue where HTTP clients don't copy default headers across requests in Twirp client configurations. The change adds support for setting default headers in direct mode and ensures they are properly applied to requests.
Key changes:
- Added
with_default_headermethod toClientBuilderfor setting default headers in direct mode - Modified request execution to apply default headers when they don't already exist on the request
- Refactored
ClientBuilderto use struct spread syntax for cleaner code
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
crates/twirp/src/client.rs
Outdated
| if let Some(handlers) = &mut self.handlers { | ||
| handlers.default_headers.insert(key, value); | ||
| } else { | ||
| panic!("you must use `ClientBuilder::direct()` to register handler headers"); |
Copilot
AI
Oct 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message mentions 'handler headers' but the method is for 'default headers'. Consider changing to 'you must use ClientBuilder::direct() to register default headers'.
| panic!("you must use `ClientBuilder::direct()` to register handler headers"); | |
| panic!("you must use `ClientBuilder::direct()` to register default headers"); |
| if let Some(handlers) = &mut self.handlers { | ||
| handlers.default_headers.insert(key, value); | ||
| } else { | ||
| panic!("you must use `ClientBuilder::direct()` to register handler headers"); | ||
| } |
Copilot
AI
Oct 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using panic! for API misuse creates a poor developer experience. Consider returning a Result or using a builder pattern with marker types to prevent this at compile time, as mentioned in the PR description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense to me
7fa4373 to
d40edd0
Compare
A work around for an outstanding issue where HTTP client doesn't copy the default headers across.
We need to decide long term if we want to have a different approach for HTTP and direct or you just pass in a HTTP client to a direct to give default headers.
It does feel a bit weird to pass in a HTTP client to a direct
TwirpClient, but we have one already.Right now there are some runtime panics if you use the wrong methods on the builder (e.g. if you try to add a handler to a http client). We could get around this by adding a marker type to be builder and only implementing direct mode functions on e.g.
ClientBuilder<DirectMode>