forked from guilhermebr/go-stone-openbank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
396 lines (325 loc) · 8.97 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package openbank
import (
"bytes"
"context"
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"sync"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"github.com/golang-jwt/jwt/v4"
"github.com/sirupsen/logrus"
"github.com/stone-payments/merchant-go-stone-openbank/types"
"go.opentelemetry.io/otel/trace"
"golang.org/x/oauth2"
)
const (
libraryVersion = "1.0"
prodAccountURL = "https://accounts.openbank.stone.com.br"
sandboxAccountURL = "https://sandbox-accounts.openbank.stone.com.br"
prodAPIBaseURL = "https://api.openbank.stone.com.br"
sandboxAPIBaseURL = "https://sandbox-api.openbank.stone.com.br"
prodSiteURL = "https://conta.stone.com.br"
sandboxSiteURL = "https://sandbox.conta.stone.com.br"
userAgent = "merchant-go-stone-openbank/" + libraryVersion
idempotencyKeyMaxSize = 72
)
type Client struct {
client *http.Client
log *logrus.Entry
m *sync.Mutex
debug bool
AccountURL *url.URL
ApiBaseURL *url.URL
SiteURL *url.URL
StonePublicKeys types.StonePublicKeys
ClientID string
ConsentRedirectURL string
privateKeyData []byte // used to build privateKey
privateKey *rsa.PrivateKey
Sandbox bool
UserAgent string
token oauth2.Token
otelTracer trace.Tracer
}
func NewClient(opts ...ClientOpt) (*Client, error) {
accountURL, _ := url.Parse(prodAccountURL)
apiURL, _ := url.Parse(prodAPIBaseURL)
siteURL, _ := url.Parse(prodSiteURL)
c := Client{
client: http.DefaultClient,
UserAgent: userAgent,
AccountURL: accountURL,
ApiBaseURL: apiURL,
SiteURL: siteURL,
StonePublicKeys: make(types.StonePublicKeys),
m: &sync.Mutex{},
}
c.ApplyOpts(opts...)
if len(c.privateKeyData) > 0 {
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(c.privateKeyData)
if err != nil {
return nil, err
}
if privateKey == nil {
return nil, fmt.Errorf("invalid private key")
}
c.privateKey = privateKey
}
// Set log
log := logrus.New().WithFields(logrus.Fields{
"apiURL": c.ApiBaseURL.String(),
"accountURL": c.AccountURL.String(),
"siteURL": c.SiteURL.String(),
})
c.log = log
return &c, nil
}
type ClientOpt func(*Client)
func WithClientID(key string) ClientOpt {
return func(c *Client) {
c.ClientID = key
}
}
func WithTracer(tracer trace.Tracer) ClientOpt {
return func(c *Client) {
c.otelTracer = tracer
}
}
func WithPEMPrivateKey(pk []byte) ClientOpt {
return func(c *Client) {
c.privateKeyData = pk
}
}
func SetConsentURL(url string) ClientOpt {
return func(c *Client) {
c.ConsentRedirectURL = url
}
}
func SetBaseURL(newBaseUrl string) (ClientOpt, error) {
apiBaseURL, err := url.Parse(newBaseUrl)
if err != nil {
return nil, fmt.Errorf("invalid base url: %w", err)
}
return func(c *Client) {
c.ApiBaseURL = apiBaseURL
}, nil
}
func SetAccountURL(newAccountUrl string) (ClientOpt, error) {
accountURL, err := url.Parse(newAccountUrl)
if err != nil {
return nil, fmt.Errorf("invalid account url: %w", err)
}
return func(c *Client) {
c.AccountURL = accountURL
}, nil
}
func WithHttpClient(hc http.Client) ClientOpt {
return func(c *Client) {
c.client = &hc
}
}
func UseSandbox() ClientOpt {
return func(c *Client) {
accountURL, _ := url.Parse(sandboxAccountURL)
apiURL, _ := url.Parse(sandboxAPIBaseURL)
siteURL, _ := url.Parse(sandboxSiteURL)
c.Sandbox = true
c.ApiBaseURL = apiURL
c.AccountURL = accountURL
c.SiteURL = siteURL
}
}
func SetUserAgent(ua string) ClientOpt {
return func(c *Client) {
c.UserAgent = fmt.Sprintf("%s %s", ua, c.UserAgent)
}
}
func EnableDebug() ClientOpt {
return func(c *Client) {
c.debug = true
}
}
func (c *Client) ApplyOpts(opts ...ClientOpt) {
if opts == nil {
return
}
for _, opt := range opts {
opt(c)
}
}
type Response struct {
*http.Response
}
type ErrorResponse struct {
// HTTP response that caused this error
Response *http.Response
// Error message
Message string `json:"message"`
// RequestID returned from the API, useful to contact support.
RequestID string `json:"request_id"`
TransferError interface{} `json:"transfer_error"`
}
type TransferError struct {
Type string `json:"type,omitempty"`
ValidationErrors []struct {
Error string `json:"error,omitempty"`
Path []string `json:"path,omitempty"`
} `json:"validation_errors,omitempty"`
Reason []struct {
Error string `json:"error,omitempty"`
Path []string `json:"path,omitempty"`
} `json:"reason,omitempty"`
}
func (r *ErrorResponse) Error() string {
if r.RequestID != "" {
return fmt.Sprintf("%v %v: %d (request %q) %+v %v",
r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.RequestID, r.TransferError, r.Message)
}
return fmt.Sprintf("%v %v: %d %+v %v",
r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode, r.TransferError, r.Message)
}
func parseBody(data []byte, body interface{}) error {
if len(data) > 0 && body != nil {
if w, ok := body.(io.Writer); ok {
_, err := io.Copy(w, bytes.NewReader(data))
if err != nil {
return err
}
} else {
err := json.Unmarshal(data, body)
if err != nil {
return err
}
return nil
}
}
return nil
}
func CheckResponse(r *http.Response, errorBody interface{}) error {
if c := r.StatusCode; c >= 200 && c <= 299 {
return nil
}
errorResponse := &ErrorResponse{Response: r}
data, err := io.ReadAll(r.Body)
if err == nil {
if err = parseBody(data, errorBody); err != nil {
errorResponse.Message = string(data)
}
errorResponse.TransferError = errorBody
}
return errorResponse
}
// NewAPIRequest creates an API request. A relative URL PATH can be provided in pathStr, which will be resolved to the
// ApiBaseURL of the Client.
func (c *Client) NewAPIRequest(method, pathStr string, body interface{}) (*http.Request, error) {
u, err := c.ApiBaseURL.Parse(pathStr)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if body != nil {
err := json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("User-Agent", c.UserAgent)
return req, nil
}
// AddIdempotencyHeader add in request the header used to realize idempotent operations
func (c *Client) AddIdempotencyHeader(req *http.Request, idempotencyKey string) error {
trimmedIdempotencyKey := strings.TrimSpace(idempotencyKey)
if trimmedIdempotencyKey != "" {
if len(trimmedIdempotencyKey) > idempotencyKeyMaxSize {
return errors.New("invalid idempotency key")
}
req.Header.Add("x-stone-idempotency-key", trimmedIdempotencyKey)
}
return nil
}
func (c *Client) Do(req *http.Request, successResponse, errorResponse interface{}) (*Response, error) {
_, span := c.newSpan(
req.Context(),
"merchant openbank client request",
trace.SpanKindClient,
)
defer c.endSpan(span)
c.addSpanAttribute(span, attribute.String("http.request.path", req.URL.String()))
c.addSpanAttribute(span, attribute.String("http.request.protocol", req.Proto))
c.addSpanAttribute(span, attribute.String("http.request.method", req.Method))
if c.debug {
d, _ := httputil.DumpRequestOut(req, true)
c.log.Infof(">>> REQUEST:\n%s", string(d))
}
resp, err := c.client.Do(req)
if err != nil {
c.setSpanStatus(span, codes.Error, "error executing request")
c.spanRecordError(span, err)
return nil, err
}
c.addSpanAttribute(span, attribute.Int("http.response.status_code", resp.StatusCode))
defer func() {
if rerr := resp.Body.Close(); err == nil {
err = rerr
}
}()
if c.debug {
dr, _ := httputil.DumpResponse(resp, true)
c.log.Infof("<<< RESULT:\n%s", string(dr))
}
response := &Response{Response: resp}
err = CheckResponse(resp, errorResponse)
if err != nil {
c.setSpanStatus(span, codes.Error, "client request error")
c.spanRecordError(span, err)
return response, err
}
data, err := io.ReadAll(resp.Body)
if err = parseBody(data, successResponse); err != nil {
c.setSpanStatus(span, codes.Error, "client request error")
c.spanRecordError(span, err)
return response, err
}
c.setSpanStatus(span, codes.Ok, "client request succeeded")
return response, err
}
func (c *Client) newSpan(ctx context.Context, name string, kind trace.SpanKind) (context.Context, trace.Span) {
if c.otelTracer != nil {
return c.otelTracer.Start(ctx, name, trace.WithSpanKind(kind))
}
return ctx, nil
}
func (c *Client) setSpanStatus(span trace.Span, code codes.Code, description string) {
if span != nil {
span.SetStatus(code, description)
}
}
func (c *Client) spanRecordError(span trace.Span, err error) {
if span != nil {
span.RecordError(err)
}
}
func (c *Client) endSpan(span trace.Span) {
if span != nil {
span.End()
}
}
func (c *Client) addSpanAttribute(span trace.Span, attributes ...attribute.KeyValue) {
if span != nil {
span.SetAttributes(attributes...)
}
}