-
Notifications
You must be signed in to change notification settings - Fork 28
/
client.go
113 lines (97 loc) · 3.31 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
/*
* Copyright 2018 The Service Manager Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package util
import (
"bytes"
"context"
"encoding/json"
"github.com/gofrs/uuid"
"io"
"io/ioutil"
"net/http"
"github.com/Peripli/service-manager/pkg/log"
)
// DoRequestFunc is an alias for any function that takes an http request and returns a response and error
type DoRequestFunc func(request *http.Request) (*http.Response, error)
func BasicAuthDecorator(username, password string, requestFunc DoRequestFunc) DoRequestFunc {
return func(req *http.Request) (*http.Response, error) {
req.SetBasicAuth(username, password)
return requestFunc(req)
}
}
// SendRequest sends a request to the specified client and the provided URL with the specified parameters and body.
func SendRequest(ctx context.Context, doRequest DoRequestFunc, method, url string, params map[string]string, body interface{}) (*http.Response, error) {
return SendRequestWithHeaders(ctx, doRequest, method, url, params, body, map[string]string{})
}
// SendRequestWithHeaders sends a request to the specified client and the provided URL with the specified parameters, body and headers.
func SendRequestWithHeaders(ctx context.Context, doRequest DoRequestFunc, method, url string, params map[string]string, body interface{}, headers map[string]string) (*http.Response, error) {
var bodyReader io.Reader
if body != nil {
bodyBytes, err := json.Marshal(body)
if err != nil {
return nil, err
}
bodyReader = bytes.NewReader(bodyBytes)
}
request, err := http.NewRequest(method, url, bodyReader)
if err != nil {
return nil, err
}
for key, value := range headers {
request.Header.Add(key, value)
}
if params != nil {
q := request.URL.Query()
for k, v := range params {
q.Set(k, v)
}
request.URL.RawQuery = q.Encode()
}
request = request.WithContext(ctx)
logger := log.C(ctx)
correlationID, exists := logger.Data[log.FieldCorrelationID].(string)
if exists && correlationID != "-" {
request.Header.Set(log.CorrelationIDHeaders[0], correlationID)
} else {
uuids, err := uuid.NewV4()
if err == nil {
request.Header.Set(log.CorrelationIDHeaders[0], uuids.String())
}
}
logger.Debugf("Sending request %s %s", request.Method, request.URL)
return doRequest(request)
}
// BodyToBytes of the request inside given struct
func BodyToBytes(closer io.ReadCloser) ([]byte, error) {
defer func() {
if err := closer.Close(); err != nil {
log.D().Errorf("ReadCloser couldn't be closed: %v", err)
}
}()
body, err := ioutil.ReadAll(closer)
if err != nil {
return nil, err
}
return body, nil
}
// BodyToObject of the request inside given struct
func BodyToObject(closer io.ReadCloser, object interface{}) error {
body, err := BodyToBytes(closer)
if err != nil {
return err
}
return BytesToObject(body, object)
}