-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
177 lines (162 loc) · 4.76 KB
/
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"strconv"
"strings"
)
func Post(reqUrl string, auth string, body interface{}) (respBodyObj ResponseBody, err error) {
postBody, _ := json.Marshal(body)
requestBody := bytes.NewBuffer(postBody)
log.WithFields(log.Fields{
"url": reqUrl,
"body": string(postBody),
}).Trace("The request details")
req, err := http.NewRequest("POST", reqUrl, requestBody)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set(AuthHeaderKey(auth), auth)
return handleResp(req)
}
func Put(reqUrl string, auth string, body io.Reader) (respBodyObj ResponseBody, err error) {
req, err := http.NewRequest("PUT", reqUrl, body)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set(AuthHeaderKey(auth), auth)
return handleResp(req)
}
func Get(reqUrl string, auth string) (respBodyObj ResponseBody, err error) {
req, err := http.NewRequest("GET", reqUrl, nil)
if err != nil {
return
}
log.WithFields(log.Fields{
"url": reqUrl,
}).Trace("The request details")
req.Header.Set("Content-Type", "application/json")
req.Header.Set(AuthHeaderKey(auth), auth)
return handleResp(req)
}
func Delete(reqUrl string, auth string, body interface{}) (respBodyObj ResponseBody, err error) {
var requestBody *bytes.Buffer
if body != nil {
postBody, _ := json.Marshal(body)
requestBody = bytes.NewBuffer(postBody)
log.WithFields(log.Fields{
"url": reqUrl,
"body": string(postBody),
}).Trace("The request details")
} else {
log.WithFields(log.Fields{
"url": reqUrl,
}).Trace("The request details")
}
var req *http.Request
if requestBody != nil {
req, err = http.NewRequest("DELETE", reqUrl, requestBody)
} else {
req, err = http.NewRequest("DELETE", reqUrl, nil)
}
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set(AuthHeaderKey(auth), auth)
if requestBody == nil {
req.ContentLength = 0
}
return handleResp(req)
}
func handleResp(req *http.Request) (respBodyObj ResponseBody, err error) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return
}
log.WithFields(log.Fields{
"body": string(respBody),
}).Trace("The response body")
err = json.Unmarshal(respBody, &respBodyObj)
if err != nil {
log.Fatalln("There was error while parsing the response from server. Exiting...", err)
}
if resp.StatusCode != 200 {
if len(respBodyObj.Message) > 0 {
log.Error(respBodyObj.Message)
} else if len(respBodyObj.Messages) > 0 && len(respBodyObj.Messages[0].Message) > 0 {
log.Error(respBodyObj.Messages[0].Message)
}
return respBodyObj, errors.New("received non 200 response code. The response code was " + strconv.Itoa(resp.StatusCode))
}
return respBodyObj, nil
}
func AuthHeaderKey(auth string) string {
if strings.HasPrefix(auth, "Bearer ") {
return "Authorization"
}
return "x-api-key"
}
func GetWithAuth(host string, query string, authMethod string, base64Auth string, certPath string, keyPath string, insecure bool) (body []byte, err error) {
baseURL := "https://" + host + "/api/v1/" + query
var client *http.Client
req, err := http.NewRequest("GET", baseURL, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// Configure client based on authentication method
if authMethod == authBasic {
// Encode credentials to base64 for the Authorization header
client = &http.Client{}
// Add the Authorization header to the request
req.Header.Add("Authorization", "Basic "+base64Auth)
} else if authMethod == authx509 {
// Load client certificate
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
log.WithError(err).Error("Error loading certificate")
return nil, err
}
// Create a HTTPS client and supply the created CA pool and certificate
config := &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: insecure,
// In a real application, you should adjust the TLS settings according to your security requirements.
}
client = &http.Client{Transport: &http.Transport{TLSClientConfig: config}}
} else {
return nil, fmt.Errorf("unsupported authentication method %s", authMethod)
}
resp, err := client.Do(req)
if err != nil {
log.WithError(err).Error("Error sending request")
return nil, err
}
defer resp.Body.Close()
// Read and print the response body
body, err = io.ReadAll(resp.Body)
if err != nil {
log.Error("Error reading response body:", err)
return nil, err
}
log.Debugf("Found the following pipelines:")
log.Debugf(string(body))
return body, nil
}