Conversation
715437e to
7fa4373
Compare
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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"); | ||
| } |
There was a problem hiding this comment.
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.
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>