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 {}