This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathclient.go
327 lines (260 loc) · 7.03 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package client
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
errors2 "github.com/pkg/errors"
"github.com/TykTechnologies/tyk-operator/api/model"
"github.com/TykTechnologies/tyk-operator/api/v1alpha1"
"github.com/TykTechnologies/tyk-operator/pkg/environmet"
"github.com/go-logr/logr"
)
var (
// ErrTODO is returned when a feature is not yet implemented
ErrTODO = errors.New("TODO: This feature is not implemented yet")
// ErrNotFound is returned when an api call returns 404
ErrNotFound = errors.New("Resource not found")
// ErrFailed represents errors occurred during API calls to Tyk.
ErrFailed = errors.New("Failed api call")
)
func IsTODO(err error) bool {
return errors.Is(err, ErrTODO)
}
// IsNotFound returns true if err is ErrNotFound
func IsNotFound(err error) bool {
return errors.Is(err, ErrNotFound)
}
// IgnoreNotFound returns nil if err is ErrNotFound
func IgnoreNotFound(err error) error {
if !IsNotFound(err) {
return err
}
return nil
}
var client = &http.Client{}
func init() {
if os.Getenv(v1alpha1.SkipVerify) == "true" {
client.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
}
func JSON(res *http.Response, o interface{}) error {
return json.NewDecoder(res.Body).Decode(o)
}
func Do(r *http.Request) (*http.Response, error) {
return client.Do(r)
}
// Context information needed to make a successful http api call.
type Context struct {
Env environmet.Env
Log logr.Logger
BeforeRequest func(*http.Request)
Do func(*http.Request) (*http.Response, error)
}
type contextKey struct{}
func SetContext(ctx context.Context, rctx Context) context.Context {
return context.WithValue(ctx, contextKey{}, rctx)
}
func GetContext(ctx context.Context) Context {
if c := ctx.Value(contextKey{}); c != nil {
return c.(Context)
}
return Context{
Log: logr.Discard(),
}
}
func Request(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) {
r, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, err
}
BeforeRequest(r)
return r, nil
}
func CallJSON(ctx context.Context, method, url string, body interface{}, fn ...func(*http.Request)) (*http.Response, error) {
b, err := model.Marshal(body)
if err != nil {
return nil, err
}
fn = append(fn, AddHeaders(map[string]string{
"Content-Type": "application/json",
}))
return Call(ctx, method, url, bytes.NewReader(b), fn...)
}
func Get(ctx context.Context, url string, body io.Reader, fn ...func(*http.Request)) (*http.Response, error) {
return Call(ctx, http.MethodGet, url, body, fn...)
}
func Post(ctx context.Context, url string, body io.Reader, fn ...func(*http.Request)) (*http.Response, error) {
return Call(ctx, http.MethodPost, url, body, fn...)
}
func PostJSON(ctx context.Context, url string, body interface{}, fn ...func(*http.Request)) (*http.Response, error) {
return CallJSON(ctx, http.MethodPost, url, body, fn...)
}
func PutJSON(ctx context.Context, url string, body interface{}, fn ...func(*http.Request)) (*http.Response, error) {
return CallJSON(ctx, http.MethodPut, url, body, fn...)
}
func Delete(ctx context.Context, url string, body io.Reader, fn ...func(*http.Request)) (*http.Response, error) {
return Call(ctx, http.MethodDelete, url, body, fn...)
}
func Call(ctx context.Context, method, url string, body io.Reader, fn ...func(*http.Request)) (*http.Response, error) {
rctx := GetContext(ctx)
url = JoinURL(rctx.Env.URL, url)
r, err := Request(ctx, method, url, body)
if err != nil {
return nil, err
}
for _, f := range fn {
f(r)
}
var res *http.Response
if rctx.Do != nil {
res, err = rctx.Do(r)
} else {
res, err = client.Do(r)
}
values := []interface{}{
"Method", method, "URL", url,
}
if res != nil {
values = append(values, "Status", res.StatusCode)
} else {
if err != nil {
values = append(values, "Status", err.Error())
} else {
values = append(values, "Status undefined error")
}
}
rctx.Log.Info("Call", values...)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
defer res.Body.Close()
b, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if len(b) > 0 {
rctx.Log.Info(http.StatusText(res.StatusCode), "body", string(b))
}
switch res.StatusCode {
case http.StatusNotFound:
return nil, ErrNotFound
default:
return nil, errors2.Wrap(ErrFailed, string(b))
}
}
return res, err
}
// AddQuery call back for adding url queries
func AddQuery(q url.Values) func(*http.Request) {
return func(h *http.Request) {
if len(q) == 0 {
return
}
query := h.URL.Query()
for k, v := range q {
query[k] = append(query[k], v...)
}
h.URL.RawQuery = query.Encode()
}
}
func AddHeaders(q map[string]string) func(*http.Request) {
return func(h *http.Request) {
for k, v := range q {
h.Header.Add(k, v)
}
}
}
func SetHeaders(q map[string]string) func(*http.Request) {
return func(h *http.Request) {
for k, v := range q {
h.Header.Set(k, v)
}
}
}
// Error dumps whole response plus body and return it as an error
func Error(res *http.Response) error {
b, err := io.ReadAll(res.Body)
if err != nil {
return err
}
return fmt.Errorf("%d API call failed with %v", res.StatusCode, string(b))
}
// JoinURL returns addition of parts to the base e.URL
func JoinURL(base string, parts ...string) string {
return Join(append([]string{base}, parts...)...)
}
func Join(parts ...string) string {
l := len(parts)
if l == 1 {
return parts[0]
}
ps := make([]string, l)
for i, part := range parts {
if i == 0 {
ps[i] = strings.TrimRight(part, "/")
} else {
ps[i] = strings.TrimLeft(part, "/")
}
}
return strings.Join(ps, "/")
}
func BeforeRequest(r *http.Request) {
ctx := GetContext(r.Context())
r.Header.Set("content-type", "application/json")
switch ctx.Env.Mode {
case "pro":
r.Header.Set("authorization", ctx.Env.Auth)
case "ce":
r.Header.Set("x-tyk-authorization", ctx.Env.Auth)
}
if ctx.BeforeRequest != nil {
ctx.BeforeRequest(r)
}
}
func LError(ctx context.Context, err error, msg string, kv ...interface{}) {
GetContext(ctx).Log.Error(err, msg, kv...)
}
func LInfo(ctx context.Context, msg string, kv ...interface{}) {
GetContext(ctx).Log.Info(msg, kv...)
}
func Result(res *http.Response, err error) (*model.Result, error) {
var m model.Result
if e := Data(&m)(res, err); e != nil {
return nil, e
}
return &m, nil
}
func Data(o interface{}) func(*http.Response, error) error {
return func(res *http.Response, err error) error {
if err != nil {
return err
}
defer res.Body.Close()
return json.NewDecoder(res.Body).Decode(o)
}
}