2021-05-28 12:51:10 +00:00
|
|
|
package okexapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-06 11:14:21 +00:00
|
|
|
"encoding/json"
|
2021-05-28 12:51:10 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MarketTickersRequest struct {
|
|
|
|
client *RestClient
|
|
|
|
|
|
|
|
instType string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MarketTickersRequest) InstrumentType(instType string) *MarketTickersRequest {
|
|
|
|
r.instType = instType
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MarketTickersRequest) Do(ctx context.Context) ([]MarketTicker, error) {
|
|
|
|
// SPOT, SWAP, FUTURES, OPTION
|
|
|
|
var params = url.Values{}
|
|
|
|
params.Add("instType", string(r.instType))
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
req, err := r.client.NewRequest(ctx, "GET", "/api/v5/market/tickers", params, nil)
|
2021-05-28 12:51:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
response, err := r.client.SendRequest(req)
|
2021-05-28 12:51:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
var apiResponse APIResponse
|
|
|
|
if err := response.DecodeJSON(&apiResponse); err != nil {
|
|
|
|
return nil, err
|
2021-05-28 12:51:10 +00:00
|
|
|
}
|
2023-09-06 11:14:21 +00:00
|
|
|
var data []MarketTicker
|
|
|
|
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
|
2021-05-28 12:51:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
return data, nil
|
2021-05-28 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MarketTickerRequest struct {
|
|
|
|
client *RestClient
|
|
|
|
|
|
|
|
instId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MarketTickerRequest) InstrumentID(instId string) *MarketTickerRequest {
|
|
|
|
r.instId = instId
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *MarketTickerRequest) Do(ctx context.Context) (*MarketTicker, error) {
|
|
|
|
// SPOT, SWAP, FUTURES, OPTION
|
|
|
|
var params = url.Values{}
|
|
|
|
params.Add("instId", r.instId)
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
req, err := r.client.NewRequest(ctx, "GET", "/api/v5/market/ticker", params, nil)
|
2021-05-28 12:51:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
response, err := r.client.SendRequest(req)
|
2021-05-28 12:51:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
var apiResponse APIResponse
|
|
|
|
if err := response.DecodeJSON(&apiResponse); err != nil {
|
|
|
|
return nil, err
|
2021-05-28 12:51:10 +00:00
|
|
|
}
|
2023-09-06 11:14:21 +00:00
|
|
|
var data []MarketTicker
|
|
|
|
if err := json.Unmarshal(apiResponse.Data, &data); err != nil {
|
2021-05-28 12:51:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
if len(data) == 0 {
|
2021-05-28 12:51:10 +00:00
|
|
|
return nil, fmt.Errorf("ticker of %s not found", r.instId)
|
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
return &data[0], nil
|
2021-05-28 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
func (c *RestClient) NewMarketTickerRequest(instId string) *MarketTickerRequest {
|
2021-05-28 12:51:10 +00:00
|
|
|
return &MarketTickerRequest{
|
2023-09-06 11:14:21 +00:00
|
|
|
client: c,
|
2021-05-28 12:51:10 +00:00
|
|
|
instId: instId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 11:14:21 +00:00
|
|
|
func (c *RestClient) NewMarketTickersRequest(instType string) *MarketTickersRequest {
|
2021-05-28 12:51:10 +00:00
|
|
|
return &MarketTickersRequest{
|
2023-09-06 11:14:21 +00:00
|
|
|
client: c,
|
2021-05-28 12:51:10 +00:00
|
|
|
instType: instType,
|
|
|
|
}
|
|
|
|
}
|