Skip to content

Commit

Permalink
Create client in factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
timohirt committed Feb 18, 2022
1 parent 076b94a commit 0a9ecb7
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions hetznerdns/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ type ErrorMessage struct {
type createHTTPClient func() *http.Client

func defaultCreateHTTPClient() *http.Client {
return &http.Client{}
retryableClient := retryablehttp.NewClient()
retryableClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
ok, err := retryablehttp.DefaultRetryPolicy(ctx, resp, err)
if !ok && resp.StatusCode == http.StatusUnprocessableEntity {
return true, nil
}
return ok, err
}
retryableClient.RetryMax = 10
return retryableClient.StandardClient()
}

// Client for the Hetzner DNS API.
Expand All @@ -47,16 +56,7 @@ func NewClient(apiToken string) (*Client, error) {
}

func (c *Client) doHTTPRequest(apiToken string, method string, url string, body io.Reader) (*http.Response, error) {
retryableClient := retryablehttp.NewClient()
retryableClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
ok, err := retryablehttp.DefaultRetryPolicy(ctx, resp, err)
if !ok && resp.StatusCode == http.StatusUnprocessableEntity {
return true, nil
}
return ok, err
}
retryableClient.RetryMax = 10
client := retryableClient.StandardClient()
client := c.createHTTPClient()

log.Printf("[DEBUG] HTTP request to API %s %s", method, url)
req, err := http.NewRequest(method, url, body)
Expand Down

0 comments on commit 0a9ecb7

Please sign in to comment.