-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (92 loc) · 2.48 KB
/
main.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
package connector
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"github.com/google/jsonapi"
conn "gitlab.com/distributed_lab/json-api-connector"
"gitlab.com/distributed_lab/json-api-connector/cerrors"
iface "gitlab.com/distributed_lab/json-api-connector/client"
"gitlab.com/distributed_lab/logan/v3"
)
const privatePrefix = "/integrations/rarime-points-svc/v1/private"
type Client struct {
disabled bool
log *logan.Entry
conn *conn.Connector
}
func NewClient(cli iface.Client) *Client {
return &Client{conn: conn.NewConnector(cli), log: logan.New()}
}
func (c *Client) FulfillEvent(ctx context.Context, req FulfillEventRequest) *Error {
if c.disabled {
c.log.Info("Points connector disabled")
return nil
}
u, _ := url.Parse(privatePrefix + "/events")
err := c.conn.PatchJSON(u, req, ctx, nil)
if err == nil {
return nil
}
baseErr := err
code, err := extractErrCode(err)
if err != nil {
return &Error{
err: fmt.Errorf("failed to extract error code: %w; base error: %w", err, baseErr),
}
}
return &Error{
Code: code,
err: baseErr,
}
}
func (c *Client) FulfillVerifyProofEvent(ctx context.Context, req FulfillVerifyProofEventRequest) *Error {
// Deprecated: We will not share any data
// and that connector currently not used
if c.disabled {
c.log.Info("Points connector disabled")
return nil
}
u, _ := url.Parse(privatePrefix + "/proofs")
err := c.conn.PatchJSON(u, req, ctx, nil)
if err == nil {
return nil
}
baseErr := err
code, err := extractErrCode(err)
if err != nil {
return &Error{
err: fmt.Errorf("failed to extract error code: %w; base error: %w", err, baseErr),
}
}
return &Error{
Code: code,
err: baseErr,
}
}
func (c *Client) VerifyPassport(ctx context.Context, req VerifyPassportRequest) error {
// Deprecated: VerifyPassport whould be public endpoint
// and that connector currently not used
if c.disabled {
c.log.Info("Points connector disabled")
return nil
}
u, _ := url.Parse(privatePrefix + "/balances")
return c.conn.PatchJSON(u, req, ctx, nil)
}
func extractErrCode(err error) (ErrorCode, error) {
var apiErr cerrors.Error
if !errors.As(err, &apiErr) {
return "", errors.New("unknown error type")
}
var errs jsonapi.ErrorsPayload
if errUn := json.Unmarshal(apiErr.Body(), &errs); errUn != nil {
return "", fmt.Errorf("failed to unmarshal response: %w", errUn)
}
if len(errs.Errors) == 0 {
return "", errors.New("empty errors payload")
}
return ErrorCode(errs.Errors[0].Code), nil
}