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

Allow for configuration of http.Client #18

Open
arwineap opened this issue May 8, 2024 · 0 comments
Open

Allow for configuration of http.Client #18

arwineap opened this issue May 8, 2024 · 0 comments

Comments

@arwineap
Copy link

arwineap commented May 8, 2024

Currently the http.Client is completely managed by the constructor , and is unexported preventing any further configuration of the client after initialization

However, it may be desirable to provide a customized client to the library in order to support additional features like:

  • custom timeouts
  • proxy configuration
  • instrumentation ( otel )

There's a couple methods to solve, each with their own pros and cons

With a breaking change we could inject the client into the constructor:

func NewClient(apiKey string, client *http.Client) (Client, error) {
[...]

At the risk of a user modifying the client midflight, we could export the client field:

type Client struct {
	C *http.Client // http client
	e string       // api endpoint
	k string       // api key
}

To maintain backwards compatibility, we could add variadic, functional parameters:

func NewClient(apiKey string, opts ...Option) (Client, error) {

Which could look like this:

client, _ := NewClient(apiKey, WithHttpClient(&http.Client{}))

Given that apiKey is optional, it may be interesting to move to a new constructor signature to allow it to be omitted:

client, _ := NewClient(WithHttpClient(&http.Client{}))
client2 := NewClient(WithApiKey("XXXXX"), WithHttpClient(&http.Client{}))

Let me know what you think, and if I can help

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

No branches or pull requests

1 participant