maxapi: pre-parse relative url and cache them

This commit is contained in:
c9s 2021-06-09 02:36:32 +08:00
parent cbd0180939
commit 657e1dc9bf
6 changed files with 50 additions and 25 deletions

View File

@ -65,7 +65,7 @@ type VipLevel struct {
}
func (s *AccountService) VipLevel() (*VipLevel, error) {
req, err := s.client.newAuthenticatedRequest("GET", "v2/members/vip_level", nil)
req, err := s.client.newAuthenticatedRequest("GET", "v2/members/vip_level", nil, nil)
if err != nil {
return nil, err
}
@ -85,7 +85,7 @@ func (s *AccountService) VipLevel() (*VipLevel, error) {
}
func (s *AccountService) Account(currency string) (*Account, error) {
req, err := s.client.newAuthenticatedRequest("GET", "v2/members/accounts/"+currency, nil)
req, err := s.client.newAuthenticatedRequest("GET", "v2/members/accounts/"+currency, nil, nil)
if err != nil {
return nil, err
}
@ -111,7 +111,7 @@ func (s *AccountService) NewGetWithdrawalHistoryRequest() *GetWithdrawHistoryReq
}
func (s *AccountService) Accounts() ([]Account, error) {
req, err := s.client.newAuthenticatedRequest("GET", "v2/members/accounts", nil)
req, err := s.client.newAuthenticatedRequest("GET", "v2/members/accounts", nil, nil)
if err != nil {
return nil, err
}
@ -132,7 +132,7 @@ func (s *AccountService) Accounts() ([]Account, error) {
// Me returns the current user info by the current used MAX key and secret
func (s *AccountService) Me() (*UserInfo, error) {
req, err := s.client.newAuthenticatedRequest("GET", "v2/members/me", nil)
req, err := s.client.newAuthenticatedRequest("GET", "v2/members/me", nil, nil)
if err != nil {
return nil, err
}
@ -204,7 +204,7 @@ func (r *GetDepositHistoryRequest) To(to int64) *GetDepositHistoryRequest {
}
func (r *GetDepositHistoryRequest) Do(ctx context.Context) (deposits []Deposit, err error) {
req, err := r.client.newAuthenticatedRequest("GET", "v2/deposits", &r.params)
req, err := r.client.newAuthenticatedRequest("GET", "v2/deposits", &r.params, nil)
if err != nil {
return deposits, err
}
@ -290,7 +290,7 @@ func (r *GetWithdrawHistoryRequest) To(to int64) *GetWithdrawHistoryRequest {
}
func (r *GetWithdrawHistoryRequest) Do(ctx context.Context) (withdraws []Withdraw, err error) {
req, err := r.client.newAuthenticatedRequest("GET", "v2/withdrawals", &r.params)
req, err := r.client.newAuthenticatedRequest("GET", "v2/withdrawals", &r.params, nil)
if err != nil {
return withdraws, err
}

View File

@ -2,12 +2,35 @@ package max
import (
"context"
"net/url"
"strconv"
"time"
"github.com/pkg/errors"
)
var relUrlV2Order *url.URL
var relUrlV2Orders *url.URL
var relUrlV2OrdersClear *url.URL
var relUrlV2OrdersDelete *url.URL
var relUrlV2OrdersMultiOneByOne *url.URL
func mustParseURL(s string) *url.URL {
u, err := url.Parse(s)
if err != nil {
panic(err)
}
return u
}
func init() {
relUrlV2Order = mustParseURL("v2/order")
relUrlV2Orders = mustParseURL("v2/orders")
relUrlV2OrdersClear = mustParseURL("v2/orders/clear")
relUrlV2OrdersDelete = mustParseURL("v2/orders/delete")
relUrlV2OrdersMultiOneByOne = mustParseURL("v2/orders/multi/onebyone")
}
type OrderStateToQuery int
const (
@ -90,7 +113,7 @@ func (s *OrderService) Closed(market string, options QueryOrderOptions) ([]Order
payload["limit"] = options.Limit
}
req, err := s.client.newAuthenticatedRequest("GET", "v2/orders", payload)
req, err := s.client.newAuthenticatedRequest("GET", "v2/orders", payload, relUrlV2Orders)
if err != nil {
return nil, err
}
@ -121,7 +144,7 @@ func (s *OrderService) Open(market string, options QueryOrderOptions) ([]Order,
payload["group_id"] = options.GroupID
}
req, err := s.client.newAuthenticatedRequest("GET", "v2/orders", payload)
req, err := s.client.newAuthenticatedRequest("GET", "v2/orders", payload, relUrlV2Orders)
if err != nil {
return nil, err
}
@ -149,7 +172,7 @@ func (s *OrderService) All(market string, limit, page int, states ...OrderState)
"order_by": "desc",
}
req, err := s.client.newAuthenticatedRequest("GET", "v2/orders", payload)
req, err := s.client.newAuthenticatedRequest("GET", "v2/orders", payload, relUrlV2Orders)
if err != nil {
return nil, err
}
@ -177,7 +200,7 @@ func (s *OrderService) CancelAll(side string, market string) error {
payload["market"] = market
}
req, err := s.client.newAuthenticatedRequest("POST", "v2/orders/clear", payload)
req, err := s.client.newAuthenticatedRequest("POST", "v2/orders/clear", payload, relUrlV2OrdersClear)
if err != nil {
return err
}
@ -279,7 +302,7 @@ func (r *OrderCancelAllRequest) Do(ctx context.Context) (orders []Order, err err
payload["groupID"] = *r.groupID
}
req, err := r.client.newAuthenticatedRequest("POST", "v2/orders/clear", payload)
req, err := r.client.newAuthenticatedRequest("POST", "v2/orders/clear", payload, nil)
if err != nil {
return
}
@ -321,7 +344,7 @@ func (r *OrderCancelRequest) ClientOrderID(id string) *OrderCancelRequest {
}
func (r *OrderCancelRequest) Do(ctx context.Context) error {
req, err := r.client.newAuthenticatedRequest("POST", "v2/order/delete", &r.params)
req, err := r.client.newAuthenticatedRequest("POST", "v2/order/delete", &r.params, relUrlV2OrdersDelete)
if err != nil {
return err
}
@ -349,8 +372,7 @@ func (s *OrderService) Get(orderID uint64) (*Order, error) {
"id": orderID,
}
req, err := s.client.newAuthenticatedRequest("GET", "v2/order", payload)
req, err := s.client.newAuthenticatedRequest("GET", "v2/order", payload, relUrlV2Order)
if err != nil {
return &Order{}, err
}
@ -428,7 +450,7 @@ func (r *CreateMultiOrderRequest) Do(ctx context.Context) (multiOrderResponse *M
payload["orders"] = r.orders
req, err := r.client.newAuthenticatedRequest("POST", "v2/orders/multi/onebyone", payload)
req, err := r.client.newAuthenticatedRequest("POST", "v2/orders/multi/onebyone", payload, relUrlV2OrdersMultiOneByOne)
if err != nil {
return multiOrderResponse, errors.Wrapf(err, "order create error")
}
@ -525,7 +547,7 @@ func (r *CreateOrderRequest) Do(ctx context.Context) (order *Order, err error) {
payload["group_id"] = r.groupID
}
req, err := r.client.newAuthenticatedRequest("POST", "v2/orders", payload)
req, err := r.client.newAuthenticatedRequest("POST", "v2/orders", payload, relUrlV2Orders)
if err != nil {
return order, errors.Wrapf(err, "order create error")
}

View File

@ -197,10 +197,13 @@ func (c *RestClient) newRequest(method string, refURL string, params url.Values,
}
// newAuthenticatedRequest creates new http request for authenticated routes.
func (c *RestClient) newAuthenticatedRequest(m string, refURL string, data interface{}) (*http.Request, error) {
rel, err := url.Parse(refURL)
if err != nil {
return nil, err
func (c *RestClient) newAuthenticatedRequest(m string, refURL string, data interface{}, rel *url.URL) (*http.Request, error) {
var err error
if rel == nil {
rel, err = url.Parse(refURL)
if err != nil {
return nil, err
}
}
var p []byte
@ -351,7 +354,7 @@ func (c *RestClient) sendRequest(req *http.Request) (*util.Response, error) {
}
func (c *RestClient) sendAuthenticatedRequest(m string, refURL string, data map[string]interface{}) (*util.Response, error) {
req, err := c.newAuthenticatedRequest(m, refURL, data)
req, err := c.newAuthenticatedRequest(m, refURL, data, nil)
if err != nil {
return nil, err
}

View File

@ -188,7 +188,7 @@ func (r *RewardsRequest) Do(ctx context.Context) (rewards []Reward, err error) {
refURL += "/" + string(*r.pathType)
}
req, err := r.client.newAuthenticatedRequest("GET", refURL, payload)
req, err := r.client.newAuthenticatedRequest("GET", refURL, payload, nil)
if err != nil {
return rewards, err
}

View File

@ -220,7 +220,7 @@ func (r *PrivateTradeRequest) Do(ctx context.Context) (trades []Trade, err error
payload["offset"] = r.offset
}
req, err := r.client.newAuthenticatedRequest("GET", "v2/trades/my", payload)
req, err := r.client.newAuthenticatedRequest("GET", "v2/trades/my", payload, nil)
if err != nil {
return trades, err
}

View File

@ -71,7 +71,7 @@ func (r *WithdrawalRequest) Do(ctx context.Context) (*Withdraw, error) {
"amount": r.amount,
}
req, err := r.client.newAuthenticatedRequest("POST", "v2/withdrawal", payload)
req, err := r.client.newAuthenticatedRequest("POST", "v2/withdrawal", payload, nil)
if err != nil {
return nil, err
}
@ -121,7 +121,7 @@ func (r *GetWithdrawalAddressesRequest) Do(ctx context.Context) ([]WithdrawalAdd
"currency": r.currency,
}
req, err := r.client.newAuthenticatedRequest("GET", "v2/withdraw_addresses", payload)
req, err := r.client.newAuthenticatedRequest("GET", "v2/withdraw_addresses", payload, nil)
if err != nil {
return nil, err
}