-
Notifications
You must be signed in to change notification settings - Fork 73
/
http_client.go
124 lines (101 loc) · 2.77 KB
/
http_client.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package utils
import (
"bytes"
"context"
"fmt"
"html"
"io/ioutil"
"net/http"
"strings"
log "github.com/sirupsen/logrus"
)
type HTTPClientInterface interface {
Get(ctx context.Context, url string) ([]byte, error)
Post(ctx context.Context, url string, requestBody []byte) ([]byte, error)
Do(req *http.Request) (*http.Response, error)
}
type HTTPClient struct {
httpClient *http.Client
apiKey string
}
func NewHTTPClient(apiKey string) HTTPClientInterface {
return &HTTPClient{
httpClient: &http.Client{},
apiKey: apiKey,
}
}
func (c *HTTPClient) Get(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
log.WithFields(log.Fields{
"url": html.EscapeString(url),
"error": err.Error(),
}).Debug("HTTPClient: error creating new GET request")
return nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
return data, nil
}
func (c *HTTPClient) Post(ctx context.Context, url string, requestBody []byte) ([]byte, error) {
rBody := bytes.NewBuffer(requestBody)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, rBody)
if err != nil {
log.WithFields(log.Fields{
"url": html.EscapeString(url),
"error": err.Error(),
}).Debug("HTTPClient: error creating new POST request")
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if c.apiKey != "" {
req.Header.Set("Api-Key", c.apiKey)
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
func (c *HTTPClient) Do(req *http.Request) (*http.Response, error) {
resp, err := c.httpClient.Do(req)
if err != nil {
if strings.Contains(err.Error(), "context canceled") {
return resp, err
}
}
respStatusCode := getResponseCodeString(resp)
url := req.URL.String()
if err != nil || !isResponseSuccess(resp) {
log.WithFields(log.Fields{
"method": html.EscapeString(req.Method),
"statusCode": respStatusCode,
"url": html.EscapeString(url),
"error": err,
}).Debug("HTTPClient: error performing request")
}
if !isResponseSuccess(resp) {
err = fmt.Errorf("error performing %s request to %s: %s", html.EscapeString(req.Method), html.EscapeString(url), respStatusCode)
}
return resp, err
}
func getResponseCodeString(resp *http.Response) string {
if resp == nil {
return ""
}
return fmt.Sprintf("%d", resp.StatusCode)
}
// Ensures the response status code falls within the
// status codes that are commonly considered successful.
func isResponseSuccess(resp *http.Response) bool {
if resp == nil {
return false
}
statusCode := resp.StatusCode
return statusCode >= http.StatusOK && statusCode <= 299
}