-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.go
267 lines (229 loc) · 7.99 KB
/
client.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package api
import (
"fmt"
"net/url"
"strconv"
"time"
"go.sia.tech/core/types"
"go.sia.tech/hostd/host/contracts"
"go.sia.tech/hostd/host/metrics"
"go.sia.tech/hostd/host/settings"
"go.sia.tech/hostd/host/storage"
"go.sia.tech/hostd/wallet"
"go.sia.tech/hostd/webhooks"
"go.sia.tech/jape"
)
// A Client is a client for the hostd API.
type Client struct {
c jape.Client
}
// Host returns the current state of the host
func (c *Client) Host() (resp HostState, err error) {
err = c.c.GET("/state/host", &resp)
return
}
// Consensus returns the current consensus state.
func (c *Client) Consensus() (resp ConsensusState, err error) {
err = c.c.GET("/state/consensus", &resp)
return
}
// SyncerAddress returns the address of the syncer.
func (c *Client) SyncerAddress() (addr string, err error) {
err = c.c.GET("/syncer/address", &addr)
return
}
// SyncerPeers returns the currently connected peers of the syncer.
func (c *Client) SyncerPeers() (peers []Peer, err error) {
err = c.c.GET("/syncer/peers", &peers)
return
}
// SyncerConnect connects to a peer.
func (c *Client) SyncerConnect(address string) error {
return c.c.PUT("/syncer/peers", SyncerConnectRequest{address})
}
// SyncerDisconnect disconnects from a peer.
func (c *Client) SyncerDisconnect(address string) error {
return c.c.DELETE(fmt.Sprintf("/syncer/peers/%s", address))
}
// Announce announces the host to the network. The announced address is
// determined by the host's current settings.
func (c *Client) Announce() error {
return c.c.POST("/settings/announce", nil, nil)
}
// Settings returns the current settings of the host.
func (c *Client) Settings() (settings settings.Settings, err error) {
err = c.c.GET("/settings", &settings)
return
}
// UpdateSettings updates the host's settings.
func (c *Client) UpdateSettings(updated ...Setting) (settings settings.Settings, err error) {
values := make(map[string]any)
for _, s := range updated {
s(values)
}
err = c.c.PATCH("/settings", values, &settings)
return
}
// TestDDNS tests the dynamic DNS settings of the host.
func (c *Client) TestDDNS() error {
return c.c.PUT("/settings/ddns/update", nil)
}
// Metrics returns the metrics of the host at the specified time.
func (c *Client) Metrics(at time.Time) (metrics metrics.Metrics, err error) {
v := url.Values{
"timestamp": []string{at.Format(time.RFC3339)},
}
err = c.c.GET("/metrics?"+v.Encode(), &metrics)
return
}
// PeriodMetrics returns the metrics of the host for n periods starting at start.
func (c *Client) PeriodMetrics(start time.Time, n int, interval metrics.Interval) (periods []metrics.Metrics, err error) {
v := url.Values{
"start": []string{start.Format(time.RFC3339)},
"periods": []string{strconv.Itoa(n)},
}
err = c.c.GET("/metrics/"+interval.String()+"?"+v.Encode(), &periods)
return
}
// Contracts returns the contracts of the host matching the filter.
func (c *Client) Contracts(filter contracts.ContractFilter) ([]contracts.Contract, int, error) {
var resp ContractsResponse
err := c.c.POST("/contracts", filter, &resp)
return resp.Contracts, resp.Count, err
}
// Contract returns the contract with the specified ID.
func (c *Client) Contract(id types.FileContractID) (contract contracts.Contract, err error) {
err = c.c.GET("/contracts/"+id.String(), &contract)
return
}
// StartIntegrityCheck scans the volume with the specified ID for consistency errors.
func (c *Client) StartIntegrityCheck(id types.FileContractID) error {
return c.c.PUT(fmt.Sprintf("/contracts/%v/integrity", id), nil)
}
// IntegrityCheckProgress returns the progress of the integrity check for the
// specified contract.
func (c *Client) IntegrityCheckProgress(id types.FileContractID) (IntegrityCheckResult, error) {
var result IntegrityCheckResult
err := c.c.GET(fmt.Sprintf("/contracts/%v/integrity", id), &result)
return result, err
}
// DeleteIntegrityCheck deletes the integrity check for the specified contract.
func (c *Client) DeleteIntegrityCheck(id types.FileContractID) error {
return c.c.DELETE(fmt.Sprintf("/contracts/%v/integrity", id))
}
// DeleteSector deletes the sector with the specified root. This can cause
// contract failures if the sector is still in use.
func (c *Client) DeleteSector(root types.Hash256) error {
return c.c.DELETE(fmt.Sprintf("/sectors/%s", root))
}
// Volumes returns the volumes of the host.
func (c *Client) Volumes() (volumes []VolumeMeta, err error) {
err = c.c.GET("/volumes", &volumes)
return
}
// Volume returns the volume with the specified ID.
func (c *Client) Volume(id int) (volume VolumeMeta, err error) {
err = c.c.GET(fmt.Sprintf("/volumes/%d", id), &volume)
return
}
// AddVolume adds a new volume to the host
func (c *Client) AddVolume(localPath string, sectors uint64) (vol storage.Volume, err error) {
req := AddVolumeRequest{
LocalPath: localPath,
MaxSectors: sectors,
}
err = c.c.POST("/volumes", req, &vol)
return
}
// UpdateVolume updates the volume with the specified ID.
func (c *Client) UpdateVolume(id int, req UpdateVolumeRequest) error {
return c.c.PUT(fmt.Sprintf("/volumes/%v", id), req)
}
// DeleteVolume deletes the volume with the specified ID.
func (c *Client) DeleteVolume(id int) error {
return c.c.DELETE(fmt.Sprintf("/volumes/%v", id))
}
// ResizeVolume resizes the volume with the specified ID to a new size.
func (c *Client) ResizeVolume(id int, sectors uint64) error {
req := ResizeVolumeRequest{
MaxSectors: sectors,
}
return c.c.PUT(fmt.Sprintf("/volumes/%v/resize", id), req)
}
// Wallet returns the state of the host's wallet.
func (c *Client) Wallet() (resp WalletResponse, err error) {
err = c.c.GET("/wallet", &resp)
return
}
// Transactions returns the transactions of the host's wallet.
func (c *Client) Transactions(limit, offset int) (transactions []wallet.Transaction, err error) {
err = c.c.GET(fmt.Sprintf("/wallet/transactions?limit=%d&offset=%d", limit, offset), &transactions)
return
}
// PendingTransactions returns transactions that are not yet confirmed.
func (c *Client) PendingTransactions() (transactions []wallet.Transaction, err error) {
err = c.c.GET("/wallet/pending", &transactions)
return
}
// SendSiacoins sends siacoins to the specified address. If subtractFee is true,
// the miner fee is subtracted from the amount.
func (c *Client) SendSiacoins(address types.Address, amount types.Currency, subtractFee bool) (id types.TransactionID, err error) {
req := WalletSendSiacoinsRequest{
Address: address,
Amount: amount,
SubtractMinerFee: subtractFee,
}
err = c.c.POST("/wallet/send", req, &id)
return
}
// LocalDir returns the contents of the specified directory on the host.
func (c *Client) LocalDir(path string) (resp SystemDirResponse, err error) {
v := url.Values{
"path": []string{path},
}
err = c.c.GET("/system/dir?"+v.Encode(), &resp)
return
}
// MkDir creates a new directory on the host.
func (c *Client) MkDir(path string) error {
req := CreateDirRequest{
Path: path,
}
return c.c.PUT("/system/dir", req)
}
// RegisterWebHook registers a new WebHook.
func (c *Client) RegisterWebHook(callbackURL string, scopes []string) (hook webhooks.WebHook, err error) {
req := RegisterWebHookRequest{
CallbackURL: callbackURL,
Scopes: scopes,
}
err = c.c.POST("/webhooks", req, &hook)
return
}
// UpdateWebHook updates the WebHook with the specified ID.
func (c *Client) UpdateWebHook(id int64, callbackURL string, scopes []string) (hook webhooks.WebHook, err error) {
req := RegisterWebHookRequest{
CallbackURL: callbackURL,
Scopes: scopes,
}
err = c.c.PUT(fmt.Sprintf("/webhooks/%d", id), req)
return
}
// DeleteWebHook deletes the WebHook with the specified ID.
func (c *Client) DeleteWebHook(id int64) error {
return c.c.DELETE(fmt.Sprintf("/webhooks/%d", id))
}
// WebHooks returns all registered WebHooks.
func (c *Client) WebHooks() (hooks []webhooks.WebHook, err error) {
err = c.c.GET("/webhooks", &hooks)
return
}
// NewClient creates a new hostd API client.
func NewClient(baseURL, password string) *Client {
return &Client{
c: jape.Client{
BaseURL: baseURL,
Password: password,
},
}
}