ftx: implement get order status api

This commit is contained in:
c9s 2022-03-02 23:33:46 +08:00
parent 14bcc780a4
commit 95daa004aa
3 changed files with 155 additions and 7 deletions

View File

@ -499,8 +499,15 @@ func (e *Exchange) QueryOrder(ctx context.Context, q types.OrderQuery) (*types.O
if err != nil {
return nil, err
}
_ = orderID
return nil, nil
req := e.client.NewGetOrderStatusRequest(uint64(orderID))
ftxOrder, err := req.Do(ctx)
if err != nil {
return nil, err
}
order, err := toGlobalOrderNew(*ftxOrder)
return &order, err
}
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {

View File

@ -0,0 +1,131 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Result -url /api/orders/:orderId -type GetOrderStatusRequest -responseDataType .Order"; DO NOT EDIT.
package ftxapi
import (
"context"
"encoding/json"
"fmt"
"net/url"
"regexp"
)
func (g *GetOrderStatusRequest) OrderID(orderID uint64) *GetOrderStatusRequest {
g.orderID = orderID
return g
}
// GetQueryParameters builds and checks the query parameters and returns url.Values
func (g *GetOrderStatusRequest) 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 *GetOrderStatusRequest) 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 *GetOrderStatusRequest) 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 *GetOrderStatusRequest) 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 *GetOrderStatusRequest) GetSlugParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check orderID field -> json key orderId
orderID := g.orderID
// assign parameter of orderID
params["orderId"] = orderID
return params, nil
}
func (g *GetOrderStatusRequest) 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 *GetOrderStatusRequest) 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 *GetOrderStatusRequest) Do(ctx context.Context) (*Order, error) {
// no body params
var params interface{}
query := url.Values{}
apiURL := "/api/orders/:orderId"
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 Order
if err := json.Unmarshal(apiResponse.Result, &data); err != nil {
return nil, err
}
return &data, nil
}

View File

@ -106,22 +106,19 @@ func (c *RestClient) NewCancelAllOrderRequest() *CancelAllOrderRequest {
}
}
//go:generate requestgen -method DELETE -url "/api/orders/by_client_id/:clientOrderId" -type CancelOrderByClientOrderIdRequest -responseType .APIResponse
type CancelOrderByClientOrderIdRequest struct {
client requestgen.AuthenticatedAPIClient
client requestgen.AuthenticatedAPIClient
clientOrderId string `param:"clientOrderId,required,slug"`
}
func (c *RestClient) NewCancelOrderByClientOrderIdRequest(clientOrderId string) *CancelOrderByClientOrderIdRequest {
return &CancelOrderByClientOrderIdRequest{
client: c,
client: c,
clientOrderId: clientOrderId,
}
}
type Fill struct {
Id uint64 `json:"id"`
Future string `json:"future"`
@ -159,3 +156,16 @@ func (c *RestClient) NewGetFillsRequest() *GetFillsRequest {
client: c,
}
}
//go:generate GetRequest -url "/api/orders/:orderId" -type GetOrderStatusRequest -responseDataType .Order
type GetOrderStatusRequest struct {
client requestgen.AuthenticatedAPIClient
orderID uint64 `param:"orderId,slug"`
}
func (c *RestClient) NewGetOrderStatusRequest(orderID uint64) *GetOrderStatusRequest {
return &GetOrderStatusRequest{
client: c,
orderID: orderID,
}
}