ftxapi: add get single market api

This commit is contained in:
c9s 2022-02-22 14:24:58 +08:00
parent 2a6310c5f5
commit 3601edab84
2 changed files with 144 additions and 0 deletions

View File

@ -291,3 +291,16 @@ func (c *RestClient) NewGetMarketsRequest() *GetMarketsRequest {
client: c,
}
}
//go:generate GetRequest -url "/api/markets/:market" -type GetMarketRequest -responseDataType .Market
type GetMarketRequest struct {
client requestgen.AuthenticatedAPIClient
market string `param:"market,slug"`
}
func (c *RestClient) NewGetMarketRequest(market string) *GetMarketRequest {
return &GetMarketRequest{
client: c,
market: market,
}
}

View File

@ -0,0 +1,131 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Result -url /api/markets/:market -type GetMarketRequest -responseDataType .Market"; DO NOT EDIT.
package ftxapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"regexp"
)
func (g *GetMarketRequest) Market(market string) *GetMarketRequest {
g.market = market
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetMarketRequest) GetQueryParameters() (url.Values, error) {
var params = map[string]interface{}{}
query := url.Values{}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
// GetParameters builds and checks the parameters and return the result in a map object
func (g *GetMarketRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
return params, nil
}
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
func (g *GetMarketRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := g.GetParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
// GetParametersJSON converts the parameters from GetParameters into the JSON format
func (g *GetMarketRequest) GetParametersJSON() ([]byte, error) {
params, err := g.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
func (g *GetMarketRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check market field -> json key market
market := g.market
// assign parameter of market
params["market"] = market
return params, nil
}
func (g *GetMarketRequest) applySlugsToUrl(url string, slugs map[string]string) string {
for k, v := range slugs {
needleRE := regexp.MustCompile(":" + k + "\\b")
url = needleRE.ReplaceAllString(url, v)
}
return url
}
func (g *GetMarketRequest) GetSlugsMap() (map[string]string, error) {
slugs := map[string]string{}
params, err := g.GetSlugParameters()
if err != nil {
return slugs, nil
}
for k, v := range params {
slugs[k] = fmt.Sprintf("%v", v)
}
return slugs, nil
}
func (g *GetMarketRequest) Do(ctx context.Context) (*Market, error) {
// no body params
var params interface{}
query := url.Values{}
apiURL := "/api/markets/:market"
slugs, err := g.GetSlugsMap()
if err != nil {
return nil, err
}
apiURL = g.applySlugsToUrl(apiURL, slugs)
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
if err != nil {
return nil, err
}
response, err := g.client.SendRequest(req)
if err != nil {
return nil, err
}
var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
var data Market
if err := json.Unmarshal(apiResponse.Result, &data); err != nil {
return nil, err
}
return &data, nil
}