-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth_core.go
178 lines (150 loc) · 3.79 KB
/
auth_core.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
package mono
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"
)
// Authorizer is an interface for different types of MonoBank API authorization.
type Authorizer interface {
Auth(request *http.Request) error
}
type authCore struct {
core
auth Authorizer
}
func newAuthCore(auth Authorizer) *authCore {
return &authCore{
auth: auth,
core: *newCore(),
}
}
// GetJSON builds the full endpoint path and gets the raw JSON.
func (ac *authCore) GetJSON(ctx context.Context, endpoint string, headers map[string]string) ([]byte, int, error) {
uri, err := ac.buildURL(endpoint)
if err != nil {
return nil, 0, err
}
r, err := http.NewRequestWithContext(ctx, "GET", uri, nil)
if err != nil {
return nil, 0, err
}
if err := ac.auth.Auth(r); err != nil {
return nil, 0, err
}
// Set headers.
for k, v := range headers {
r.Header.Set(k, v)
}
resp, err := ac.Do(r)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body, resp.StatusCode, err
}
// PostJSON builds the full endpoint path and gets the raw JSON.
func (ac *authCore) PostJSON(
ctx context.Context,
endpoint string,
headers map[string]string,
payload io.Reader,
) ([]byte, int, error) {
uri, err := ac.buildURL(endpoint)
if err != nil {
return nil, 0, err
}
r, err := http.NewRequestWithContext(ctx, "POST", uri, payload)
if err != nil {
return nil, 0, err
}
if err := ac.auth.Auth(r); err != nil {
return nil, 0, err
}
// Set headers.
for k, v := range headers {
r.Header.Set(k, v)
}
resp, err := ac.Do(r)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body, resp.StatusCode, err
}
// User returns user personal information from MonoBank API.
// See https://api.monobank.ua/docs/#operation--personal-client-info-get for details.
func (ac *authCore) User(ctx context.Context, headers map[string]string) (*UserInfo, error) {
body, status, err := ac.GetJSON(ctx, "/personal/client-info", headers)
if err != nil {
return nil, err
}
if status != http.StatusOK {
var msg Error
if err := json.Unmarshal(body, &msg); err != nil {
return nil, errors.New("invalid error payload")
}
return nil, msg
}
var data UserInfo
if err = json.Unmarshal(body, &data); err != nil {
return nil, err
}
return &data, nil
}
// Transactions returns list of transactions from {from} till {to} time.
// See https://api.monobank.ua/docs/#/definitions/StatementItems for details.
func (ac *authCore) Transactions(
ctx context.Context,
account string,
from, to time.Time,
headers map[string]string,
) (
[]Transaction,
error,
) {
path := fmt.Sprintf("/personal/statement/%s/%d/%d", account, from.Unix(), to.Unix())
body, status, err := ac.GetJSON(ctx, path, headers)
if err != nil {
return nil, err
}
if status != http.StatusOK {
var msg Error
if err := json.Unmarshal(body, &msg); err != nil {
return nil, errors.New("invalid error payload")
}
return nil, msg
}
var data []Transaction
if err = json.Unmarshal(body, &data); err != nil {
return nil, err
}
return data, nil
}
// SetWebHook sets WebHook URL for authorized user.
// See https://api.monobank.ua/docs#operation--personal-webhook-post for details.
func (ac *authCore) SetWebHook(ctx context.Context, url string, headers map[string]string) ([]byte, error) {
buff, err := json.Marshal(struct{ WebHookUrl string }{url})
if err != nil {
return nil, err
}
contents, status, err := ac.PostJSON(ctx, "/personal/webhook", headers, bytes.NewReader(buff))
if err != nil {
return nil, err
}
if status != http.StatusOK {
var msg Error
if err := json.Unmarshal(contents, &msg); err != nil {
return nil, errors.New("invalid error payload")
}
return nil, msg
}
return contents, nil
}