[Mod] sendReq改名为sendRequest(同步),addReq改名为addRequest(异步)
This commit is contained in:
parent
c8a071f38e
commit
a3129a8007
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
@ -172,7 +172,7 @@ 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,
|
||||
@ -196,7 +196,7 @@ class OkexFutureRestClient(OkexFutureRestBase):
|
||||
'contractType': contractType,
|
||||
'order_id': orderId
|
||||
}
|
||||
return self.addReq('POST',
|
||||
return self.addRequest('POST',
|
||||
'/future_cancel.do',
|
||||
callback=self.onOrderCanceled,
|
||||
data=data,
|
||||
@ -221,7 +221,7 @@ 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,
|
||||
@ -253,7 +253,7 @@ class OkexFutureRestClient(OkexFutureRestBase):
|
||||
'page_length': pageLength
|
||||
}
|
||||
|
||||
return self.addReq('POST',
|
||||
return self.addRequest('POST',
|
||||
'/future_order_info.do',
|
||||
callback=self.onOrder,
|
||||
data=data,
|
||||
@ -269,7 +269,7 @@ 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))
|
||||
@ -290,7 +290,7 @@ class OkexFutureRestClient(OkexFutureRestBase):
|
||||
'symbol': symbol,
|
||||
'contractType': contractType
|
||||
}
|
||||
return self.addReq('POST',
|
||||
return self.addRequest('POST',
|
||||
'/future_position.do',
|
||||
data=data,
|
||||
callback=self.onPosition,
|
||||
|
@ -139,7 +139,7 @@ class RestClient(object):
|
||||
self._queue.join()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def addReq(self, method, path, callback,
|
||||
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
|
||||
@ -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)
|
||||
|
||||
|
@ -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()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user