-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathhttp_client.go
More file actions
64 lines (52 loc) · 1.17 KB
/
http_client.go
File metadata and controls
64 lines (52 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package fmpcloud
import (
"net/http"
"time"
"github.com/go-resty/resty/v2"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// HTTPClient ...
type HTTPClient struct {
logger *zap.Logger
client *resty.Client
apiKey string
retryCount *int
retryWaitTime *time.Duration
}
// Get ...
func (h *HTTPClient) Get(endpoint string, data map[string]string) (response *resty.Response, err error) {
if data == nil {
data = make(map[string]string)
}
data["apikey"] = h.apiKey
retries := 0
for retries < *h.retryCount {
response, err = h.client.R().
SetQueryParams(data).
Get(endpoint)
if err != nil || response.StatusCode() != http.StatusOK {
time.Sleep(*h.retryWaitTime)
retries++
// response is not valid when there is an error
var errOrStatusCode zapcore.Field
if err != nil {
errOrStatusCode = zap.Error(err)
} else {
errOrStatusCode = zap.Int("statusCode", response.StatusCode())
}
h.logger.Info(
"Retry request.",
zap.Int("retries", retries),
errOrStatusCode,
zap.String("endpoint", endpoint),
zap.Any("data", data),
)
continue
}
if err == nil {
break
}
}
return response, err
}