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

No ability to use contexts for requests #180

Open
jamesgoodhouse opened this issue Dec 16, 2022 · 1 comment · May be fixed by #192
Open

No ability to use contexts for requests #180

jamesgoodhouse opened this issue Dec 16, 2022 · 1 comment · May be fixed by #192

Comments

@jamesgoodhouse
Copy link

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 jamesgoodhouse changed the title Not ability to use contexts for requests No ability to use contexts for requests Dec 16, 2022
@jamesgoodhouse
Copy link
Author

jamesgoodhouse commented Dec 21, 2022

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)
		})
	}
}

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 a pull request may close this issue.

1 participant