-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpClient.go
134 lines (120 loc) · 2.72 KB
/
httpClient.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 main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"sort"
"strconv"
)
func httpRequest(method string, url string, query map[string]string, body interface{}, header map[string]string) (data []byte, err error) {
client := &http.Client{}
var requestReader io.Reader
if body != nil {
requestByte, err := json.Marshal(body)
if err != nil {
return nil, err
}
requestReader = bytes.NewReader(requestByte)
}
req, err := http.NewRequest(method, url, requestReader)
if err != nil {
return nil, err
}
for k, v := range header {
req.Header.Add(k, v)
}
if query != nil {
q := req.URL.Query()
for k, v := range query {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if res.StatusCode >= 300 {
return nil, errors.New(string(b))
}
return b, nil
}
func httpRequestByPage(method string, url string, query map[string]string, body *struct{}, header map[string]string, page int) (data []byte, err error) {
query["page"] = strconv.Itoa(page)
return httpRequest(method, url, query, body, header)
}
func getVars() (l *Varlist, err error) {
l = &Varlist{}
i := 1
for {
data, err := httpRequestByPage(
"GET",
fmt.Sprintf(gitlabURL, projectID),
map[string]string{},
nil,
map[string]string{"PRIVATE-TOKEN": gitlabToken, "Content-Type": "application/json"},
i,
)
if err != nil {
return nil, err
}
tmp := &Varlist{}
if err = json.Unmarshal(data, &tmp); err != nil {
return nil, err
}
if len(*tmp) != 0 {
*l = append(*l, *tmp...)
i++
} else {
sort.Slice(*l, func(i, j int) bool {
return (*l)[i].Key < (*l)[j].Key
})
return l, nil
}
}
}
func createVars(v *GitlabVar) error {
_, err := httpRequest(
"POST",
fmt.Sprintf(gitlabURL, projectID),
map[string]string{"filter[environment_scope]": v.EnvironmentScope},
v,
map[string]string{"PRIVATE-TOKEN": gitlabToken, "Content-Type": "application/json"},
)
if err != nil {
return err
}
return nil
}
func updateVars(v *GitlabVar) error {
_, err := httpRequest(
"PUT",
fmt.Sprintf(gitlabURL+"/"+v.Key, projectID),
map[string]string{"filter[environment_scope]": v.EnvironmentScope},
v,
map[string]string{"PRIVATE-TOKEN": gitlabToken, "Content-Type": "application/json"},
)
if err != nil {
return err
}
return nil
}
func deleteVars(v *GitlabVar) error {
_, err := httpRequest(
"DELETE",
fmt.Sprintf(gitlabURL+"/"+v.Key, projectID),
map[string]string{"filter[environment_scope]": v.EnvironmentScope},
v,
map[string]string{"PRIVATE-TOKEN": gitlabToken, "Content-Type": "application/json"},
)
if err != nil {
return err
}
return nil
}