-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
118 lines (98 loc) · 2.65 KB
/
api.go
File metadata and controls
118 lines (98 loc) · 2.65 KB
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
package btcindexer
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type Client struct {
baseUrl string
authToken string
c http.Client
}
const (
pathBlocks = "/bitcoin/blocks"
pathLatestHeight = "/bitcoin/latest-height"
pathDepositsBySender = "/bitcoin/deposits/"
)
func NewClient(workerUrl string, authToken string) Client {
return Client{
baseUrl: workerUrl,
authToken: authToken,
c: http.Client{Timeout: time.Second * 30},
}
}
func (c Client) PutBlocks(putBlocks PutBlocksReq) (*http.Response, error) {
bz, err := putBlocks.MarshalMsg(nil)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPut, fmt.Sprint(c.baseUrl, pathBlocks),
bytes.NewReader(bz))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/msgpack")
if c.authToken != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.authToken))
}
return c.c.Do(req)
}
func (c Client) GetLatestHeight(network string) (int64, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprint(c.baseUrl, pathLatestHeight), nil)
if err != nil {
return 0, err
}
q := req.URL.Query()
q.Add("network", network)
req.URL.RawQuery = q.Encode()
resp, err := c.c.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("indexer returned non-200 status: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("failed to read response body: %w", err)
}
var respData LatestHeightResp
if err := json.Unmarshal(body, &respData); err != nil {
return 0, fmt.Errorf("failed to decode indexer height response: %w", err)
}
if respData.Height == nil {
return 0, nil
}
return *respData.Height, nil
}
func (c Client) GetDepositsBySender(senderAddress string, network string) ([]NbtcTxStatusResp, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprint(c.baseUrl, pathDepositsBySender), nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("sender", senderAddress)
q.Add("network", network)
req.URL.RawQuery = q.Encode()
resp, err := c.c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("indexer returned non-200 status: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var respData []NbtcTxStatusResp
if err := json.Unmarshal(body, &respData); err != nil {
return nil, fmt.Errorf("failed to decode indexer deposits by sender response: %w", err)
}
return respData, nil
}