From a3129a80079de37358698f4d0ce5e1c9829f1518 Mon Sep 17 00:00:00 2001 From: nanoric Date: Mon, 15 Oct 2018 03:11:17 -0400 Subject: [PATCH] =?UTF-8?q?[Mod]=20sendReq=E6=94=B9=E5=90=8D=E4=B8=BAsendR?= =?UTF-8?q?equest=EF=BC=88=E5=90=8C=E6=AD=A5=EF=BC=89=EF=BC=8CaddReq?= =?UTF-8?q?=E6=94=B9=E5=90=8D=E4=B8=BAaddRequest=EF=BC=88=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/api/base/RestClientTest.py | 8 ++--- tests/api/base/WebSocketClientTest.py | 2 +- vnpy/api/okexfuture/OkexFutureApi.py | 46 +++++++++++++-------------- vnpy/api/rest/RestClient.py | 16 ++++++---- vnpy/api/websocket/WebSocketClient.py | 14 ++++---- 5 files changed, 44 insertions(+), 42 deletions(-) diff --git a/tests/api/base/RestClientTest.py b/tests/api/base/RestClientTest.py index 36da9289..6f350638 100644 --- a/tests/api/base/RestClientTest.py +++ b/tests/api/base/RestClientTest.py @@ -51,7 +51,7 @@ class RestfulClientTest(unittest.TestCase): def callback(data, req): self.c.p.set_result(data['args']) - self.c.addReq('GET', '/get', callback, params=args) + self.c.addRequest('GET', '/get', callback, params=args) res = self.c.p.get(3) self.assertEqual(args, res) @@ -63,7 +63,7 @@ class RestfulClientTest(unittest.TestCase): def callback(data, req): self.c.p.set_result(data['json']) - self.c.addReq('POST', '/post', callback, data=body) + self.c.addRequest('POST', '/post', callback, data=body) res = self.c.p.get(3) self.assertEqual(body, res) @@ -72,7 +72,7 @@ class RestfulClientTest(unittest.TestCase): def callback(data, req): pass - self.c.addReq('POST', '/status/401', callback) + self.c.addRequest('POST', '/status/401', callback) with self.assertRaises(FailedError): self.c.p.get(3) @@ -80,7 +80,7 @@ class RestfulClientTest(unittest.TestCase): def callback(data, req): pass - self.c.addReq('GET', '/image/svg', callback) + self.c.addRequest('GET', '/image/svg', callback) with self.assertRaises(JSONDecodeError): self.c.p.get(3) diff --git a/tests/api/base/WebSocketClientTest.py b/tests/api/base/WebSocketClientTest.py index 8361f647..0592f2c1 100644 --- a/tests/api/base/WebSocketClientTest.py +++ b/tests/api/base/WebSocketClientTest.py @@ -38,7 +38,7 @@ class WebsocketClientTest(unittest.TestCase): req = { 'name': 'val' } - self.c.sendReq(req) + self.c.sendRequest(req) res = self.c.p.get(3) self.assertEqual(res, req) diff --git a/vnpy/api/okexfuture/OkexFutureApi.py b/vnpy/api/okexfuture/OkexFutureApi.py index c5edfa9d..b6d801c9 100644 --- a/vnpy/api/okexfuture/OkexFutureApi.py +++ b/vnpy/api/okexfuture/OkexFutureApi.py @@ -172,11 +172,11 @@ class OkexFutureRestClient(OkexFutureRestBase): if leverRate: data['lever_rate'] = leverRate # 杠杆倍数 - request = self.addReq('POST', + request = self.addRequest('POST', '/future_trade.do', - callback=self.onOrderSent, - data=data, - extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) + callback=self.onOrderSent, + data=data, + extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) return request #---------------------------------------------------------------------- @@ -196,11 +196,11 @@ class OkexFutureRestClient(OkexFutureRestBase): 'contractType': contractType, 'order_id': orderId } - return self.addReq('POST', + return self.addRequest('POST', '/future_cancel.do', - callback=self.onOrderCanceled, - data=data, - extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) + callback=self.onOrderCanceled, + data=data, + extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) #---------------------------------------------------------------------- def queryOrder(self, symbol, contractType, orderId, onSuccess, onFailed=None, @@ -221,11 +221,11 @@ class OkexFutureRestClient(OkexFutureRestBase): 'contractType': contractType, 'order_id': orderId } - return self.addReq('POST', + return self.addRequest('POST', '/future_order_info.do', - callback=self.onOrder, - data=data, - extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) + callback=self.onOrder, + data=data, + extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) #---------------------------------------------------------------------- def queryOrders(self, symbol, contractType, status, @@ -253,11 +253,11 @@ class OkexFutureRestClient(OkexFutureRestBase): 'page_length': pageLength } - return self.addReq('POST', + return self.addRequest('POST', '/future_order_info.do', - callback=self.onOrder, - data=data, - extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) + callback=self.onOrder, + data=data, + extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) #---------------------------------------------------------------------- def queryUserInfo(self, onSuccess, onFailed=None, @@ -269,10 +269,10 @@ class OkexFutureRestClient(OkexFutureRestBase): :param extra: Any :return: Request """ - return self.addReq('POST', + return self.addRequest('POST', '/future_userinfo.do', - callback=self.onOrder, - extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) + callback=self.onOrder, + extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) #---------------------------------------------------------------------- def queryPosition(self, symbol, contractType, @@ -290,11 +290,11 @@ class OkexFutureRestClient(OkexFutureRestBase): 'symbol': symbol, 'contractType': contractType } - return self.addReq('POST', + return self.addRequest('POST', '/future_position.do', - data=data, - callback=self.onPosition, - extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) + data=data, + callback=self.onPosition, + extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra)) #---------------------------------------------------------------------- @staticmethod diff --git a/vnpy/api/rest/RestClient.py b/vnpy/api/rest/RestClient.py index a1f5c06e..420637b1 100644 --- a/vnpy/api/rest/RestClient.py +++ b/vnpy/api/rest/RestClient.py @@ -139,10 +139,10 @@ class RestClient(object): self._queue.join() #---------------------------------------------------------------------- - def addReq(self, method, path, callback, - params=None, data=None, headers = None, - onFailed=None, skipDefaultOnFailed=True, - extra=None): # type: (str, str, Callable[[dict, Request], Any], dict, dict, dict, Callable[[dict, Request], Any], bool, Any)->Request + def addRequest(self, method, path, callback, + params=None, data=None, headers = None, + onFailed=None, skipDefaultOnFailed=True, + extra=None): # type: (str, str, Callable[[dict, Request], Any], dict, dict, dict, Callable[[dict, Request], Any], bool, Any)->Request """ 发送一个请求 :param method: GET, POST, PUT, DELETE, QUERY @@ -171,7 +171,7 @@ class RestClient(object): try: req = self._queue.get(timeout=1) try: - self._processReq(req, session) + self._processRequest(req, session) finally: self._queue.task_done() except Empty: @@ -215,8 +215,10 @@ class RestClient(object): sys.excepthook(exceptionType, exceptionValue, tb) #---------------------------------------------------------------------- - def _processReq(self, req, session): # type: (Request, requests.Session)->None - """处理请求""" + def _processRequest(self, req, session): # type: (Request, requests.Session)->None + """ + 用于内部:将请求发送出去 + """ try: req = self.beforeRequest(req) diff --git a/vnpy/api/websocket/WebSocketClient.py b/vnpy/api/websocket/WebSocketClient.py index 12cbfca5..7cc82429 100644 --- a/vnpy/api/websocket/WebSocketClient.py +++ b/vnpy/api/websocket/WebSocketClient.py @@ -9,7 +9,7 @@ import time from abc import abstractmethod from threading import Thread, Lock -import vnpy.api.websocket +import websocket class WebsocketClient(object): @@ -55,18 +55,18 @@ class WebsocketClient(object): self._disconnect() #---------------------------------------------------------------------- - def sendReq(self, req): # type: (dict)->None - """发出请求""" - return self._get_ws().send(json.dumps(req), opcode=vnpy.api.websocket.ABNF.OPCODE_TEXT) + def sendRequest(self, dictObj): # type: (dict)->None + """发出请求:相当于sendText(json.dumps(dictObj))""" + return self._get_ws().send(json.dumps(dictObj), opcode=vnpy.api.websocket.ABNF.OPCODE_TEXT) #---------------------------------------------------------------------- def sendText(self, text): # type: (str)->None - """发出请求""" + """发送文本数据""" return self._get_ws().send(text, opcode=vnpy.api.websocket.ABNF.OPCODE_TEXT) #---------------------------------------------------------------------- def sendData(self, data): # type: (bytes)->None - """发出请求""" + """发送字节数据""" return self._get_ws().send_binary(data) #---------------------------------------------------------------------- @@ -78,7 +78,7 @@ class WebsocketClient(object): #---------------------------------------------------------------------- def _connect(self): """""" - self._ws = vnpy.api.websocket.create_connection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE}) + self._ws = websocket.create_connection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE}) self.onConnect() #----------------------------------------------------------------------