11package api
22
33import (
4+ "context"
45 "fmt"
56 "net/url"
67 "strconv"
@@ -28,69 +29,69 @@ func NewClient(addr, password string) *Client {
2829
2930// State returns information about the current state of the explored daemon.
3031func (c * Client ) State () (resp StateResponse , err error ) {
31- err = c .c .GET ("/state" , & resp )
32+ err = c .c .GET (context . Background (), "/state" , & resp )
3233 return
3334}
3435
3536// TxpoolBroadcast broadcasts a set of transaction to the network.
3637func (c * Client ) TxpoolBroadcast (txns []types.Transaction , v2txns []types.V2Transaction ) (err error ) {
37- err = c .c .POST ("/txpool/broadcast" , TxpoolBroadcastRequest {txns , v2txns }, nil )
38+ err = c .c .POST (context . Background (), "/txpool/broadcast" , TxpoolBroadcastRequest {txns , v2txns }, nil )
3839 return
3940}
4041
4142// TxpoolTransactions returns all transactions in the transaction pool.
4243func (c * Client ) TxpoolTransactions () (txns []types.Transaction , v2txns []types.V2Transaction , err error ) {
4344 var resp TxpoolTransactionsResponse
44- err = c .c .GET ("/txpool/transactions" , & resp )
45+ err = c .c .GET (context . Background (), "/txpool/transactions" , & resp )
4546 return resp .Transactions , resp .V2Transactions , err
4647}
4748
4849// TxpoolFee returns the recommended fee (per weight unit) to ensure a high
4950// probability of inclusion in the next block.
5051func (c * Client ) TxpoolFee () (resp types.Currency , err error ) {
51- err = c .c .GET ("/txpool/fee" , & resp )
52+ err = c .c .GET (context . Background (), "/txpool/fee" , & resp )
5253 return
5354}
5455
5556// SyncerConnect adds the address as a peer of the syncer.
5657func (c * Client ) SyncerConnect (addr string ) (err error ) {
57- err = c .c .POST ("/syncer/connect" , addr , nil )
58+ err = c .c .POST (context . Background (), "/syncer/connect" , addr , nil )
5859 return
5960}
6061
6162// SyncerPeers returns the peers of the syncer.
6263func (c * Client ) SyncerPeers () (resp []string , err error ) {
63- err = c .c .GET ("/syncer/peers" , & resp )
64+ err = c .c .GET (context . Background (), "/syncer/peers" , & resp )
6465 return
6566}
6667
6768// SyncerBroadcastBlock broadcasts a block to all peers.
6869func (c * Client ) SyncerBroadcastBlock (b types.Block ) (err error ) {
69- err = c .c .POST ("/syncer/broadcast/block" , b , nil )
70+ err = c .c .POST (context . Background (), "/syncer/broadcast/block" , b , nil )
7071 return
7172}
7273
7374// ConsensusTip returns the current tip of the chain manager.
7475func (c * Client ) ConsensusTip () (resp types.ChainIndex , err error ) {
75- err = c .c .GET ("/consensus/tip" , & resp )
76+ err = c .c .GET (context . Background (), "/consensus/tip" , & resp )
7677 return
7778}
7879
7980// Tip returns the current tip of the explorer.
8081func (c * Client ) Tip () (resp types.ChainIndex , err error ) {
81- err = c .c .GET ("/explorer/tip" , & resp )
82+ err = c .c .GET (context . Background (), "/explorer/tip" , & resp )
8283 return
8384}
8485
8586// BestIndex returns the chain index at the specified height.
8687func (c * Client ) BestIndex (height uint64 ) (resp types.ChainIndex , err error ) {
87- err = c .c .GET (fmt .Sprintf ("/consensus/tip/%d" , height ), & resp )
88+ err = c .c .GET (context . Background (), fmt .Sprintf ("/consensus/tip/%d" , height ), & resp )
8889 return
8990}
9091
9192// ConsensusNetwork returns the network parameters of the consensus set.
9293func (c * Client ) ConsensusNetwork () (n * consensus.Network , err error ) {
93- err = c .c .GET ("/consensus/network" , & n )
94+ err = c .c .GET (context . Background (), "/consensus/network" , & n )
9495 return
9596}
9697
@@ -102,186 +103,186 @@ func (c *Client) ConsensusState() (state consensus.State, err error) {
102103 return
103104 }
104105 }
105- err = c .c .GET ("/consensus/state" , & state )
106+ err = c .c .GET (context . Background (), "/consensus/state" , & state )
106107 state .Network = c .n
107108 return
108109}
109110
110111// Block returns the block with the specified ID.
111112func (c * Client ) Block (id types.BlockID ) (resp explorer.Block , err error ) {
112- err = c .c .GET (fmt .Sprintf ("/blocks/%s" , id ), & resp )
113+ err = c .c .GET (context . Background (), fmt .Sprintf ("/blocks/%s" , id ), & resp )
113114 return
114115}
115116
116117// Transaction returns the transaction with the specified ID.
117118func (c * Client ) Transaction (id types.TransactionID ) (resp explorer.Transaction , err error ) {
118- err = c .c .GET (fmt .Sprintf ("/transactions/%s" , id ), & resp )
119+ err = c .c .GET (context . Background (), fmt .Sprintf ("/transactions/%s" , id ), & resp )
119120 return
120121}
121122
122123// Transactions returns the transactions with the specified IDs.
123124func (c * Client ) Transactions (ids []types.TransactionID ) (resp []explorer.Transaction , err error ) {
124- err = c .c .POST ("/transactions" , ids , & resp )
125+ err = c .c .POST (context . Background (), "/transactions" , ids , & resp )
125126 return
126127}
127128
128129// TransactionChainIndices returns chain indices a transaction was
129130// included in.
130131func (c * Client ) TransactionChainIndices (id types.TransactionID , offset , limit uint64 ) (resp []types.ChainIndex , err error ) {
131- err = c .c .GET (fmt .Sprintf ("/transactions/%s/indices?offset=%d&limit=%d" , id , offset , limit ), & resp )
132+ err = c .c .GET (context . Background (), fmt .Sprintf ("/transactions/%s/indices?offset=%d&limit=%d" , id , offset , limit ), & resp )
132133 return
133134}
134135
135136// V2Transaction returns the v2 transaction with the specified ID.
136137func (c * Client ) V2Transaction (id types.TransactionID ) (resp explorer.V2Transaction , err error ) {
137- err = c .c .GET (fmt .Sprintf ("/v2/transactions/%s" , id ), & resp )
138+ err = c .c .GET (context . Background (), fmt .Sprintf ("/v2/transactions/%s" , id ), & resp )
138139 return
139140}
140141
141142// V2Transactions returns the v2 transactions with the specified IDs.
142143func (c * Client ) V2Transactions (ids []types.TransactionID ) (resp []explorer.V2Transaction , err error ) {
143- err = c .c .POST ("/v2/transactions" , ids , & resp )
144+ err = c .c .POST (context . Background (), "/v2/transactions" , ids , & resp )
144145 return
145146}
146147
147148// V2TransactionChainIndices returns chain indices a v2 transaction was
148149// included in.
149150func (c * Client ) V2TransactionChainIndices (id types.TransactionID , offset , limit uint64 ) (resp []types.ChainIndex , err error ) {
150- err = c .c .GET (fmt .Sprintf ("/v2/transactions/%s/indices?offset=%d&limit=%d" , id , offset , limit ), & resp )
151+ err = c .c .GET (context . Background (), fmt .Sprintf ("/v2/transactions/%s/indices?offset=%d&limit=%d" , id , offset , limit ), & resp )
151152 return
152153}
153154
154155// AddressSiacoinUTXOs returns the specified address' unspent outputs.
155156func (c * Client ) AddressSiacoinUTXOs (address types.Address , offset , limit uint64 ) (resp []explorer.SiacoinOutput , err error ) {
156- err = c .c .GET (fmt .Sprintf ("/addresses/%s/utxos/siacoin?offset=%d&limit=%d" , address , offset , limit ), & resp )
157+ err = c .c .GET (context . Background (), fmt .Sprintf ("/addresses/%s/utxos/siacoin?offset=%d&limit=%d" , address , offset , limit ), & resp )
157158 return
158159}
159160
160161// AddressSiafundUTXOs returns the specified address' unspent outputs.
161162func (c * Client ) AddressSiafundUTXOs (address types.Address , offset , limit uint64 ) (resp []explorer.SiafundOutput , err error ) {
162- err = c .c .GET (fmt .Sprintf ("/addresses/%s/utxos/siafund?offset=%d&limit=%d" , address , offset , limit ), & resp )
163+ err = c .c .GET (context . Background (), fmt .Sprintf ("/addresses/%s/utxos/siafund?offset=%d&limit=%d" , address , offset , limit ), & resp )
163164 return
164165}
165166
166167// OutputSiacoin returns the specified siacoin output.
167168func (c * Client ) OutputSiacoin (id types.SiacoinOutputID ) (resp explorer.SiacoinOutput , err error ) {
168- err = c .c .GET (fmt .Sprintf ("/outputs/siacoin/%s" , id ), & resp )
169+ err = c .c .GET (context . Background (), fmt .Sprintf ("/outputs/siacoin/%s" , id ), & resp )
169170 return
170171}
171172
172173// OutputSiafund returns the specified siafund output.
173174func (c * Client ) OutputSiafund (id types.SiafundOutputID ) (resp explorer.SiafundOutput , err error ) {
174- err = c .c .GET (fmt .Sprintf ("/outputs/siafund/%s" , id ), & resp )
175+ err = c .c .GET (context . Background (), fmt .Sprintf ("/outputs/siafund/%s" , id ), & resp )
175176 return
176177}
177178
178179// AddressEvents returns the specified address' events.
179180func (c * Client ) AddressEvents (address types.Address , offset , limit uint64 ) (resp []explorer.Event , err error ) {
180- err = c .c .GET (fmt .Sprintf ("/addresses/%s/events?offset=%d&limit=%d" , address , offset , limit ), & resp )
181+ err = c .c .GET (context . Background (), fmt .Sprintf ("/addresses/%s/events?offset=%d&limit=%d" , address , offset , limit ), & resp )
181182 return
182183}
183184
184185// AddressUnconfirmedEvents returns the specified address' unconfirmed events.
185186func (c * Client ) AddressUnconfirmedEvents (address types.Address ) (resp []explorer.Event , err error ) {
186- err = c .c .GET (fmt .Sprintf ("/addresses/%s/events/unconfirmed" , address ), & resp )
187+ err = c .c .GET (context . Background (), fmt .Sprintf ("/addresses/%s/events/unconfirmed" , address ), & resp )
187188 return
188189}
189190
190191// Event returns the specified event.
191192func (c * Client ) Event (id types.Hash256 ) (resp explorer.Event , err error ) {
192- err = c .c .GET (fmt .Sprintf ("/events/%s" , id ), & resp )
193+ err = c .c .GET (context . Background (), fmt .Sprintf ("/events/%s" , id ), & resp )
193194 return
194195}
195196
196197// AddressBalance returns the specified address' balance.
197198func (c * Client ) AddressBalance (address types.Address ) (resp AddressBalanceResponse , err error ) {
198- err = c .c .GET (fmt .Sprintf ("/addresses/%s/balance" , address ), & resp )
199+ err = c .c .GET (context . Background (), fmt .Sprintf ("/addresses/%s/balance" , address ), & resp )
199200 return
200201}
201202
202203// Contract returns the file contract with the specified ID.
203204func (c * Client ) Contract (id types.FileContractID ) (resp explorer.ExtendedFileContract , err error ) {
204- err = c .c .GET (fmt .Sprintf ("/contracts/%s" , id ), & resp )
205+ err = c .c .GET (context . Background (), fmt .Sprintf ("/contracts/%s" , id ), & resp )
205206 return
206207}
207208
208209// Contracts returns the contracts with the specified IDs.
209210func (c * Client ) Contracts (ids []types.FileContractID ) (resp []explorer.ExtendedFileContract , err error ) {
210- err = c .c .POST ("/contracts" , ids , & resp )
211+ err = c .c .POST (context . Background (), "/contracts" , ids , & resp )
211212 return
212213}
213214
214215// ContractsKey returns the contracts for a particular ed25519 key.
215216func (c * Client ) ContractsKey (key types.PublicKey ) (resp []explorer.ExtendedFileContract , err error ) {
216- err = c .c .GET (fmt .Sprintf ("/pubkey/%s/contracts" , key ), & resp )
217+ err = c .c .GET (context . Background (), fmt .Sprintf ("/pubkey/%s/contracts" , key ), & resp )
217218 return
218219}
219220
220221// ContractRevisions returns all the revisions of the contract with the
221222// specified ID.
222223func (c * Client ) ContractRevisions (id types.FileContractID ) (resp []explorer.ExtendedFileContract , err error ) {
223- err = c .c .GET (fmt .Sprintf ("/contracts/%s/revisions" , id ), & resp )
224+ err = c .c .GET (context . Background (), fmt .Sprintf ("/contracts/%s/revisions" , id ), & resp )
224225 return
225226}
226227
227228// V2Contract returns the v2 file contract with the specified ID.
228229func (c * Client ) V2Contract (id types.FileContractID ) (resp explorer.V2FileContract , err error ) {
229- err = c .c .GET (fmt .Sprintf ("/v2/contracts/%s" , id ), & resp )
230+ err = c .c .GET (context . Background (), fmt .Sprintf ("/v2/contracts/%s" , id ), & resp )
230231 return
231232}
232233
233234// V2Contracts returns the v2 contracts with the specified IDs.
234235func (c * Client ) V2Contracts (ids []types.FileContractID ) (resp []explorer.V2FileContract , err error ) {
235- err = c .c .POST ("/v2/contracts" , ids , & resp )
236+ err = c .c .POST (context . Background (), "/v2/contracts" , ids , & resp )
236237 return
237238}
238239
239240// V2ContractRevisions returns all the revisions of the contract with the
240241// specified ID.
241242func (c * Client ) V2ContractRevisions (id types.FileContractID ) (resp []explorer.V2FileContract , err error ) {
242- err = c .c .GET (fmt .Sprintf ("/v2/contracts/%s/revisions" , id ), & resp )
243+ err = c .c .GET (context . Background (), fmt .Sprintf ("/v2/contracts/%s/revisions" , id ), & resp )
243244 return
244245}
245246
246247// V2ContractsKey returns the v2 contracts for a particular ed25519 key.
247248func (c * Client ) V2ContractsKey (key types.PublicKey ) (resp []explorer.V2FileContract , err error ) {
248- err = c .c .GET (fmt .Sprintf ("/v2/pubkey/%s/contracts" , key ), & resp )
249+ err = c .c .GET (context . Background (), fmt .Sprintf ("/v2/pubkey/%s/contracts" , key ), & resp )
249250 return
250251}
251252
252253// Host returns information about the host with a given ed25519 key.
253254func (c * Client ) Host (key types.PublicKey ) (resp explorer.Host , err error ) {
254- err = c .c .GET (fmt .Sprintf ("/hosts/%s" , key ), & resp )
255+ err = c .c .GET (context . Background (), fmt .Sprintf ("/hosts/%s" , key ), & resp )
255256 return
256257}
257258
258259// ScanHost triggers a manual host scan.
259260func (c * Client ) ScanHost (key types.PublicKey ) (resp explorer.HostScan , err error ) {
260- err = c .c .POST (fmt .Sprintf ("/hosts/%s/scan" , key ), nil , & resp )
261+ err = c .c .POST (context . Background (), fmt .Sprintf ("/hosts/%s/scan" , key ), nil , & resp )
261262 return
262263}
263264
264265// BlockMetrics returns the most recent metrics about the Sia blockchain.
265266func (c * Client ) BlockMetrics () (resp explorer.Metrics , err error ) {
266- err = c .c .GET ("/metrics/block" , & resp )
267+ err = c .c .GET (context . Background (), "/metrics/block" , & resp )
267268 return
268269}
269270
270271// BlockMetricsID returns various metrics about Sia at the given block ID.
271272func (c * Client ) BlockMetricsID (id types.BlockID ) (resp explorer.Metrics , err error ) {
272- err = c .c .GET (fmt .Sprintf ("/metrics/block/%s" , id ), & resp )
273+ err = c .c .GET (context . Background (), fmt .Sprintf ("/metrics/block/%s" , id ), & resp )
273274 return
274275}
275276
276277// HostMetrics returns various metrics about currently available hosts.
277278func (c * Client ) HostMetrics () (resp explorer.HostMetrics , err error ) {
278- err = c .c .GET ("/metrics/host" , & resp )
279+ err = c .c .GET (context . Background (), "/metrics/host" , & resp )
279280 return
280281}
281282
282283// Search returns what type of object an ID is.
283284func (c * Client ) Search (id string ) (resp explorer.SearchType , err error ) {
284- err = c .c .GET (fmt .Sprintf ("/search/%s" , id ), & resp )
285+ err = c .c .GET (context . Background (), fmt .Sprintf ("/search/%s" , id ), & resp )
285286 return
286287}
287288
@@ -292,12 +293,12 @@ func (c *Client) HostsList(params explorer.HostQuery, sortBy explorer.HostSortCo
292293 v .Add ("dir" , string (dir ))
293294 v .Add ("offset" , strconv .FormatUint (offset , 10 ))
294295 v .Add ("limit" , strconv .FormatUint (limit , 10 ))
295- err = c .c .POST ("/hosts?" + v .Encode (), params , & resp )
296+ err = c .c .POST (context . Background (), "/hosts?" + v .Encode (), params , & resp )
296297 return
297298}
298299
299300// ExchangeRate returns the value of 1 SC in the specified currency.
300301func (c * Client ) ExchangeRate (currency string ) (resp float64 , err error ) {
301- err = c .c .GET (fmt .Sprintf ("/exchange-rate/siacoin/%s" , currency ), & resp )
302+ err = c .c .GET (context . Background (), fmt .Sprintf ("/exchange-rate/siacoin/%s" , currency ), & resp )
302303 return
303304}
0 commit comments