Skip to content

Commit a71a0c1

Browse files
authored
constants for pester methods, http package constants for standart methods and status codes, fix typos in sample (#41)
1 parent e88d85b commit a71a0c1

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

pester.go

+20-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ import (
1616
"time"
1717
)
1818

19+
const (
20+
methodDo = "Do"
21+
methodGet = "Get"
22+
methodHead = "Head"
23+
methodPost = "Post"
24+
methodPostForm = "PostForm"
25+
)
26+
1927
//ErrUnexpectedMethod occurs when an http.Client method is unable to be mapped from a calling method in the pester client
2028
var ErrUnexpectedMethod = errors.New("unexpected client method, must be one of Do, Get, Head, Post, or PostFrom")
2129

@@ -196,7 +204,7 @@ func (c *Client) pester(p params) (*http.Response, error) {
196204
// of concurrency. Other verbs can mutate and should not
197205
// make use of the concurrency feature
198206
concurrency := c.Concurrency
199-
if p.verb != "GET" {
207+
if p.verb != http.MethodGet {
200208
concurrency = 1
201209
}
202210

@@ -269,15 +277,15 @@ func (c *Client) pester(p params) (*http.Response, error) {
269277
var resp *http.Response
270278
// route the calls
271279
switch p.method {
272-
case "Do":
280+
case methodDo:
273281
resp, err = httpClient.Do(p.req)
274-
case "Get":
282+
case methodGet:
275283
resp, err = httpClient.Get(p.url)
276-
case "Head":
284+
case methodHead:
277285
resp, err = httpClient.Head(p.url)
278-
case "Post":
286+
case methodPost:
279287
resp, err = httpClient.Post(p.url, p.bodyType, p.body)
280-
case "PostForm":
288+
case methodPostForm:
281289
resp, err = httpClient.PostForm(p.url, p.data)
282290
default:
283291
err = ErrUnexpectedMethod
@@ -286,7 +294,7 @@ func (c *Client) pester(p params) (*http.Response, error) {
286294
// Early return if we have a valid result
287295
// Only retry (ie, continue the loop) on 5xx status codes and 429
288296

289-
if err == nil && resp.StatusCode < 500 && (resp.StatusCode != 429 || (resp.StatusCode == 429 && !c.RetryOnHTTP429)) {
297+
if err == nil && resp.StatusCode < http.StatusInternalServerError && (resp.StatusCode != http.StatusTooManyRequests || (resp.StatusCode == http.StatusTooManyRequests && !c.RetryOnHTTP429)) {
290298
multiplexCh <- result{resp: resp, err: err, req: n, retry: i}
291299
return
292300
}
@@ -417,27 +425,27 @@ func (c *Client) log(ctx context.Context, e ErrEntry) {
417425

418426
// Do provides the same functionality as http.Client.Do
419427
func (c *Client) Do(req *http.Request) (resp *http.Response, err error) {
420-
return c.pester(params{method: "Do", req: req, verb: req.Method, url: req.URL.String()})
428+
return c.pester(params{method: methodDo, req: req, verb: req.Method, url: req.URL.String()})
421429
}
422430

423431
// Get provides the same functionality as http.Client.Get
424432
func (c *Client) Get(url string) (resp *http.Response, err error) {
425-
return c.pester(params{method: "Get", url: url, verb: "GET"})
433+
return c.pester(params{method: methodGet, url: url, verb: http.MethodGet})
426434
}
427435

428436
// Head provides the same functionality as http.Client.Head
429437
func (c *Client) Head(url string) (resp *http.Response, err error) {
430-
return c.pester(params{method: "Head", url: url, verb: "HEAD"})
438+
return c.pester(params{method: methodHead, url: url, verb: http.MethodHead})
431439
}
432440

433441
// Post provides the same functionality as http.Client.Post
434442
func (c *Client) Post(url string, bodyType string, body io.Reader) (resp *http.Response, err error) {
435-
return c.pester(params{method: "Post", url: url, bodyType: bodyType, body: body, verb: "POST"})
443+
return c.pester(params{method: methodPost, url: url, bodyType: bodyType, body: body, verb: http.MethodPost})
436444
}
437445

438446
// PostForm provides the same functionality as http.Client.PostForm
439447
func (c *Client) PostForm(url string, data url.Values) (resp *http.Response, err error) {
440-
return c.pester(params{method: "PostForm", url: url, data: data, verb: "POST"})
448+
return c.pester(params{method: methodPostForm, url: url, data: data, verb: http.MethodPost})
441449
}
442450

443451
// set RetryOnHTTP429 for clients,

sample/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func main() {
4949
{ // drop in replacement for http.Get and other client methods
5050
resp, err := pester.Get(fmt.Sprintf("http://localhost:%d", port))
5151
if err != nil {
52-
log.Fatalf("error GETing default", err)
52+
log.Fatalf("error GETing default - %v\n\n", err)
5353
}
5454
defer resp.Body.Close()
5555

@@ -84,7 +84,7 @@ func main() {
8484

8585
resp, err := client.Get(fmt.Sprintf("http://localhost:%d", port))
8686
if err != nil {
87-
log.Fatalf("error GETing custom backoff\n\n", client.LogString())
87+
log.Fatalf("error GETing custom backoff, %s\n\n", client.LogString())
8888
}
8989
defer resp.Body.Close()
9090

0 commit comments

Comments
 (0)