forked from harness/harness-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
111 lines (94 loc) · 2.69 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
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
)
func Post(reqUrl string, auth string, body interface{}, contentType string) (respBodyObj ResponseBody, err error) {
postBody, _ := json.Marshal(body)
requestBody := bytes.NewBuffer(postBody)
log.WithFields(log.Fields{
"body": string(postBody),
}).Debug("The request body")
req, err := http.NewRequest("POST", reqUrl, requestBody)
if err != nil {
return
}
req.Header.Set("Content-Type", contentType)
req.Header.Set(AuthHeaderKey(auth), auth)
if err != nil {
log.Fatalln(err)
}
//fmt.Printf("Request = %s\n", string(b))
return handleResp(req)
}
func Put(reqUrl string, auth string, body interface{}, contentType string) (respBodyObj ResponseBody, err error) {
postBody, _ := json.Marshal(body)
requestBody := bytes.NewBuffer(postBody)
log.WithFields(log.Fields{
"body": string(postBody),
}).Debug("The request body")
req, err := http.NewRequest("PUT", reqUrl, requestBody)
if err != nil {
return
}
req.Header.Set("Content-Type", contentType)
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
}
req.Header.Set("Content-Type", CONTENT_TYPE_JSON)
req.Header.Set(AuthHeaderKey(auth), cliCdRequestData.AuthToken)
return handleResp(req)
}
func Delete(reqUrl string, auth string) (respBodyObj ResponseBody, err error) {
req, err := http.NewRequest("DELETE", reqUrl, nil)
if err != nil {
return
}
req.Header.Set("Content-Type", CONTENT_TYPE_JSON)
req.Header.Set(AuthHeaderKey(auth), auth)
return handleResp(req)
}
func handleResp(req *http.Request) (respBodyObj ResponseBody, err error) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return
}
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"
}