-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.go
93 lines (85 loc) · 2.32 KB
/
utils.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
package es
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/hashicorp/go-retryablehttp"
)
type Response struct {
Status int
Data map[string]interface{}
}
var httpClient *retryablehttp.Client
func init() {
//TODO:: Enable custom certification verification
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient = retryablehttp.NewClient()
httpClient.HTTPClient.Transport = tr
httpClient.RetryWaitMin = 200 * time.Millisecond
httpClient.CheckRetry = checkRetry
httpClient.Logger = nil
}
func checkRetry(ctx context.Context, resp *http.Response, err error) (bool, error) {
if resp == nil {
return false, nil
}
// Handling special bad request of resource creation else relying on default policy for 400
// Don't retry 400 for all bad request
if resp.StatusCode == 400 {
var data map[string]interface{}
json.NewDecoder(resp.Body).Decode(&data)
// This is required to reassign the Body as we're reading it. Stream can't be read twice
dataByte, _ := json.Marshal(data)
resp.Body = ioutil.NopCloser(bytes.NewBuffer(dataByte))
var reason string
respErr := data["error"].(map[string]interface{})
if respErr != nil {
reason = data["error"].(map[string]interface{})["type"].(string)
}
if reason == "resource_already_exists_exception" {
return true, nil
}
}
return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
}
// MakeRequest initiate request to ES API
func (esClient *Client) MakeRequest(method string,
endPoint string,
body []byte,
headers map[string]string) (Response, error) {
var response Response
var err error
req, err := retryablehttp.NewRequest(method, esClient.URL+endPoint, bytes.NewBuffer(body))
if headers != nil {
for key, value := range headers {
req.Header.Set(key, value)
}
}
//Username and password can not be blank, if blank skip
if esClient.Username != "" && esClient.Password != "" {
req.SetBasicAuth(esClient.Username, esClient.Password)
}
doneCh := make(chan bool)
go func() {
defer close(doneCh)
resp, err := httpClient.Do(req)
if err != nil {
doneCh <- false
return
}
defer resp.Body.Close()
json.NewDecoder(resp.Body).Decode(&response.Data)
response.Status = resp.StatusCode
doneCh <- true
}()
if <-doneCh {
return response, nil
}
return response, err
}