Skip to content

Commit

Permalink
Broker support (#163)
Browse files Browse the repository at this point in the history
* fix set collateral coin post method to postV5JSON

* add referer header to client

---------

Co-authored-by: Rostislav Lyupa <>
  • Loading branch information
lyro41 authored Feb 9, 2024
1 parent 41b4c7c commit 7dbac89
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Client struct {
key string
secret string

referer string

checkResponseBody checkResponseBodyFunc
syncTimeDeltaNanoSeconds int64
}
Expand Down Expand Up @@ -101,6 +103,12 @@ func (c *Client) WithBaseURL(url string) *Client {
return c
}

func (c *Client) WithReferer(referer string) *Client {
c.referer = referer

return c
}

// Request :
func (c *Client) Request(req *http.Request, dst interface{}) (err error) {
c.debugf("request: %v", req)
Expand Down Expand Up @@ -162,6 +170,9 @@ func (c *Client) populateSignature(src url.Values) url.Values {

src.Add("api_key", c.key)
src.Add("timestamp", strconv.FormatInt(c.getTimestamp(), 10))
if c.referer != "" {
src.Add("referer", c.referer)
}
src.Add("sign", getSignature(src, c.secret))

return src
Expand All @@ -175,6 +186,9 @@ func (c *Client) populateSignatureForBody(src []byte) []byte {

body["api_key"] = c.key
body["timestamp"] = strconv.FormatInt(c.getTimestamp(), 10)
if c.referer != "" {
body["referer"] = c.referer
}
body["sign"] = getSignatureForBody(body, c.secret)

result, err := json.Marshal(body)
Expand Down Expand Up @@ -378,6 +392,9 @@ func (c *Client) postV5JSON(path string, body []byte, dst interface{}) error {
req.Header.Set("X-BAPI-API-KEY", c.key)
req.Header.Set("X-BAPI-TIMESTAMP", strconv.FormatInt(timestamp, 10))
req.Header.Set("X-BAPI-SIGN", sign)
if c.referer != "" {
req.Header.Set("X-Referer", c.referer)
}

if err := c.Request(req, &dst); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion v5_account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (s *V5AccountService) SetCollateralCoin(param V5SetCollateralCoinParam) (*V
return nil, err
}

if err := s.client.postJSON("/v5/account/set-collateral-switch", body, &res); err != nil {
if err := s.client.postV5JSON("/v5/account/set-collateral-switch", body, &res); err != nil {
return nil, err
}

Expand Down

0 comments on commit 7dbac89

Please sign in to comment.