mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-15 08:46:58 +08:00
robustness: add HTTP status code checks to market data & provider APIs
- market/api_client.go: check StatusCode for exchangeInfo, klines, price endpoints + add truncateBody helper for safe error messages - market/data.go: check StatusCode for openInterest and premiumIndex - provider/coinank: check StatusCode for GET and POST requests - provider/twelvedata: check StatusCode for time series and quote APIs - Prevents confusing JSON unmarshal errors when APIs return 429/500/etc
This commit is contained in:
@@ -15,6 +15,14 @@ const (
|
||||
baseURL = "https://fapi.binance.com"
|
||||
)
|
||||
|
||||
// truncateBody returns the first 512 bytes of body for error messages.
|
||||
func truncateBody(body []byte) string {
|
||||
if len(body) > 512 {
|
||||
return string(body[:512]) + "..."
|
||||
}
|
||||
return string(body)
|
||||
}
|
||||
|
||||
type APIClient struct {
|
||||
client *http.Client
|
||||
}
|
||||
@@ -47,6 +55,9 @@ func (c *APIClient) GetExchangeInfo() (*ExchangeInfo, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("Binance API error (status %d): %s", resp.StatusCode, truncateBody(body))
|
||||
}
|
||||
var exchangeInfo ExchangeInfo
|
||||
err = json.Unmarshal(body, &exchangeInfo)
|
||||
if err != nil {
|
||||
@@ -79,6 +90,9 @@ func (c *APIClient) GetKlines(symbol, interval string, limit int) ([]Kline, erro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("Binance klines API error (status %d): %s", resp.StatusCode, truncateBody(body))
|
||||
}
|
||||
|
||||
var klineResponses []KlineResponse
|
||||
err = json.Unmarshal(body, &klineResponses)
|
||||
@@ -144,6 +158,9 @@ func (c *APIClient) GetCurrentPrice(symbol string) (float64, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return 0, fmt.Errorf("Binance price API error (status %d): %s", resp.StatusCode, truncateBody(body))
|
||||
}
|
||||
|
||||
var ticker PriceTicker
|
||||
err = json.Unmarshal(body, &ticker)
|
||||
|
||||
Reference in New Issue
Block a user