-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
107 lines (80 loc) · 2.44 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
package frame
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
// InvokeRestService convenience method to call a http endpoint and utilize the raw results
func (s *Service) InvokeRestService(ctx context.Context,
method string, endpointURL string, payload map[string]any,
headers map[string][]string) (int, []byte, error) {
if headers == nil {
headers = map[string][]string{
"Content-Type": {"application/json"},
"Accept": {"application/json"},
}
}
var body io.Reader
if payload != nil {
postBody, err := json.Marshal(payload)
if err != nil {
return 0, nil, err
}
body = bytes.NewBuffer(postBody)
}
req, err := http.NewRequestWithContext(ctx, method, endpointURL, body)
if err != nil {
return 0, nil, err
}
req.Header = headers
reqDump, _ := httputil.DumpRequestOut(req, true)
s.L().WithField("request", string(reqDump)).Debug("request out")
resp, err := s.client.Do(req)
if err != nil {
return 0, nil, err
}
respDump, _ := httputil.DumpResponse(resp, true)
s.L().WithField("response", string(respDump)).Debug("response in")
defer resp.Body.Close()
response, err := io.ReadAll(resp.Body)
return resp.StatusCode, response, err
}
// InvokeRestServiceUrlEncoded convenience method to call a http endpoint and utilize the raw results
func (s *Service) InvokeRestServiceUrlEncoded(ctx context.Context,
method string, endpointURL string, payload url.Values,
headers map[string]string) (int, []byte, error) {
if headers == nil {
headers = map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
}
}
logger := s.L().WithField("method", method).WithField("endpoint", endpointURL).WithField("header", headers)
req, err := http.NewRequestWithContext(ctx, method, endpointURL, strings.NewReader(payload.Encode()))
if err != nil {
return 0, nil, err
}
for key, val := range headers {
req.Header.Set(key, val)
}
reqDump, _ := httputil.DumpRequestOut(req, true)
logger.WithField("request", string(reqDump)).Info("request out")
resp, err := s.client.Do(req)
if err != nil {
return 0, nil, err
}
respDump, _ := httputil.DumpResponse(resp, true)
s.L().WithField("response", string(respDump)).Info("response in")
defer func(Body io.ReadCloser) {
err = Body.Close()
if err != nil {
logger.WithError(err).Warn("issue closing body")
}
}(resp.Body)
response, err := io.ReadAll(resp.Body)
return resp.StatusCode, response, err
}