Skip to content

Commit 59b5d12

Browse files
committed
fix(): modify api response code verify
1 parent 77daa78 commit 59b5d12

File tree

1 file changed

+42
-9
lines changed

1 file changed

+42
-9
lines changed

client.go

+42-9
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,27 @@ func (c *Client) SetShortConnUrl(url string) {
5555
}
5656

5757
func (c *Client) GetInfo() (info *types.NetworkInfo, err error) {
58-
body, _, err := c.httpGet("info")
58+
body, code, err := c.httpGet("info")
5959
if err != nil {
6060
return nil, ErrBadGateway
6161
}
62+
if code != 200 {
63+
return nil, fmt.Errorf("get info error: %s", string(body))
64+
}
6265

6366
info = &types.NetworkInfo{}
6467
err = json.Unmarshal(body, info)
6568
return
6669
}
6770

6871
func (c *Client) GetPeers() ([]string, error) {
69-
body, _, err := c.httpGet("peers")
72+
body, code, err := c.httpGet("peers")
7073
if err != nil {
7174
return nil, ErrBadGateway
7275
}
76+
if code != 200 {
77+
return nil, fmt.Errorf("get peers error: %s", string(body))
78+
}
7379

7480
peers := make([]string, 0)
7581
err = json.Unmarshal(body, &peers)
@@ -226,19 +232,34 @@ func (c *Client) GetTransactionPrice(data []byte, target *string) (reward int64,
226232
url = fmt.Sprintf("%v/%v", url, *target)
227233
}
228234

229-
body, _, err := c.httpGet(url)
235+
body, code, err := c.httpGet(url)
236+
if err != nil {
237+
return
238+
}
239+
if code != 200 {
240+
return 0, fmt.Errorf("get reward error: %s", string(body))
241+
}
242+
243+
reward, err = strconv.ParseInt(string(body), 10, 64)
230244
if err != nil {
231245
return
232246
}
233247

234-
return strconv.ParseInt(string(body), 10, 64)
248+
// reward can not be 0
249+
if reward <= 0 {
250+
err = errors.New("reward must more than 0")
251+
}
252+
return
235253
}
236254

237255
func (c *Client) GetTransactionAnchor() (anchor string, err error) {
238-
body, _, err := c.httpGet("tx_anchor")
256+
body, code, err := c.httpGet("tx_anchor")
239257
if err != nil {
240258
return
241259
}
260+
if code != 200 {
261+
return "", fmt.Errorf("get tx anchor err: %s", string(body))
262+
}
242263

243264
anchor = string(body)
244265
return
@@ -308,10 +329,13 @@ func (c *Client) GraphQL(query string) ([]byte, error) {
308329

309330
// Wallet
310331
func (c *Client) GetWalletBalance(address string) (arAmount *big.Float, err error) {
311-
body, _, err := c.httpGet(fmt.Sprintf("wallet/%s/balance", address))
332+
body, code, err := c.httpGet(fmt.Sprintf("wallet/%s/balance", address))
312333
if err != nil {
313334
return
314335
}
336+
if code != 200 {
337+
return nil, fmt.Errorf("get balance error: %s", string(body))
338+
}
315339

316340
winstomStr := string(body)
317341
winstom, ok := new(big.Int).SetString(winstomStr, 10)
@@ -325,33 +349,42 @@ func (c *Client) GetWalletBalance(address string) (arAmount *big.Float, err erro
325349
}
326350

327351
func (c *Client) GetLastTransactionID(address string) (id string, err error) {
328-
body, _, err := c.httpGet(fmt.Sprintf("wallet/%s/last_tx", address))
352+
body, code, err := c.httpGet(fmt.Sprintf("wallet/%s/last_tx", address))
329353
if err != nil {
330354
return
331355
}
356+
if code != 200 {
357+
return "", fmt.Errorf("get last id error: %s", string(body))
358+
}
332359

333360
id = string(body)
334361
return
335362
}
336363

337364
// Block
338365
func (c *Client) GetBlockByID(id string) (block *types.Block, err error) {
339-
body, _, err := c.httpGet(fmt.Sprintf("block/hash/%s", id))
366+
body, code, err := c.httpGet(fmt.Sprintf("block/hash/%s", id))
340367
if err != nil {
341368
return
342369
}
343370

371+
if code != 200 {
372+
return nil, fmt.Errorf("get block by id error: %s", string(body))
373+
}
344374
block = &types.Block{}
345375
err = json.Unmarshal(body, block)
346376
return
347377
}
348378

349379
func (c *Client) GetBlockByHeight(height int64) (block *types.Block, err error) {
350-
body, _, err := c.httpGet(fmt.Sprintf("block/height/%d", height))
380+
body, code, err := c.httpGet(fmt.Sprintf("block/height/%d", height))
351381
if err != nil {
352382
return
353383
}
354384

385+
if code != 200 {
386+
return nil, fmt.Errorf("get block by height error: %s", string(body))
387+
}
355388
block = &types.Block{}
356389
err = json.Unmarshal(body, block)
357390
return

0 commit comments

Comments
 (0)