forked from harness/harness-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
127 lines (109 loc) · 3.14 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
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, requestBodyWithFile *bytes.Buffer) (respBodyObj ResponseBody, err error) {
postBody, _ := json.Marshal(body)
requestBody := bytes.NewBuffer(postBody)
var req *http.Request
log.WithFields(log.Fields{
"body": string(postBody),
}).Debug("The request body")
if requestBodyWithFile != nil {
requestBody = requestBodyWithFile
}
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)
}
return handleResp(req)
}
func Put(reqUrl string, auth string, body interface{}, contentType string, requestBodyWithFile *bytes.Buffer) (respBodyObj ResponseBody, err error) {
postBody, _ := json.Marshal(body)
requestBody := bytes.NewBuffer(postBody)
var req *http.Request
log.WithFields(log.Fields{
"body": string(postBody),
}).Debug("The request body")
if requestBodyWithFile != nil {
requestBody = requestBodyWithFile
}
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
}
if resp.StatusCode >= 400 && resp.StatusCode <= 500 {
log.Error("Server returned 'Unauthorized access' code ", resp.StatusCode)
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 resp.StatusCode >= 400 || resp.StatusCode < 500 {
println(respBodyObj.Message)
} else {
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"
}