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: make RequestBuilder configurable #725

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

WqyJh
Copy link
Contributor

@WqyJh WqyJh commented Apr 24, 2024

RequestBuilder is an interface, therefore, it's designed to be extensible.

type RequestBuilder interface {
	Build(ctx context.Context, method, url string, body any, header http.Header) (*http.Request, error)
}

However, currently it's a private field of Client, which make it impossible to customize.

type Client struct {
	config ClientConfig

	requestBuilder    utils.RequestBuilder
	createFormBuilder func(io.Writer) utils.FormBuilder
}

This PR moves requestBuilder to ClientConfig, making it configurable, so that users can implement their own RequestBuilder, which is very useful in situation of adding custom request headers and injecting tracing attributes.

Copy link

codecov bot commented Apr 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.67%. Comparing base (774fc9d) to head (4b70b10).
Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #725      +/-   ##
==========================================
+ Coverage   98.46%   98.67%   +0.21%     
==========================================
  Files          24       24              
  Lines        1364     1135     -229     
==========================================
- Hits         1343     1120     -223     
+ Misses         15        9       -6     
  Partials        6        6              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sashabaranov
Copy link
Owner

@WqyJh thank you for the PR! Could you outline some cases where exposed HTTPClient is not enough?

@WqyJh
Copy link
Contributor Author

WqyJh commented Apr 25, 2024

HTTPClient is not enough for setting custom headers, which needs to modify *http.Request.

Custom headers is useful if you use an non-official api-key. What's more, it's also useful to pass tracing information through headers.

@sashabaranov
Copy link
Owner

@WqyJh it is possible to modify requests with custom Transport in http.Client. We have a basic proxy example in our README, and here's an example for telemetry (+ entire transport implementation)

On another note, I think the biggest problem with custom request builders is that users would need to substitute full request builder logic every time:

var bodyReader io.Reader
if body != nil {
if v, ok := body.(io.Reader); ok {
bodyReader = v
} else {
var reqBytes []byte
reqBytes, err = b.marshaller.Marshal(body)
if err != nil {
return
}
bodyReader = bytes.NewBuffer(reqBytes)
}
}
req, err = http.NewRequestWithContext(ctx, method, url, bodyReader)
if err != nil {
return
}
if header != nil {
req.Header = header
}

It might make sense to introduce instrumentation just before request is sent similar to gRPC interceptors concept. That might have better ergonomics than implementing custom HTTP transport.

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.

None yet

2 participants