Skip to content

Commit

Permalink
Allow using a custom http.Client for RequestToken and AccessToken (dg…
Browse files Browse the repository at this point in the history
…hubble#49)

* Allow using a custom http.Client for RequestToken and AccessToken. This may be useful when
authenticating against a endpoints that use a custom root CA
* Continue to default to `http.DefaultClient`
  • Loading branch information
fdcds authored and bvuong committed Sep 16, 2021
1 parent c8d844b commit 748040a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions config.go
Expand Up @@ -31,6 +31,8 @@ type Config struct {
Signer Signer
// Noncer creates request nonces (defaults to DefaultNoncer)
Noncer Noncer
// HTTPClient overrides the choice of http.DefaultClient for RequestToken and AccessToken
HTTPClient *http.Client
}

// NewConfig returns a new Config with the given consumer key and secret.
Expand Down Expand Up @@ -71,7 +73,7 @@ func (c *Config) RequestToken() (requestToken, requestSecret string, err error)
if err != nil {
return "", "", err
}
resp, err := http.DefaultClient.Do(req)
resp, err := c.httpClient().Do(req)
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -148,7 +150,7 @@ func (c *Config) AccessToken(requestToken, requestSecret, verifier string) (acce
if err != nil {
return "", "", err
}
resp, err := http.DefaultClient.Do(req)
resp, err := c.httpClient().Do(req)
if err != nil {
return "", "", err
}
Expand All @@ -173,3 +175,10 @@ func (c *Config) AccessToken(requestToken, requestSecret, verifier string) (acce
}
return accessToken, accessSecret, nil
}

func (c *Config) httpClient() *http.Client {
if c.HTTPClient != nil {
return c.HTTPClient
}
return http.DefaultClient
}

0 comments on commit 748040a

Please sign in to comment.