-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_client.go
81 lines (70 loc) · 1.7 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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"time"
)
type HTTP struct {
BaseUrl string
client *http.Client
}
func NewHTTP(baseUrl string) *HTTP {
return &HTTP{
BaseUrl: baseUrl,
client: http.DefaultClient,
}
}
func (a *HTTP) Get(path string) (int, []byte) {
return a.RetryRequest("GET", path, []byte{})
}
func (a *HTTP) Post(path string, data []byte) (int, []byte) {
return a.RetryRequest("POST", path, data)
}
func (a *HTTP) Put(path string, data []byte) (int, []byte) {
return a.RetryRequest("PUT", path, data)
}
func (a *HTTP) Delete(path string) (int, []byte) {
return a.RetryRequest("DELETE", path, []byte{})
}
func (a *HTTP) RetryRequest(method string, path string, data []byte) (int, []byte) {
for x := 0; x < 5; x++ {
i, b, err := a.Request(method, path, data)
if err == nil {
return i, b
}
minSleep := x * 200
sleep := time.Duration(minSleep + rand.Intn(500))
time.Sleep(sleep * time.Millisecond)
}
return 0, nil
}
func (a *HTTP) Request(method string, path string, data []byte) (int, []byte, error) {
url := fmt.Sprintf("%s%s", a.BaseUrl, path)
req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
if err != nil {
logger.Error("http.NewRequest",
"method", method,
"path", path,
"err", err)
return 0, nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := a.client.Do(req)
if err != nil {
logger.Error("client.Do",
"method", method,
"path", path,
"err", err)
return 0, nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
logger.Info("submitted http request",
"method", method,
"url", url,
"status_code", resp.StatusCode)
return resp.StatusCode, body, nil
}