ftx: fix ftx api get markets request

This commit is contained in:
c9s 2022-05-08 18:36:25 +08:00
parent 278eb937ac
commit 3af08abef2
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 79 additions and 21 deletions

View File

@ -47,6 +47,16 @@ func TestClient_Requests(t *testing.T) {
name string name string
tt func(t *testing.T) tt func(t *testing.T)
} { } {
{
name: "GetMarketsRequest",
tt: func(t *testing.T) {
req := client.NewGetMarketsRequest()
markets ,err := req.Do(ctx)
assert.NoError(t, err)
assert.NotNil(t, markets)
t.Logf("markets: %+v", markets)
},
},
{ {
name: "GetAccountRequest", name: "GetAccountRequest",
tt: func(t *testing.T) { tt: func(t *testing.T) {
@ -85,7 +95,7 @@ func TestClient_Requests(t *testing.T) {
name: "GetFillsRequest", name: "GetFillsRequest",
tt: func(t *testing.T) { tt: func(t *testing.T) {
req := client.NewGetFillsRequest() req := client.NewGetFillsRequest()
req.Market("CRO/USDT") req.Market("CRO/USD")
fills, err := req.Do(ctx) fills, err := req.Do(ctx)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, fills) assert.NotNil(t, fills)

View File

@ -7,6 +7,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/url" "net/url"
"reflect"
"regexp" "regexp"
) )
@ -20,8 +21,8 @@ func (g *GetMarketRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{} var params = map[string]interface{}{}
query := url.Values{} query := url.Values{}
for k, v := range params { for _k, _v := range params {
query.Add(k, fmt.Sprintf("%v", v)) query.Add(_k, fmt.Sprintf("%v", _v))
} }
return query, nil return query, nil
@ -43,8 +44,14 @@ func (g *GetMarketRequest) GetParametersQuery() (url.Values, error) {
return query, err return query, err
} }
for k, v := range params { for _k, _v := range params {
query.Add(k, fmt.Sprintf("%v", v)) if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
} }
return query, nil return query, nil
@ -73,14 +80,31 @@ func (g *GetMarketRequest) GetSlugParameters() (map[string]interface{}, error) {
} }
func (g *GetMarketRequest) applySlugsToUrl(url string, slugs map[string]string) string { func (g *GetMarketRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for k, v := range slugs { for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + k + "\\b") needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, v) url = needleRE.ReplaceAllString(url, _v)
} }
return url return url
} }
func (g *GetMarketRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetMarketRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetMarketRequest) GetSlugsMap() (map[string]string, error) { func (g *GetMarketRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{} slugs := map[string]string{}
params, err := g.GetSlugParameters() params, err := g.GetSlugParameters()
@ -88,8 +112,8 @@ func (g *GetMarketRequest) GetSlugsMap() (map[string]string, error) {
return slugs, nil return slugs, nil
} }
for k, v := range params { for _k, _v := range params {
slugs[k] = fmt.Sprintf("%v", v) slugs[_k] = fmt.Sprintf("%v", _v)
} }
return slugs, nil return slugs, nil

View File

@ -7,6 +7,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/url" "net/url"
"reflect"
"regexp" "regexp"
) )
@ -15,8 +16,8 @@ func (g *GetMarketsRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{} var params = map[string]interface{}{}
query := url.Values{} query := url.Values{}
for k, v := range params { for _k, _v := range params {
query.Add(k, fmt.Sprintf("%v", v)) query.Add(_k, fmt.Sprintf("%v", _v))
} }
return query, nil return query, nil
@ -38,8 +39,14 @@ func (g *GetMarketsRequest) GetParametersQuery() (url.Values, error) {
return query, err return query, err
} }
for k, v := range params { for _k, _v := range params {
query.Add(k, fmt.Sprintf("%v", v)) if g.isVarSlice(_v) {
g.iterateSlice(_v, func(it interface{}) {
query.Add(_k+"[]", fmt.Sprintf("%v", it))
})
} else {
query.Add(_k, fmt.Sprintf("%v", _v))
}
} }
return query, nil return query, nil
@ -63,14 +70,31 @@ func (g *GetMarketsRequest) GetSlugParameters() (map[string]interface{}, error)
} }
func (g *GetMarketsRequest) applySlugsToUrl(url string, slugs map[string]string) string { func (g *GetMarketsRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for k, v := range slugs { for _k, _v := range slugs {
needleRE := regexp.MustCompile(":" + k + "\\b") needleRE := regexp.MustCompile(":" + _k + "\\b")
url = needleRE.ReplaceAllString(url, v) url = needleRE.ReplaceAllString(url, _v)
} }
return url return url
} }
func (g *GetMarketsRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
sliceValue := reflect.ValueOf(slice)
for _i := 0; _i < sliceValue.Len(); _i++ {
it := sliceValue.Index(_i).Interface()
_f(it)
}
}
func (g *GetMarketsRequest) isVarSlice(_v interface{}) bool {
rt := reflect.TypeOf(_v)
switch rt.Kind() {
case reflect.Slice:
return true
}
return false
}
func (g *GetMarketsRequest) GetSlugsMap() (map[string]string, error) { func (g *GetMarketsRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{} slugs := map[string]string{}
params, err := g.GetSlugParameters() params, err := g.GetSlugParameters()
@ -78,8 +102,8 @@ func (g *GetMarketsRequest) GetSlugsMap() (map[string]string, error) {
return slugs, nil return slugs, nil
} }
for k, v := range params { for _k, _v := range params {
slugs[k] = fmt.Sprintf("%v", v) slugs[_k] = fmt.Sprintf("%v", _v)
} }
return slugs, nil return slugs, nil
@ -93,7 +117,7 @@ func (g *GetMarketsRequest) Do(ctx context.Context) ([]Market, error) {
apiURL := "api/markets" apiURL := "api/markets"
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params) req, err := g.client.NewRequest(ctx, "GET", apiURL, query, params)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -35,7 +35,7 @@ type Market struct {
} }
//go:generate GetRequest -url "api/markets" -type GetMarketsRequest -responseDataType []Market //go:generate GetRequest -url "api/markets" -type GetMarketsRequest -responseDataType []Market
type GetMarketsRequest struct { type GetMarketsRequest struct {
client requestgen.AuthenticatedAPIClient client requestgen.APIClient
} }
func (c *RestClient) NewGetMarketsRequest() *GetMarketsRequest { func (c *RestClient) NewGetMarketsRequest() *GetMarketsRequest {