-
Notifications
You must be signed in to change notification settings - Fork 0
/
httprequests.go
115 lines (105 loc) · 3.64 KB
/
httprequests.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
package httprequests
import (
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/Lord-Y/cypress-parallel-cli/logger"
"github.com/hashicorp/go-retryablehttp"
"github.com/rs/zerolog/log"
)
func init() {
logger.SetLoggerLogLevel()
}
// retryQuery permit to perform retries when HTTP requests fails
func retryQuery() (z *http.Client) {
retryClient := retryablehttp.NewClient()
if strings.TrimSpace(os.Getenv("HTTP_RETRY_MAX")) != "" {
max, err := strconv.Atoi(os.Getenv("HTTP_RETRY_MAX"))
if err != nil {
log.Warn().Err(err).Msgf("Error occured while converting string to integer")
}
if max > 0 {
retryClient.RetryMax = max
}
}
if strings.TrimSpace(os.Getenv("HTTP_RETRY_WAIT_MIN")) != "" {
waitMin, err := strconv.Atoi(os.Getenv("HTTP_RETRY_WAIT_MIN"))
if err != nil {
log.Warn().Err(err).Msgf("Error occured while converting string to integer")
}
if waitMin > 0 {
retryClient.RetryWaitMin = time.Duration(waitMin) * time.Second
}
}
if strings.TrimSpace(os.Getenv("HTTP_RETRY_WAIT_MAX")) != "" {
waitMax, err := strconv.Atoi(os.Getenv("HTTP_RETRY_WAIT_MAX"))
if err != nil {
log.Warn().Err(err).Msgf("Error occured while converting string to integer")
}
if waitMax > 0 {
retryClient.RetryWaitMax = time.Duration(waitMax) * time.Second
}
}
retryClient.Logger = nil
retryClient.RequestLogHook = func(_ retryablehttp.Logger, req *http.Request, attempt int) {
retryClient.ResponseLogHook = func(_ retryablehttp.Logger, resp *http.Response) {
wait := retryClient.Backoff(retryClient.RetryWaitMin, retryClient.RetryWaitMax, attempt, resp)
if resp.StatusCode == 0 || (resp.StatusCode >= 500 && resp.StatusCode != 501) {
if req.Header.Get("X-Request-Id") != "" {
log.Warn().Str("requestId", req.Header.Get("X-Request-Id")).Msgf("Error occured while performing http request method %s %s with statusCode %d for attempt number %d, retrying in %s", req.Method, req.URL, resp.StatusCode, attempt, wait)
} else {
log.Warn().Msgf("Error occured while performing http request method %s %s with statusCode %d for attempt number %d, retrying in %s", req.Method, req.URL, resp.StatusCode, attempt, wait)
}
}
}
}
return retryClient.StandardClient()
}
// PerformRequests permit to perform HTTP requests
func PerformRequests(headers map[string]string, method string, url string, payload string, retryProvider string) (body []byte, resp *http.Response, err error) {
var (
req *http.Request
)
switch retryProvider {
default:
standardClient := retryQuery()
switch method {
case "GET":
req, err = http.NewRequest(method, url, nil)
case "POST":
req, err = http.NewRequest(method, url, strings.NewReader(payload))
default:
if payload == "" {
req, err = http.NewRequest(method, url, nil)
} else {
req, err = http.NewRequest(method, url, strings.NewReader(payload))
}
}
if err != nil {
log.Error().Err(err).Msgf("Error occured while initializing http request")
return nil, nil, err
}
if len(headers) > 0 {
for k, v := range headers {
req.Header.Set(k, v)
}
}
if payload != "" {
req.Header.Set("Content-Length", strconv.Itoa(len(payload)))
log.Debug().Msgf("%s %s headers: %s body %v", req.Method, req.URL.Redacted(), req.Header, payload)
} else {
log.Debug().Msgf("%s %s headers: %s body %v", req.Method, req.URL.Redacted(), req.Header, nil)
}
resp, err = standardClient.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
log.Debug().Msgf("%s %s statusCode %d headers: %s body %v", req.Method, req.URL.Redacted(), resp.StatusCode, resp.Header, string(body))
}
return body, resp, err
}