-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
http.go
44 lines (40 loc) · 1.08 KB
/
http.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
package fixture
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"github.com/argoproj/argo-cd/v2/common"
)
// DoHttpRequest executes a http request against the Argo CD API server
func DoHttpRequest(method string, path string, data ...byte) (*http.Response, error) {
reqUrl, err := url.Parse(path)
if err != nil {
return nil, err
}
reqUrl.Scheme = "http"
reqUrl.Host = apiServerAddress
var body io.Reader
if data != nil {
body = bytes.NewReader(data)
}
req, err := http.NewRequest(method, reqUrl.String(), body)
if err != nil {
return nil, err
}
req.AddCookie(&http.Cookie{Name: common.AuthCookieName, Value: token})
return http.DefaultClient.Do(req)
}
// DoHttpJsonRequest executes a http request against the Argo CD API server and unmarshals the response body as JSON
func DoHttpJsonRequest(method string, path string, result interface{}, data ...byte) error {
resp, err := DoHttpRequest(method, path, data...)
if err != nil {
return err
}
responseData, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return json.Unmarshal(responseData, result)
}