-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
75 lines (58 loc) · 1.71 KB
/
common.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
package monobank
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type CommonAPI interface {
PublicAPI
// SetWebHook - sets webhook for statements
SetWebHook(ctx context.Context, uri string) error
}
// commonClient contains common to Personal and Corporate API
type commonClient struct {
Client
}
func newCommonClient(client *http.Client) commonClient {
return commonClient{
Client: NewClient(client),
}
}
func (c commonClient) ClientInfo(ctx context.Context) (*ClientInfo, error) {
const urlPath = "/personal/client-info"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlPath, http.NoBody)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
var v ClientInfo
err = c.do(req, &v, http.StatusOK)
return &v, err
}
// TODO: make `to` optional
func (c commonClient) 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.NewRequestWithContext(ctx, http.MethodGet, uri, http.NoBody)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
var v Transactions
err = c.do(req, &v, http.StatusOK)
return v, err
}
func (c commonClient) setWebHook(ctx context.Context, uri, urlPath string) error {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(WebHookRequest{WebHookURL: uri})
if err != nil {
return fmt.Errorf("failed to marshal: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, urlPath, &buf)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
return c.do(req, nil, http.StatusOK)
}