requester: defer execution of request.Item generation in closure (#723)

* requester: defer execution of request.Item generation in closure.

* bithumb: fix issue

* coinut/itbit: fix linter issues

* binance: fix bug on recvWindow setting

* requester: standardize sendpayload + add readme update

* nonce: remove inc() function

* request: defer unlockiflocked

* binance: revert changes for open orders

* btcmarkets: defer auth generation functionality, rm context deadline as this will be created just before sending HTTP request.

* binance: move const to top

* exmo: remove debug output as its generated in the requester function

* ftx: defer auth functionality

* requester: move error to top

* bittrex: defer auth functionality

* bitmex: defer auth functionality and remove deadline as generation occurs after rate limiting.

* btse: defer auth functionality

* coinbasepro: defer auth functionality and removed context deadline as this is generated after rate limiting

* coinbene: defer auth functionality and remove context deadline as this is generated after rate limiting

* huobi: defer auth functionality and remove context deadline as this is generated after rate limiting

* huobi-futures: defer auth functionality and remove context deadline as this is generated after rate limiting

* kraken: defer auth functionality and remove context deadline as this is generated after rate limiting

* kraken: remove deadline protection for timestamp generation

* okgroup: defer auth functionality and remove context deadline as this is generated after rate limiting

* poloniex: defer auth functionality

* zb: defer auth functionality and remove context deadline as this is generated after rate limiting

* exchanges: clean up log output which are done and inspected in the requester package

* binance: fix path bug on every retry, rm timeout context as this is not needed

* coinbene: fix path bug on retry

* binance: consolidate functionality

* coinbene: fix linter issues

* poloniex: linter fix

* kraken: change add -> set

* bitstamp: fix path bug for retry

* BTSE: fix retry path bug

* coinbene: fix path bug whoopsie by me

* gateio: fix bug where on retry it does not reset reader

* localbitcoins: fix path bug on retry

* zb: change domain to land

* exchanges: make sure io.Reader is generated every request

* exchanges: move reader generation into function scope

* wrapper_issues: setup exchange manager

* engine: expand withdraw manager test

* engine: dont look for environment

* bitstamp: fix pathing bug (@thrasher-)

* engine/withdraw_manager: purge tests as this is covered in repository withdraw
This commit is contained in:
Ryan O'Hara-Reid
2021-08-06 17:24:38 +10:00
committed by GitHub
parent 2da239735f
commit 279b53827f
46 changed files with 1471 additions and 1324 deletions

View File

@@ -576,66 +576,59 @@ func (o *OKGroup) SendHTTPRequest(ep exchange.URL, httpMethod, requestType, requ
if err != nil {
return err
}
now := time.Now()
utcTime := now.UTC().Format(time.RFC3339)
payload := []byte("")
if data != nil {
payload, err = json.Marshal(data)
if err != nil {
return errors.New("sendHTTPRequest: Unable to JSON request")
}
if o.Verbose {
log.Debugf(log.ExchangeSys, "Request JSON: %s\n", payload)
}
}
path := endpoint + requestType + o.APIVersion + requestPath
if o.Verbose {
log.Debugf(log.ExchangeSys, "Sending %v request to %s \n", requestType, path)
}
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
if authenticated {
signPath := fmt.Sprintf("/%v%v%v%v", OKGroupAPIPath,
requestType, o.APIVersion, requestPath)
hmac := crypto.GetHMAC(crypto.HashSHA256,
[]byte(utcTime+httpMethod+signPath+string(payload)),
[]byte(o.API.Credentials.Secret))
headers["OK-ACCESS-KEY"] = o.API.Credentials.Key
headers["OK-ACCESS-SIGN"] = crypto.Base64Encode(hmac)
headers["OK-ACCESS-TIMESTAMP"] = utcTime
headers["OK-ACCESS-PASSPHRASE"] = o.API.Credentials.ClientID
}
// Requests that have a 30+ second difference between the timestamp and the API service time will be considered expired or rejected
ctx, cancel := context.WithDeadline(context.Background(), now.Add(30*time.Second))
defer cancel()
var intermediary json.RawMessage
newRequest := func() (*request.Item, error) {
now := time.Now()
utcTime := now.UTC().Format(time.RFC3339)
payload := []byte("")
if data != nil {
payload, err = json.Marshal(data)
if err != nil {
return nil, err
}
}
path := endpoint + requestType + o.APIVersion + requestPath
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
if authenticated {
signPath := fmt.Sprintf("/%v%v%v%v", OKGroupAPIPath,
requestType, o.APIVersion, requestPath)
hmac := crypto.GetHMAC(crypto.HashSHA256,
[]byte(utcTime+httpMethod+signPath+string(payload)),
[]byte(o.API.Credentials.Secret))
headers["OK-ACCESS-KEY"] = o.API.Credentials.Key
headers["OK-ACCESS-SIGN"] = crypto.Base64Encode(hmac)
headers["OK-ACCESS-TIMESTAMP"] = utcTime
headers["OK-ACCESS-PASSPHRASE"] = o.API.Credentials.ClientID
}
return &request.Item{
Method: strings.ToUpper(httpMethod),
Path: path,
Headers: headers,
Body: bytes.NewBuffer(payload),
Result: &intermediary,
AuthRequest: authenticated,
Verbose: o.Verbose,
HTTPDebugging: o.HTTPDebugging,
HTTPRecording: o.HTTPRecording,
}, nil
}
err = o.SendPayload(context.Background(), request.Unset, newRequest)
if err != nil {
return err
}
type errCapFormat struct {
Error int64 `json:"error_code,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
Result bool `json:"result,string,omitempty"`
}
errCap := errCapFormat{}
errCap.Result = true
err = o.SendPayload(ctx, &request.Item{
Method: strings.ToUpper(httpMethod),
Path: path,
Headers: headers,
Body: bytes.NewBuffer(payload),
Result: &intermediary,
AuthRequest: authenticated,
Verbose: o.Verbose,
HTTPDebugging: o.HTTPDebugging,
HTTPRecording: o.HTTPRecording,
})
if err != nil {
return err
}
errCap := errCapFormat{Result: true}
err = json.Unmarshal(intermediary, &errCap)
if err == nil {