From f0c192cbbf936120c5ebb62dd924287f4e00f411 Mon Sep 17 00:00:00 2001 From: nanoric Date: Thu, 27 Dec 2018 01:10:20 -0400 Subject: [PATCH] =?UTF-8?q?[Fix]=20=E4=BF=AE=E6=AD=A3=E4=BA=86huobigateway?= =?UTF-8?q?=E4=B8=8EGET=E7=9B=B8=E5=85=B3=E7=9A=84=E7=AD=BE=E5=90=8D?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/rest/RestClient.py | 1 + .../gateway/huobiGateway/huobiGateway.py | 32 +++++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/vnpy/api/rest/RestClient.py b/vnpy/api/rest/RestClient.py index 32b96666..6d94bbab 100644 --- a/vnpy/api/rest/RestClient.py +++ b/vnpy/api/rest/RestClient.py @@ -48,6 +48,7 @@ class Request(object): statusCode = 'terminated' else: statusCode = self.response.status_code + # todo: encoding error return ("reuqest : {} {} {} because {}: \n" "headers: {}\n" "params: {}\n" diff --git a/vnpy/trader/gateway/huobiGateway/huobiGateway.py b/vnpy/trader/gateway/huobiGateway/huobiGateway.py index 74187a1b..46b9975b 100644 --- a/vnpy/trader/gateway/huobiGateway/huobiGateway.py +++ b/vnpy/trader/gateway/huobiGateway/huobiGateway.py @@ -19,8 +19,8 @@ from vnpy.api.websocket import WebsocketClient from vnpy.trader.vtGateway import * from vnpy.trader.vtFunction import getTempPath, getJsonPath -#REST_HOST = 'https://api.huobipro.com' -REST_HOST = 'https://api.huobi.pro/v1' +REST_HOST = 'https://api.huobipro.com' +# REST_HOST = 'https://api.huobi.pro/v1' WEBSOCKET_MARKET_HOST = 'wss://api.huobi.pro/ws' # Global站行情 WEBSOCKET_TRADE_HOST = 'wss://api.huobi.pro/ws/v1' # 资产和订单 @@ -37,14 +37,21 @@ def _split_url(url): #---------------------------------------------------------------------- -def createSignature(apiKey, method, host, path, secretKey): - """创建签名""" - sortedParams = ( +def createSignature(apiKey, method, host, path, secretKey, getParams=None): + """ + 创建签名 + :param getParams: dict 使用GET方法时附带的额外参数(urlparams) + :return: + """ + sortedParams = [ ("AccessKeyId", apiKey), ("SignatureMethod", 'HmacSHA256'), ("SignatureVersion", "2"), ("Timestamp", datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')) - ) + ] + if getParams: + sortedParams.extend(getParams.items()) + sortedParams = list(sorted(sortedParams)) encodeParams = urllib.urlencode(sortedParams) payload = [method, host, path, encodeParams] @@ -213,16 +220,13 @@ class HuobiRestApi(RestClient): request.headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36" } - - additionalParams = createSignature(self.apiKey, + paramsWithSignature = createSignature(self.apiKey, request.method, self.signHost, request.path, - self.apiSecret) - if not request.params: - request.params = additionalParams - else: - request.params.update(additionalParams) + self.apiSecret, + request.params) + request.params = paramsWithSignature if request.method == "POST": request.headers['Content-Type'] = 'application/json' @@ -500,7 +504,7 @@ class HuobiWebsocketApiBase(WebsocketClient): #---------------------------------------------------------------------- def onErrorMsg(self, packet): # type: (dict)->None """""" - self.gateway.writeLog(packet['err-msg'])) + self.gateway.writeLog(packet['err-msg']) ########################################################################