-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
90 lines (71 loc) · 2.42 KB
/
snapshot.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
package rc
import (
"context"
"fmt"
"strconv"
"time"
"github.com/shopspring/decimal"
)
// Snapshot Snapshot
type Snapshot struct {
SnapshotID string `json:"snapshot_id,omitempty"`
TraceID string `json:"trace_id,omitempty"`
AssetID string `json:"asset_id,omitempty"`
Amount decimal.Decimal `json:"amount,omitempty"`
UserID string `json:"user_id,omitempty"`
OpponentID string `json:"opponent_id,omitempty"`
Memo string `json:"memo,omitempty"`
Type string `json:"type,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
Asset *Asset `json:"asset,omitempty"`
}
func (c *Client) ReadSnapshots(ctx context.Context, assetID string, offset time.Time, order string, limit int) ([]*Snapshot, error) {
var snapshots []*Snapshot
params := buildReadSnapshotsParams(assetID, offset, order, limit)
if err := c.Get(ctx, fmt.Sprintf("/v1/app/accounts/%s/snapshots", c.ClientID), params, &snapshots); err != nil {
return nil, err
}
return snapshots, nil
}
func (c *Client) ReadSnapshot(ctx context.Context, snapshotID string) (*Snapshot, error) {
var snapshot Snapshot
if err := c.Get(ctx, fmt.Sprintf("/v1/app/accounts/%s/snapshots/%s", c.ClientID, snapshotID), nil, &snapshot); err != nil {
return nil, err
}
return &snapshot, nil
}
func (c *Client) ReadPublicSnapshots(ctx context.Context, assetID string, offset time.Time, order string, limit int) ([]*Snapshot, error) {
var snapshots []*Snapshot
params := buildReadSnapshotsParams(assetID, offset, order, limit)
if err := c.Get(ctx, "/v1/public/snapshots", params, &snapshots); err != nil {
return nil, err
}
return snapshots, nil
}
func (c *Client) ReadPublicSnapshot(ctx context.Context, snapshotID string) (*Snapshot, error) {
uri := fmt.Sprintf("/v1/public/snapshots/%s", snapshotID)
var snapshot Snapshot
if err := c.Get(ctx, uri, nil, &snapshot); err != nil {
return nil, err
}
return &snapshot, nil
}
func buildReadSnapshotsParams(assetID string, offset time.Time, order string, limit int) map[string]string {
params := make(map[string]string)
if assetID != "" {
params["asset"] = assetID
}
if !offset.IsZero() {
params["offset"] = offset.UTC().Format(time.RFC3339Nano)
}
switch order {
case "ASC", "DESC":
default:
order = "DESC"
}
params["order"] = order
if limit > 0 {
params["limit"] = strconv.Itoa(limit)
}
return params
}