[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):
|
def callback(data, req):
|
||||||
self.c.p.set_result(data['args'])
|
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)
|
res = self.c.p.get(3)
|
||||||
|
|
||||||
self.assertEqual(args, res)
|
self.assertEqual(args, res)
|
||||||
@ -63,7 +63,7 @@ class RestfulClientTest(unittest.TestCase):
|
|||||||
def callback(data, req):
|
def callback(data, req):
|
||||||
self.c.p.set_result(data['json'])
|
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)
|
res = self.c.p.get(3)
|
||||||
|
|
||||||
self.assertEqual(body, res)
|
self.assertEqual(body, res)
|
||||||
@ -72,7 +72,7 @@ class RestfulClientTest(unittest.TestCase):
|
|||||||
def callback(data, req):
|
def callback(data, req):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.c.addReq('POST', '/status/401', callback)
|
self.c.addRequest('POST', '/status/401', callback)
|
||||||
with self.assertRaises(FailedError):
|
with self.assertRaises(FailedError):
|
||||||
self.c.p.get(3)
|
self.c.p.get(3)
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ class RestfulClientTest(unittest.TestCase):
|
|||||||
def callback(data, req):
|
def callback(data, req):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.c.addReq('GET', '/image/svg', callback)
|
self.c.addRequest('GET', '/image/svg', callback)
|
||||||
with self.assertRaises(JSONDecodeError):
|
with self.assertRaises(JSONDecodeError):
|
||||||
self.c.p.get(3)
|
self.c.p.get(3)
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ class WebsocketClientTest(unittest.TestCase):
|
|||||||
req = {
|
req = {
|
||||||
'name': 'val'
|
'name': 'val'
|
||||||
}
|
}
|
||||||
self.c.sendReq(req)
|
self.c.sendRequest(req)
|
||||||
res = self.c.p.get(3)
|
res = self.c.p.get(3)
|
||||||
|
|
||||||
self.assertEqual(res, req)
|
self.assertEqual(res, req)
|
||||||
|
@ -172,11 +172,11 @@ class OkexFutureRestClient(OkexFutureRestBase):
|
|||||||
if leverRate:
|
if leverRate:
|
||||||
data['lever_rate'] = leverRate # 杠杆倍数
|
data['lever_rate'] = leverRate # 杠杆倍数
|
||||||
|
|
||||||
request = self.addReq('POST',
|
request = self.addRequest('POST',
|
||||||
'/future_trade.do',
|
'/future_trade.do',
|
||||||
callback=self.onOrderSent,
|
callback=self.onOrderSent,
|
||||||
data=data,
|
data=data,
|
||||||
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
||||||
return request
|
return request
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
@ -196,11 +196,11 @@ class OkexFutureRestClient(OkexFutureRestBase):
|
|||||||
'contractType': contractType,
|
'contractType': contractType,
|
||||||
'order_id': orderId
|
'order_id': orderId
|
||||||
}
|
}
|
||||||
return self.addReq('POST',
|
return self.addRequest('POST',
|
||||||
'/future_cancel.do',
|
'/future_cancel.do',
|
||||||
callback=self.onOrderCanceled,
|
callback=self.onOrderCanceled,
|
||||||
data=data,
|
data=data,
|
||||||
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def queryOrder(self, symbol, contractType, orderId, onSuccess, onFailed=None,
|
def queryOrder(self, symbol, contractType, orderId, onSuccess, onFailed=None,
|
||||||
@ -221,11 +221,11 @@ class OkexFutureRestClient(OkexFutureRestBase):
|
|||||||
'contractType': contractType,
|
'contractType': contractType,
|
||||||
'order_id': orderId
|
'order_id': orderId
|
||||||
}
|
}
|
||||||
return self.addReq('POST',
|
return self.addRequest('POST',
|
||||||
'/future_order_info.do',
|
'/future_order_info.do',
|
||||||
callback=self.onOrder,
|
callback=self.onOrder,
|
||||||
data=data,
|
data=data,
|
||||||
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def queryOrders(self, symbol, contractType, status,
|
def queryOrders(self, symbol, contractType, status,
|
||||||
@ -253,11 +253,11 @@ class OkexFutureRestClient(OkexFutureRestBase):
|
|||||||
'page_length': pageLength
|
'page_length': pageLength
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.addReq('POST',
|
return self.addRequest('POST',
|
||||||
'/future_order_info.do',
|
'/future_order_info.do',
|
||||||
callback=self.onOrder,
|
callback=self.onOrder,
|
||||||
data=data,
|
data=data,
|
||||||
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def queryUserInfo(self, onSuccess, onFailed=None,
|
def queryUserInfo(self, onSuccess, onFailed=None,
|
||||||
@ -269,10 +269,10 @@ class OkexFutureRestClient(OkexFutureRestBase):
|
|||||||
:param extra: Any
|
:param extra: Any
|
||||||
:return: Request
|
:return: Request
|
||||||
"""
|
"""
|
||||||
return self.addReq('POST',
|
return self.addRequest('POST',
|
||||||
'/future_userinfo.do',
|
'/future_userinfo.do',
|
||||||
callback=self.onOrder,
|
callback=self.onOrder,
|
||||||
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def queryPosition(self, symbol, contractType,
|
def queryPosition(self, symbol, contractType,
|
||||||
@ -290,11 +290,11 @@ class OkexFutureRestClient(OkexFutureRestBase):
|
|||||||
'symbol': symbol,
|
'symbol': symbol,
|
||||||
'contractType': contractType
|
'contractType': contractType
|
||||||
}
|
}
|
||||||
return self.addReq('POST',
|
return self.addRequest('POST',
|
||||||
'/future_position.do',
|
'/future_position.do',
|
||||||
data=data,
|
data=data,
|
||||||
callback=self.onPosition,
|
callback=self.onPosition,
|
||||||
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
extra=_OkexFutureCustomExtra(onSuccess, onFailed, extra))
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -139,10 +139,10 @@ class RestClient(object):
|
|||||||
self._queue.join()
|
self._queue.join()
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def addReq(self, method, path, callback,
|
def addRequest(self, method, path, callback,
|
||||||
params=None, data=None, headers = None,
|
params=None, data=None, headers = None,
|
||||||
onFailed=None, skipDefaultOnFailed=True,
|
onFailed=None, skipDefaultOnFailed=True,
|
||||||
extra=None): # type: (str, str, Callable[[dict, Request], Any], dict, dict, dict, Callable[[dict, Request], Any], bool, Any)->Request
|
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
|
:param method: GET, POST, PUT, DELETE, QUERY
|
||||||
@ -171,7 +171,7 @@ class RestClient(object):
|
|||||||
try:
|
try:
|
||||||
req = self._queue.get(timeout=1)
|
req = self._queue.get(timeout=1)
|
||||||
try:
|
try:
|
||||||
self._processReq(req, session)
|
self._processRequest(req, session)
|
||||||
finally:
|
finally:
|
||||||
self._queue.task_done()
|
self._queue.task_done()
|
||||||
except Empty:
|
except Empty:
|
||||||
@ -215,8 +215,10 @@ class RestClient(object):
|
|||||||
sys.excepthook(exceptionType, exceptionValue, tb)
|
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:
|
try:
|
||||||
req = self.beforeRequest(req)
|
req = self.beforeRequest(req)
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ import time
|
|||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from threading import Thread, Lock
|
from threading import Thread, Lock
|
||||||
|
|
||||||
import vnpy.api.websocket
|
import websocket
|
||||||
|
|
||||||
|
|
||||||
class WebsocketClient(object):
|
class WebsocketClient(object):
|
||||||
@ -55,18 +55,18 @@ class WebsocketClient(object):
|
|||||||
self._disconnect()
|
self._disconnect()
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def sendReq(self, req): # type: (dict)->None
|
def sendRequest(self, dictObj): # type: (dict)->None
|
||||||
"""发出请求"""
|
"""发出请求:相当于sendText(json.dumps(dictObj))"""
|
||||||
return self._get_ws().send(json.dumps(req), opcode=vnpy.api.websocket.ABNF.OPCODE_TEXT)
|
return self._get_ws().send(json.dumps(dictObj), opcode=vnpy.api.websocket.ABNF.OPCODE_TEXT)
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def sendText(self, text): # type: (str)->None
|
def sendText(self, text): # type: (str)->None
|
||||||
"""发出请求"""
|
"""发送文本数据"""
|
||||||
return self._get_ws().send(text, opcode=vnpy.api.websocket.ABNF.OPCODE_TEXT)
|
return self._get_ws().send(text, opcode=vnpy.api.websocket.ABNF.OPCODE_TEXT)
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def sendData(self, data): # type: (bytes)->None
|
def sendData(self, data): # type: (bytes)->None
|
||||||
"""发出请求"""
|
"""发送字节数据"""
|
||||||
return self._get_ws().send_binary(data)
|
return self._get_ws().send_binary(data)
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
@ -78,7 +78,7 @@ class WebsocketClient(object):
|
|||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
def _connect(self):
|
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()
|
self.onConnect()
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user