ns1 / ns1-go

Golang API client for NS1
Apache License 2.0
34 stars 59 forks source link

No ability to use contexts for requests #180

Open jamesgoodhouse opened 1 year ago

jamesgoodhouse commented 1 year ago

There appears to be no way to utilize contexts for requests. This makes it impossible to set effective timeouts for requests, as well as instrument any requests. To maintain backwards compatibility, I'd suggest making additional methods called GetWithContext or UpdateWithContext for example.

jamesgoodhouse commented 1 year ago

There does appear to be a way to modify a request via the Decorator func, but this means needing to construct a new NS1 client for every request.

func NewNS1Client(ctx context.Context, apiKey string) *ns1.Client {
    doer := ns1.Decorate(http.DefaultClient, addContextToRequest(ctx))
    return ns1.NewClient(doer, ns1.SetAPIKey(apiKey))
}

func addContextToRequest(ctx context.Context) ns1.Decorator {
    return func(d ns1.Doer) ns1.Doer {
        return ns1.DoerFunc(func(r *http.Request) (*http.Response, error) {
            r = r.WithContext(ctx)
            return d.Do(r)
        })
    }
}