Skip to content

Allow for configuration of http.Client #18

@arwineap

Description

@arwineap

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions