-
Notifications
You must be signed in to change notification settings - Fork 0
/
mono.go
131 lines (97 loc) · 3.23 KB
/
mono.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
package monobank
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
)
type PublicAPI interface {
// Currency https://api.monobank.ua/docs/#/definitions/CurrencyInfo
Currency(context.Context) (Currencies, error)
}
type PersonalAPI interface {
PublicAPI
// ClientInfo - https://api.monobank.ua/docs/#/definitions/UserInfo
ClientInfo(context.Context) (*ClientInfo, error)
// Transactions - gets bank account statements(transations)
// https://api.monobank.ua/docs/#/definitions/StatementItems
Transactions(ctx context.Context, accountID string, from, to time.Time) (Transactions, error)
// SetWebHook - sets webhook for statements
SetWebHook(ctx context.Context, uri string) error
}
type CorporateAPI interface {
PersonalAPI
// Auth initializes access.
// Use corporate authorizer for it
Auth(ctx context.Context, callbackURL string) (*TokenRequest, error)
// CheckAuth checks status of request for client's personal data.
CheckAuth(context.Context) error
}
const urlPathAuth = "/personal/auth/request"
func (c Client) Currency(ctx context.Context) (Currencies, error) {
const urlPath = "/bank/currency"
req, err := http.NewRequest(http.MethodGet, urlPath, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}
var v Currencies
err = c.do(ctx, req, &v, http.StatusOK)
return v, err
}
func (c Client) ClientInfo(ctx context.Context) (*ClientInfo, error) {
const urlPath = "/personal/client-info"
req, err := http.NewRequest(http.MethodGet, urlPath, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}
var v ClientInfo
err = c.do(ctx, req, &v, http.StatusOK)
return &v, err
}
// TODO: make `to` optional
func (c Client) Transactions(ctx context.Context, accountID string, from, to time.Time) (
Transactions, error) {
const urlPath = "/personal/statement"
uri := fmt.Sprintf("%s/%s/%d/%d", urlPath, accountID, from.Unix(), to.Unix())
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}
var v Transactions
err = c.do(ctx, req, &v, http.StatusOK)
return v, err
}
func (c Client) SetWebHook(ctx context.Context, uri string) error {
const urlPath = "/personal/webhook"
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(WebHookRequest{WebHookURL: uri})
if err != nil {
return errors.Wrap(err, "failed to marshal")
}
req, err := http.NewRequest(http.MethodPost, urlPath, &buf)
if err != nil {
return errors.Wrap(err, "failed to create request")
}
return c.do(ctx, req, nil, http.StatusOK)
}
func (c Client) Auth(ctx context.Context, callbackURL string) (*TokenRequest, error) {
req, err := http.NewRequest(http.MethodPost, urlPathAuth, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}
req.Header.Set("X-Callback", callbackURL)
var v TokenRequest
err = c.do(ctx, req, &v, http.StatusOK)
return &v, err
}
// CheckAuth checks status of request for client's personal data.
func (c Client) CheckAuth(ctx context.Context) error {
req, err := http.NewRequest(http.MethodGet, urlPathAuth, nil)
if err != nil {
return errors.Wrap(err, "failed to create request")
}
return c.do(ctx, req, nil, http.StatusOK)
}