[Mod] sendReq改名为sendRequest(同步),addReq改名为addRequest(异步)

This commit is contained in:
nanoric 2018-10-15 03:11:17 -04:00
parent c8a071f38e
commit a3129a8007
5 changed files with 44 additions and 42 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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()
#----------------------------------------------------------------------