-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathclient.go
157 lines (131 loc) · 3.57 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
package cfclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// Client token, host, htpp.Client
type Client struct {
Token string
TokenHeader string
Host string
HostV2 string
featureFlags map[string]bool
Client *http.Client
}
// RequestOptions path, method, etc
type RequestOptions struct {
Path string
Method string
Body []byte
QS map[string]string
XAccessToken string
}
// NewClient returns a new client configured to communicate on a server with the
// given hostname and to send an Authorization Header with the value of
// token
func NewClient(hostname string, hostnameV2 string, token string, tokenHeader string) *Client {
if tokenHeader == "" {
tokenHeader = "Authorization"
}
return &Client{
Host: hostname,
HostV2: hostnameV2,
Token: token,
TokenHeader: tokenHeader,
Client: &http.Client{},
featureFlags: map[string]bool{},
}
}
// RequestAPI http request to Codefresh API
func (client *Client) RequestAPI(opt *RequestOptions) ([]byte, error) {
finalURL := fmt.Sprintf("%s%s", client.Host, opt.Path)
if opt.QS != nil {
finalURL += ToQS(opt.QS)
}
request, err := http.NewRequest(opt.Method, finalURL, bytes.NewBuffer(opt.Body))
if err != nil {
return nil, err
}
tokenHeader := client.TokenHeader
if tokenHeader == "" {
tokenHeader = "Authorization"
}
request.Header.Set(tokenHeader, client.Token)
request.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := client.Client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Failed to read body %v %v", resp.StatusCode, resp.Status)
}
// todo: maybe other 2**?
if resp.StatusCode != 200 && resp.StatusCode != 201 {
return nil, fmt.Errorf("%v, %s", resp.Status, string(body))
}
return body, nil
}
func (client *Client) RequestApiXAccessToken(opt *RequestOptions) ([]byte, error) {
finalURL := fmt.Sprintf("%s%s", client.Host, opt.Path)
if opt.QS != nil {
finalURL += ToQS(opt.QS)
}
request, err := http.NewRequest(opt.Method, finalURL, bytes.NewBuffer(opt.Body))
if err != nil {
return nil, err
}
request.Header.Set("x-access-token", opt.XAccessToken)
request.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := client.Client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Failed to read body %v %v", resp.StatusCode, resp.Status)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("%v, %s", resp.Status, string(body))
}
return body, nil
}
func (client *Client) isFeatureFlagEnabled(flagName string) (bool, error) {
if len(client.featureFlags) == 0 {
currAcc, err := client.GetCurrentAccount()
if err != nil {
return false, err
}
client.featureFlags = currAcc.FeatureFlags
}
if val, ok := client.featureFlags[flagName]; ok {
return val, nil
}
return false, nil
}
// ToQS add extra parameters to path
func ToQS(qs map[string]string) string {
var arr = []string{}
for k, v := range qs {
arr = append(arr, fmt.Sprintf("%s=%s", k, v))
}
return "?" + strings.Join(arr, "&")
}
// DecodeResponseInto json Unmarshall
func DecodeResponseInto(body []byte, target interface{}) error {
return json.Unmarshal(body, target)
}
// EncodeToJSON json Marshal
func EncodeToJSON(object interface{}) ([]byte, error) {
body, err := json.Marshal(object)
if err != nil {
return nil, err
}
return body, nil
}