-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.go
139 lines (114 loc) · 3.91 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
package gotsrpc
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
"github.com/ugorji/go/codec"
)
const (
HeaderServiceToService = "X-Foomo-S2S"
)
// ClientTransport to use for calls
// var ClientTransport = &http.Transport{}
var _ Client = &bufferedClient{}
type Client interface {
Call(ctx context.Context, url string, endpoint string, method string, args []interface{}, reply []interface{}) (err error)
SetClientEncoding(encoding ClientEncoding)
SetTransportHttpClient(client *http.Client)
SetDefaultHeaders(headers http.Header)
}
func NewClient() Client {
return &bufferedClient{client: defaultHttpFactory(), handle: getHandleForEncoding(EncodingMsgpack), headers: nil}
}
func NewClientWithHttpClient(client *http.Client) Client { //nolint:stylecheck
if client != nil {
return &bufferedClient{client: client, handle: getHandleForEncoding(EncodingMsgpack), headers: nil}
} else {
return &bufferedClient{client: defaultHttpFactory(), handle: getHandleForEncoding(EncodingMsgpack), headers: nil}
}
}
func newRequest(ctx context.Context, url string, contentType string, buffer *bytes.Buffer, headers http.Header) (r *http.Request, err error) {
if buffer == nil {
buffer = &bytes.Buffer{}
}
request, errRequest := http.NewRequestWithContext(ctx, http.MethodPost, url, buffer)
if errRequest != nil {
return nil, errors.Wrap(errRequest, "could not create a request")
}
if len(headers) > 0 {
request.Header = headers
}
request.Header.Set("Content-Type", contentType)
request.Header.Set("Accept", contentType)
request.Header.Set(HeaderServiceToService, "true")
return request, nil
}
type bufferedClient struct {
client *http.Client
handle *clientHandle
headers http.Header
}
func (c *bufferedClient) SetDefaultHeaders(headers http.Header) {
c.headers = headers
}
func (c *bufferedClient) SetClientEncoding(encoding ClientEncoding) {
c.handle = getHandleForEncoding(encoding)
}
func (c *bufferedClient) SetTransportHttpClient(client *http.Client) { //nolint:stylecheck
c.client = client
}
// Call calls a method on the remove service
func (c *bufferedClient) Call(ctx context.Context, url string, endpoint string, method string, args []interface{}, reply []interface{}) error {
// Marshall args
b := new(bytes.Buffer)
// If no arguments are set, remove
if len(args) > 0 {
if err := codec.NewEncoder(b, c.handle.handle).Encode(args); err != nil {
return NewClientError(errors.Wrap(err, "failed to encode arguments"))
}
}
// Create post url
postURL := fmt.Sprintf("%s%s/%s", url, endpoint, method)
// Create request
request, errRequest := newRequest(ctx, postURL, c.handle.contentType, b, c.headers.Clone())
if errRequest != nil {
return NewClientError(errors.Wrap(errRequest, "failed to create request"))
}
resp, errDo := c.client.Do(request)
if errDo != nil {
return NewClientError(errors.Wrap(errDo, "failed to send request"))
}
defer resp.Body.Close()
// Check status
if resp.StatusCode != http.StatusOK {
var msg string
if value, err := io.ReadAll(resp.Body); err != nil {
msg = "failed to read response body: " + err.Error()
} else {
msg = string(value)
}
return NewClientError(NewHTTPError(msg, resp.StatusCode))
}
clientHandle := getHandlerForContentType(resp.Header.Get("Content-Type"))
wrappedReply := reply
if clientHandle.beforeDecodeReply != nil {
if value, err := clientHandle.beforeDecodeReply(reply); err != nil {
return NewClientError(errors.Wrap(err, "failed to call beforeDecodeReply hook"))
} else {
wrappedReply = value
}
}
if err := codec.NewDecoder(resp.Body, clientHandle.handle).Decode(wrappedReply); err != nil {
return NewClientError(errors.Wrap(err, "failed to decode response"))
}
// replace error
if clientHandle.afterDecodeReply != nil {
if err := clientHandle.afterDecodeReply(&reply, wrappedReply); err != nil {
return NewClientError(errors.Wrap(err, "failed to call afterDecodeReply hook"))
}
}
return nil
}