From 7dbac89dc5f14807ea52e9b06ada7e87ed71ca87 Mon Sep 17 00:00:00 2001 From: Rostislav <43965646+lyro41@users.noreply.github.com> Date: Fri, 9 Feb 2024 02:12:57 +0000 Subject: [PATCH] Broker support (#163) * fix set collateral coin post method to postV5JSON * add referer header to client --------- Co-authored-by: Rostislav Lyupa <> --- client.go | 17 +++++++++++++++++ v5_account_service.go | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index c0828f0..b4109e5 100644 --- a/client.go +++ b/client.go @@ -36,6 +36,8 @@ type Client struct { key string secret string + referer string + checkResponseBody checkResponseBodyFunc syncTimeDeltaNanoSeconds int64 } @@ -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) @@ -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 @@ -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) @@ -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 diff --git a/v5_account_service.go b/v5_account_service.go index 9e07056..972e5a3 100644 --- a/v5_account_service.go +++ b/v5_account_service.go @@ -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 }