From 297b5b86f1501f3811fccfc88b59f46e1f5d4178 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 22 Sep 2021 15:25:27 +0200 Subject: [PATCH] More idiomatic naming for the rate limit handling --- breach.go | 8 ++++---- breach_test.go | 2 +- hibp.go | 27 +++++++++++++++------------ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/breach.go b/breach.go index 280a601..4ae83f4 100644 --- a/breach.go +++ b/breach.go @@ -99,7 +99,7 @@ func (b *BreachApi) Breaches(options ...BreachOption) ([]*Breach, *http.Response queryParams := b.setBreachOpts(options...) apiUrl := fmt.Sprintf("%s/breaches", BaseUrl) - hb, hr, err := b.hibp.HttpReqBody(http.MethodGet, apiUrl, queryParams) + hb, hr, err := b.hibp.HttpResBody(http.MethodGet, apiUrl, queryParams) if err != nil { return nil, nil, err } @@ -121,7 +121,7 @@ func (b *BreachApi) BreachByName(n string, options ...BreachOption) (*Breach, *h } apiUrl := fmt.Sprintf("%s/breach/%s", BaseUrl, n) - hb, hr, err := b.hibp.HttpReqBody(http.MethodGet, apiUrl, queryParams) + hb, hr, err := b.hibp.HttpResBody(http.MethodGet, apiUrl, queryParams) if err != nil { return nil, nil, err } @@ -138,7 +138,7 @@ func (b *BreachApi) BreachByName(n string, options ...BreachOption) (*Breach, *h // with all registered data classes known to HIBP func (b *BreachApi) DataClasses() ([]string, *http.Response, error) { apiUrl := fmt.Sprintf("%s/dataclasses", BaseUrl) - hb, hr, err := b.hibp.HttpReqBody(http.MethodGet, apiUrl, nil) + hb, hr, err := b.hibp.HttpResBody(http.MethodGet, apiUrl, nil) if err != nil { return nil, nil, err } @@ -160,7 +160,7 @@ func (b *BreachApi) BreachedAccount(a string, options ...BreachOption) ([]*Breac } apiUrl := fmt.Sprintf("%s/breachedaccount/%s", BaseUrl, a) - hb, hr, err := b.hibp.HttpReqBody(http.MethodGet, apiUrl, queryParams) + hb, hr, err := b.hibp.HttpResBody(http.MethodGet, apiUrl, queryParams) if err != nil { return nil, nil, err } diff --git a/breach_test.go b/breach_test.go index 4e27d37..4d66054 100644 --- a/breach_test.go +++ b/breach_test.go @@ -244,7 +244,7 @@ func TestBreachedAccountWithoutTruncate(t *testing.T) { if apiKey == "" { t.SkipNow() } - hc := New(WithApiKey(apiKey), WithRateLimitNoFail()) + hc := New(WithApiKey(apiKey), WithRateLimitSleep()) if hc == nil { t.Error("failed to create HIBP client") return diff --git a/hibp.go b/hibp.go index 89fec59..7d36fd6 100644 --- a/hibp.go +++ b/hibp.go @@ -24,11 +24,14 @@ const DefaultUserAgent = `go-hibp v` + Version // + ` - https://github.com/wnees // Client is the HIBP client object type Client struct { - hc *http.Client // HTTP client to perform the API requests - to time.Duration // HTTP client timeout - ak string // HIBP API key - ua string // User agent string for the HTTP client - rlNoFail bool // Controls wether the HTTP client should fail or sleep in case the rate limiting hits + hc *http.Client // HTTP client to perform the API requests + to time.Duration // HTTP client timeout + ak string // HIBP API key + ua string // User agent string for the HTTP client + + // If set to true, the HTTP client will sleep instead of failing in case the HTTP 429 + // rate limit hits a request + rlSleep bool PwnedPassApi *PwnedPassApi // Reference to the PwnedPassApi API PwnedPassApiOpts *PwnedPasswordOptions // Additional options for the PwnedPassApi API @@ -97,10 +100,10 @@ func WithUserAgent(a string) Option { } } -// WithRateLimitNoFail let's the HTTP client sleep in case the API rate limiting hits (Defaults to fail) -func WithRateLimitNoFail() Option { +// WithRateLimitSleep let's the HTTP client sleep in case the API rate limiting hits (Defaults to fail) +func WithRateLimitSleep() Option { return func(c *Client) { - c.rlNoFail = true + c.rlSleep = true } } @@ -146,8 +149,8 @@ func (c *Client) HttpReq(m, p string, q map[string]string) (*http.Request, error return hr, nil } -// HttpReqBody performs the API call to the given path and returns the response body as byte array -func (c *Client) HttpReqBody(m string, p string, q map[string]string) ([]byte, *http.Response, error) { +// HttpResBody performs the API call to the given path and returns the response body as byte array +func (c *Client) HttpResBody(m string, p string, q map[string]string) ([]byte, *http.Response, error) { hreq, err := c.HttpReq(m, p, q) if err != nil { return nil, nil, err @@ -165,7 +168,7 @@ func (c *Client) HttpReqBody(m string, p string, q map[string]string) ([]byte, * return nil, hr, err } - if hr.StatusCode == 429 && c.rlNoFail { + if hr.StatusCode == 429 && c.rlSleep { headerDelay := hr.Header.Get("Retry-After") delayTime, err := time.ParseDuration(headerDelay + "s") if err != nil { @@ -173,7 +176,7 @@ func (c *Client) HttpReqBody(m string, p string, q map[string]string) ([]byte, * } log.Printf("API rate limit hit. Retrying request in %s", delayTime.String()) time.Sleep(delayTime) - return c.HttpReqBody(m, p, q) + return c.HttpResBody(m, p, q) } if hr.StatusCode != 200 {