Open glevine opened 5 years ago
After another look, I realized I could use the oauth2.TokenSource
interface.
type APIClient struct {
src oauth2.TokenSource
}
func (client *APIClient) WithTokenSource(src oauth2.TokenSource) *APIClient {
client.src = src
return client
}
func (client *APIClient) Get(ctx context.Context, ...) (*http.Response, err) {
httpClient := oauth2.NewClient(ctx, client.src)
return httpClient.Get(...)
}
client := APIClient{}
config := &oauth2.Config{...}
token := &oauth2.Token{...}
ts := conf.TokenSource(ctx, token)
resp, err := client.WithTokenSource(ts).Get(...)
// OR
config := &clientcredentials.Config{...}
ts := conf.TokenSource(ctx)
resp, err := client.WithTokenSource(ts).Get(...)
I'd like to be able to pass either an
oauth2.Config
orclientcredentials.Config
into a function and produce an*http.Client
. This would help with supporting either two- or three-legged OAuth in an API client.But I don't see any methods that share the same signatures. And I haven't found a way to work around that cleanly.
Is there an existing pattern that allows me to do this and I'm just not seeing it?