Skip to content

Commit b46a2b0

Browse files
authored
Ability to set custom http client (#19)
* set custom http client * add example configuration to readme * format * format readme code block * add json tag for http client (omit) * format
1 parent 48429ca commit b46a2b0

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,21 @@ To clarify, do NOT use API keys, which are globally-scoped:
2929
DO use scoped API tokens:
3030

3131
![Don't use API keys](https://user-images.githubusercontent.com/1128849/81196503-5c91d800-8f7c-11ea-93cc-ad7d73420fab.png)
32+
33+
## Example Configuration
34+
35+
```golang
36+
// With Auth
37+
p := cloudflare.Provider{
38+
APIToken: "apitoken",
39+
ZoneToken: "zonetoken", // optional
40+
}
41+
42+
// With Custom HTTP Client
43+
p := cloudflare.Provider{
44+
APIToken: "apitoken",
45+
HTTPClient: http.Client{
46+
Timeout: 10 * time.Second,
47+
},
48+
}
49+
```

client.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ func (p *Provider) getZoneInfo(ctx context.Context, zoneName string) (cfZone, er
118118
return zones[0], nil
119119
}
120120

121+
// getClient returns http client to use
122+
func (p *Provider) getClient() HTTPClient {
123+
if p.HTTPClient == nil {
124+
return http.DefaultClient
125+
}
126+
return p.HTTPClient
127+
}
128+
121129
// doAPIRequest does the round trip, adding Authorization header if not already supplied.
122130
// It returns the decoded response from Cloudflare if successful; otherwise it returns an
123131
// error including error information from the API if applicable. If result is a
@@ -128,7 +136,7 @@ func (p *Provider) doAPIRequest(req *http.Request, result any) (cfResponse, erro
128136
req.Header.Set("Authorization", "Bearer "+p.APIToken)
129137
}
130138

131-
resp, err := http.DefaultClient.Do(req)
139+
resp, err := p.getClient().Do(req)
132140
if err != nil {
133141
return cfResponse{}, err
134142
}

provider.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
"github.com/libdns/libdns"
1010
)
1111

12+
type HTTPClient interface {
13+
Do(req *http.Request) (*http.Response, error)
14+
}
15+
1216
// Provider implements the libdns interfaces for Cloudflare.
1317
// TODO: Support pagination and retries, handle rate limits.
1418
type Provider struct {
@@ -17,6 +21,10 @@ type Provider struct {
1721
APIToken string `json:"api_token,omitempty"` // API token with Zone.DNS:Write (can be scoped to single Zone if ZoneToken is also provided)
1822
ZoneToken string `json:"zone_token,omitempty"` // Optional Zone:Read token (global scope)
1923

24+
// HTTPClient is the client used to communicate with Cloudflare.
25+
// If nil, a default client will be used.
26+
HTTPClient HTTPClient `json:"-"`
27+
2028
zones map[string]cfZone
2129
zonesMu sync.Mutex
2230
}

0 commit comments

Comments
 (0)