From 486e6055e6ac048e9199c65bec820d4cf01cb345 Mon Sep 17 00:00:00 2001 From: nanoric Date: Wed, 10 Oct 2018 03:57:46 -0400 Subject: [PATCH] =?UTF-8?q?[Mod]=20HttClient=EF=BC=9A=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E7=9A=84=E6=B5=8B=E8=AF=95=20[Del]=20?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86HeadersAuthenticateHttpClient?= =?UTF-8?q?=EF=BC=8C=E5=9B=A0=E4=B8=BA=E4=B8=8D=E5=86=8D=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 该版本的测试可以通过 --- tests/network/RestfulClientTest.py | 9 +++-- vnpy/api/okexfuture/vnokexFuture.py | 11 +++--- vnpy/network/HeadersAuthenticateHttpClient.py | 38 ------------------- 3 files changed, 11 insertions(+), 47 deletions(-) delete mode 100644 vnpy/network/HeadersAuthenticateHttpClient.py diff --git a/tests/network/RestfulClientTest.py b/tests/network/RestfulClientTest.py index 8cf8eaaf..90d400c1 100644 --- a/tests/network/RestfulClientTest.py +++ b/tests/network/RestfulClientTest.py @@ -6,7 +6,7 @@ import unittest from simplejson import JSONDecodeError from Promise import Promise -from vnpy.network.HttpClient import HttpClient +from vnpy.network.HttpClient import HttpClient, Request class FailedError(RuntimeError): @@ -22,9 +22,10 @@ class TestHttpClient(HttpClient): self.p = Promise() - def beforeRequest(self, method, path, params, data): - data = json.dumps(data) - return method, path, params, data, {'Content-Type': 'application/json'} + def beforeRequest(self, req): #type: (Request)->Request + req.data = json.dumps(req.data) + req.headers = {'Content-Type': 'application/json'} + return req def onError(self, exceptionType, exceptionValue, tb, req): self.p.set_exception(exceptionValue) diff --git a/vnpy/api/okexfuture/vnokexFuture.py b/vnpy/api/okexfuture/vnokexFuture.py index 288955bc..263356b7 100644 --- a/vnpy/api/okexfuture/vnokexFuture.py +++ b/vnpy/api/okexfuture/vnokexFuture.py @@ -4,7 +4,7 @@ import urllib ######################################################################## -from vnpy.network.HttpClient import HttpClient +from vnpy.network.HttpClient import HttpClient, Request ######################################################################## @@ -25,9 +25,9 @@ class OkexFutureHttpClient(HttpClient): self.apiSecret = apiSecret #---------------------------------------------------------------------- - def beforeRequest(self, method, path, params, data): # type: (str, str, dict, dict)->(str, str, dict, dict, dict) - args = params or {} - args.update(data or {}) + def beforeRequest(self, req): # type: (Request)->Request + args = req.params or {} + args.update(req.data or {}) if 'sign' in args: args.pop('sign') if 'apiKey' not in args: @@ -38,5 +38,6 @@ class OkexFutureHttpClient(HttpClient): sign = hashlib.md5(data.encode()).hexdigest().upper() data += "&sign=" + sign - return method, path, params, data, {'Content-Type': 'application/x-www-form-urlencoded'} + req.headers = {'Content-Type': 'application/x-www-form-urlencoded'} + return req diff --git a/vnpy/network/HeadersAuthenticateHttpClient.py b/vnpy/network/HeadersAuthenticateHttpClient.py deleted file mode 100644 index a50730e6..00000000 --- a/vnpy/network/HeadersAuthenticateHttpClient.py +++ /dev/null @@ -1,38 +0,0 @@ -# encoding: UTF-8 - - -######################################################################## -from abc import abstractmethod - -from vnpy.network.HttpClient import HttpClient - - -######################################################################## -class HeadersAuthenticateHttpClient(HttpClient): - """ - 该类简化了RESTFulAPI客户端的重载。 - 该类提供了一个setUser函数,可以方便地设置apiKey和apiSecret。 - 使用self.apiKey和self.apiSecret便可以访问设置后的值 - 要建立一个签名在HTTP Headers的RESTFul客户端,继承该类并重载authencitate即可。 - """ - - #---------------------------------------------------------------------- - def __init__(self): - super(HeadersAuthenticateHttpClient, self).__init__() - self.apiKey = None # type: str - self.apiSecret = None # type: str - - #---------------------------------------------------------------------- - def beforeRequest(self, method, path, params, data): # type: (str, str, dict, dict)->(str, str, dict, dict, dict) - return method, path, params, data, self.onAuthenticate(method, path, params, data) - - #---------------------------------------------------------------------- - @abstractmethod - def onAuthenticate(self, method, path, params, data): - """ - 重载该函数以添加签名到头部 - 该函数在每个请求之前都会被调用。 - @:return dict 返回的数据会被加入到HTTP请求头部中 - - """ - return {}