/
requests.go
134 lines (111 loc) · 3.74 KB
/
requests.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
125
126
127
128
129
130
131
132
133
134
package requests
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
"github.com/sirupsen/logrus"
)
var response *http.Response
var requestError error
var client = &http.Client{
Timeout: time.Second * 10,
}
func CheckRateLimit(req *http.Request) {
// loop until the request is successful
for requestError != nil {
// logrus.Error("Error: ", err.Error())
time.Sleep(5 * time.Second)
fmt.Println(" .. .. ")
response, requestError = client.Do(req)
}
// As long as the response.StatusCode is 429, keep retrying until the request is successful
for response.StatusCode == 429 && response != nil {
time.Sleep(5 * time.Second)
fmt.Println(" .. ")
response, requestError = client.Do(req)
}
}
func SendGetRequest(url string, authToken string) (*http.Response, []byte) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
//return nil, fmt.Errorf("Got error %s", err.Error())
}
req.Header.Set("user-agent", "GitlabScannerAPI")
req.Header.Set("Accept", "*/*")
req.Header.Set("Keep-Alive", "timeout=30, max=60")
req.Header.Add("Authorization", fmt.Sprintf("%s%s", "Bearer ", authToken))
// send the request and get the response
response, requestError = client.Do(req)
CheckRateLimit(req)
body, _ := ioutil.ReadAll(response.Body)
// return response.Header, body
return response, body
}
func SendPostRequest(url string, authToken string, requestBody io.Reader) (*http.Response, []byte) {
client := &http.Client{
Timeout: time.Second * 10,
}
req, err := http.NewRequest("POST", url, requestBody)
if err != nil {
//return nil, fmt.Errorf("Got error %s", err.Error())
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("user-agent", "GitlabScannerAPI")
req.Header.Set("Accept", "*/*")
//req.Header.Set("Keep-Alive", "timeout=30, max=60")
req.Header.Add("Authorization", fmt.Sprintf("%s%s", "Bearer ", authToken))
// send the request and get the response
response, requestError = client.Do(req)
CheckRateLimit(req)
body, _ := ioutil.ReadAll(response.Body)
// Check for 200 response code before returning the body
statusOK := response.StatusCode >= 200 && response.StatusCode < 300
if statusOK {
return response, body
} else {
logrus.Errorln(response.StatusCode, " - Error sending POST request. ", string(body))
return response, nil
}
//return response.Header, body
}
func SendPutRequest(url string, authToken string, requestBody io.Reader) (*http.Response, []byte) {
client := &http.Client{
Timeout: time.Second * 10,
}
req, err := http.NewRequest("PUT", url, requestBody)
if err != nil {
//return nil, fmt.Errorf("Got error %s", err.Error())
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("user-agent", "GitlabScannerAPI")
req.Header.Set("Accept", "*/*")
req.Header.Set("Keep-Alive", "timeout=30, max=60")
req.Header.Add("Authorization", fmt.Sprintf("%s%s", "Bearer ", authToken))
// send the request and get the response
response, requestError = client.Do(req)
CheckRateLimit(req)
body, _ := ioutil.ReadAll(response.Body)
// return response.Header, body
return response, body
}
func SendDeleteRequest(url string, authToken string, requestBody io.Reader) (*http.Response, []byte) {
client := &http.Client{
Timeout: time.Second * 10,
}
req, err := http.NewRequest("DELETE", url, requestBody)
if err != nil {
//return nil, fmt.Errorf("Got error %s", err.Error())
}
req.Header.Set("user-agent", "GitlabScannerAPI")
req.Header.Set("Accept", "*/*")
req.Header.Set("Keep-Alive", "timeout=30, max=60")
req.Header.Add("Authorization", fmt.Sprintf("%s%s", "Bearer ", authToken))
// send the request and get the response
response, requestError = client.Do(req)
CheckRateLimit(req)
body, _ := ioutil.ReadAll(response.Body)
// return response.Header, body
return response, body
}