mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
refactor newAuthenticatedRequest
This commit is contained in:
parent
bf4a0169bd
commit
93b19faa3a
|
@ -72,7 +72,7 @@ type VipLevel struct {
|
|||
}
|
||||
|
||||
func (s *AccountService) VipLevel() (*VipLevel, error) {
|
||||
req, err := s.client.NewAuthenticatedRequest(nil, "GET", "v2/members/vip_level", nil, nil)
|
||||
req, err := s.client.newAuthenticatedRequest(nil, "GET", "v2/members/vip_level", nil, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ func (s *AccountService) VipLevel() (*VipLevel, error) {
|
|||
}
|
||||
|
||||
func (s *AccountService) Account(currency string) (*Account, error) {
|
||||
req, err := s.client.NewAuthenticatedRequest(nil, "GET", "v2/members/accounts/"+currency, nil, nil)
|
||||
req, err := s.client.newAuthenticatedRequest(nil, "GET", "v2/members/accounts/"+currency, nil, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -124,12 +124,12 @@ type GetAccountsRequest struct {
|
|||
}
|
||||
|
||||
|
||||
func (s *AccountService) NewAccountsRequest() (*GetAccountsRequest) {
|
||||
func (s *AccountService) NewAccountsRequest() *GetAccountsRequest {
|
||||
return &GetAccountsRequest{client: s.client}
|
||||
}
|
||||
|
||||
func (s *AccountService) Accounts() ([]Account, error) {
|
||||
req, err := s.client.NewAuthenticatedRequest(nil, "GET", "v2/members/accounts", nil, nil)
|
||||
req, err := s.client.newAuthenticatedRequest(nil, "GET", "v2/members/accounts", nil, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -150,7 +150,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(nil, "GET", "v2/members/me", nil, nil)
|
||||
req, err := s.client.newAuthenticatedRequest(nil, "GET", "v2/members/me", nil, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -222,7 +222,7 @@ func (r *GetDepositHistoryRequest) To(to int64) *GetDepositHistoryRequest {
|
|||
}
|
||||
|
||||
func (r *GetDepositHistoryRequest) Do(ctx context.Context) (deposits []Deposit, err error) {
|
||||
req, err := r.client.NewAuthenticatedRequest(nil, "GET", "v2/deposits", &r.params, nil)
|
||||
req, err := r.client.newAuthenticatedRequest(nil, "GET", "v2/deposits", nil, &r.params, nil)
|
||||
if err != nil {
|
||||
return deposits, err
|
||||
}
|
||||
|
@ -308,7 +308,7 @@ func (r *GetWithdrawHistoryRequest) To(to int64) *GetWithdrawHistoryRequest {
|
|||
}
|
||||
|
||||
func (r *GetWithdrawHistoryRequest) Do(ctx context.Context) (withdraws []Withdraw, err error) {
|
||||
req, err := r.client.NewAuthenticatedRequest(nil, "GET", "v2/withdrawals", &r.params, nil)
|
||||
req, err := r.client.newAuthenticatedRequest(nil, "GET", "v2/withdrawals", nil, &r.params, nil)
|
||||
if err != nil {
|
||||
return withdraws, err
|
||||
}
|
||||
|
|
|
@ -123,7 +123,7 @@ func (s *OrderService) Closed(market string, options QueryOrderOptions) ([]Order
|
|||
payload["order_by"] = options.OrderBy
|
||||
}
|
||||
|
||||
req, err := s.client.NewAuthenticatedRequest(nil, "GET", "v2/orders", payload, relUrlV2Orders)
|
||||
req, err := s.client.newAuthenticatedRequest(nil, "GET", "v2/orders", nil, payload, relUrlV2Orders)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ func (s *OrderService) Open(market string, options QueryOrderOptions) ([]Order,
|
|||
payload["group_id"] = options.GroupID
|
||||
}
|
||||
|
||||
req, err := s.client.NewAuthenticatedRequest(nil, "GET", "v2/orders", payload, relUrlV2Orders)
|
||||
req, err := s.client.newAuthenticatedRequest(nil, "GET", "v2/orders", nil, payload, relUrlV2Orders)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -182,7 +182,7 @@ func (s *OrderService) All(market string, limit, page int, states ...OrderState)
|
|||
"order_by": "desc",
|
||||
}
|
||||
|
||||
req, err := s.client.NewAuthenticatedRequest(nil, "GET", "v2/orders", payload, relUrlV2Orders)
|
||||
req, err := s.client.newAuthenticatedRequest(nil, "GET", "v2/orders", nil, payload, relUrlV2Orders)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -210,7 +210,7 @@ func (s *OrderService) CancelAll(side string, market string) error {
|
|||
payload["market"] = market
|
||||
}
|
||||
|
||||
req, err := s.client.NewAuthenticatedRequest(nil, "POST", "v2/orders/clear", payload, relUrlV2OrdersClear)
|
||||
req, err := s.client.newAuthenticatedRequest(nil, "POST", "v2/orders/clear", nil, payload, relUrlV2OrdersClear)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -312,7 +312,7 @@ func (r *OrderCancelAllRequest) Do(ctx context.Context) (orders []Order, err err
|
|||
payload["groupID"] = *r.groupID
|
||||
}
|
||||
|
||||
req, err := r.client.NewAuthenticatedRequest(nil, "POST", "v2/orders/clear", payload, nil)
|
||||
req, err := r.client.newAuthenticatedRequest(nil, "POST", "v2/orders/clear", nil, payload, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -354,7 +354,7 @@ func (r *OrderCancelRequest) ClientOrderID(id string) *OrderCancelRequest {
|
|||
}
|
||||
|
||||
func (r *OrderCancelRequest) Do(ctx context.Context) error {
|
||||
req, err := r.client.NewAuthenticatedRequest(nil, "POST", "v2/order/delete", &r.params, relUrlV2OrderDelete)
|
||||
req, err := r.client.newAuthenticatedRequest(nil, "POST", "v2/order/delete", nil, &r.params, relUrlV2OrderDelete)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -382,7 +382,7 @@ func (s *OrderService) Get(orderID uint64) (*Order, error) {
|
|||
"id": orderID,
|
||||
}
|
||||
|
||||
req, err := s.client.NewAuthenticatedRequest(nil, "GET", "v2/order", payload, relUrlV2Order)
|
||||
req, err := s.client.newAuthenticatedRequest(nil, "GET", "v2/order", nil, payload, relUrlV2Order)
|
||||
if err != nil {
|
||||
return &Order{}, err
|
||||
}
|
||||
|
@ -460,7 +460,7 @@ func (r *CreateMultiOrderRequest) Do(ctx context.Context) (multiOrderResponse *M
|
|||
|
||||
payload["orders"] = r.orders
|
||||
|
||||
req, err := r.client.NewAuthenticatedRequest(nil, "POST", "v2/orders/multi/onebyone", payload, relUrlV2OrdersMultiOneByOne)
|
||||
req, err := r.client.newAuthenticatedRequest(nil, "POST", "v2/orders/multi/onebyone", nil, payload, relUrlV2OrdersMultiOneByOne)
|
||||
if err != nil {
|
||||
return multiOrderResponse, errors.Wrapf(err, "order create error")
|
||||
}
|
||||
|
@ -557,7 +557,7 @@ func (r *CreateOrderRequest) Do(ctx context.Context) (order *Order, err error) {
|
|||
payload["group_id"] = r.groupID
|
||||
}
|
||||
|
||||
req, err := r.client.NewAuthenticatedRequest(nil, "POST", "v2/orders", payload, relUrlV2Orders)
|
||||
req, err := r.client.newAuthenticatedRequest(nil, "POST", "v2/orders", nil, payload, relUrlV2Orders)
|
||||
if err != nil {
|
||||
return order, errors.Wrapf(err, "order create error")
|
||||
}
|
||||
|
|
|
@ -2,12 +2,12 @@ package max
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
|
@ -182,13 +182,14 @@ func (c *RestClient) NewRequest(ctx context.Context, method string, refURL strin
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if params != nil {
|
||||
rel.RawQuery = params.Encode()
|
||||
}
|
||||
|
||||
var req *http.Request
|
||||
u := c.BaseURL.ResolveReference(rel)
|
||||
|
||||
|
||||
body, err := castPayload(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -199,6 +200,8 @@ func (c *RestClient) NewRequest(ctx context.Context, method string, refURL strin
|
|||
return nil, err
|
||||
}
|
||||
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
if addUserAgentHeader {
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
}
|
||||
|
@ -206,8 +209,12 @@ func (c *RestClient) NewRequest(ctx context.Context, method string, refURL strin
|
|||
return req, nil
|
||||
}
|
||||
|
||||
// NewAuthenticatedRequest creates new http request for authenticated routes.
|
||||
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, m string, refURL string, data interface{}, rel *url.URL) (*http.Request, error) {
|
||||
func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, m string, refURL string, params url.Values, payload interface{}) (*http.Request, error) {
|
||||
return c.newAuthenticatedRequest(ctx, m, refURL, params, payload, nil)
|
||||
}
|
||||
|
||||
// newAuthenticatedRequest creates new http request for authenticated routes.
|
||||
func (c *RestClient) newAuthenticatedRequest(ctx context.Context, m string, refURL string, params url.Values, data interface{}, rel *url.URL) (*http.Request, error) {
|
||||
var err error
|
||||
if rel == nil {
|
||||
rel, err = url.Parse(refURL)
|
||||
|
@ -267,7 +274,7 @@ func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, m string, refU
|
|||
return nil, errors.New("empty api secret")
|
||||
}
|
||||
|
||||
req, err := c.NewRequest(nil, m, refURL, nil, p)
|
||||
req, err := c.NewRequest(ctx, m, refURL, params, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -364,7 +371,7 @@ func (c *RestClient) SendRequest(req *http.Request) (*requestgen.Response, error
|
|||
}
|
||||
|
||||
func (c *RestClient) sendAuthenticatedRequest(m string, refURL string, data map[string]interface{}) (*requestgen.Response, error) {
|
||||
req, err := c.NewAuthenticatedRequest(nil, m, refURL, data, nil)
|
||||
req, err := c.newAuthenticatedRequest(nil, m, refURL, nil, data, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -458,7 +465,6 @@ func ToErrorResponse(response *requestgen.Response) (errorResponse *ErrorRespons
|
|||
return errorResponse, fmt.Errorf("unexpected response content type %s", contentType)
|
||||
}
|
||||
|
||||
|
||||
func castPayload(payload interface{}) ([]byte, error) {
|
||||
if payload != nil {
|
||||
switch v := payload.(type) {
|
||||
|
|
|
@ -188,7 +188,7 @@ func (r *RewardsRequest) Do(ctx context.Context) (rewards []Reward, err error) {
|
|||
refURL += "/" + string(*r.pathType)
|
||||
}
|
||||
|
||||
req, err := r.client.NewAuthenticatedRequest(nil, "GET", refURL, payload, nil)
|
||||
req, err := r.client.newAuthenticatedRequest(nil, "GET", refURL, nil, payload, nil)
|
||||
if err != nil {
|
||||
return rewards, err
|
||||
}
|
||||
|
|
|
@ -220,7 +220,7 @@ func (r *PrivateTradeRequest) Do(ctx context.Context) (trades []Trade, err error
|
|||
payload["offset"] = r.offset
|
||||
}
|
||||
|
||||
req, err := r.client.NewAuthenticatedRequest(nil, "GET", "v2/trades/my", payload, nil)
|
||||
req, err := r.client.newAuthenticatedRequest(nil, "GET", "v2/trades/my", nil, payload, nil)
|
||||
if err != nil {
|
||||
return trades, err
|
||||
}
|
||||
|
|
|
@ -71,7 +71,7 @@ func (r *WithdrawalRequest) Do(ctx context.Context) (*Withdraw, error) {
|
|||
"amount": r.amount,
|
||||
}
|
||||
|
||||
req, err := r.client.NewAuthenticatedRequest(nil, "POST", "v2/withdrawal", payload, nil)
|
||||
req, err := r.client.newAuthenticatedRequest(nil, "POST", "v2/withdrawal", nil, 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(nil, "GET", "v2/withdraw_addresses", payload, nil)
|
||||
req, err := r.client.newAuthenticatedRequest(nil, "GET", "v2/withdraw_addresses", nil, payload, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user