[Mod] 将RestClient和WebSocketClient的默认回调改为可赋值的函数

This commit is contained in:
nanoric 2018-10-16 00:37:19 -04:00
parent a5fc1bdba0
commit 713c183a14
3 changed files with 36 additions and 29 deletions

View File

@ -1,7 +1,6 @@
# encoding: UTF-8
import hashlib
import urllib
from abc import abstractmethod
from vnpy.api.rest import Request, RestClient
from vnpy.api.websocket import WebSocketClient
@ -80,8 +79,3 @@ class OkexFutureWebSocketBase(WebSocketClient):
signature = sign(data, self.apiSecret)
dictObj['sign'] = signature
return super(OkexFutureWebSocketBase, self).sendPacket(dictObj)
#----------------------------------------------------------------------
@abstractmethod
def onPacket(self, packet):
pass

View File

@ -3,7 +3,6 @@
import sys
from Queue import Empty, Queue
from abc import abstractmethod
from multiprocessing.dummy import Pool
import requests
@ -79,10 +78,10 @@ class RestClient(object):
"""
HTTP 客户端目前是为了对接各种RESTfulAPI而设计的
如果需要给请求加上签名重载beforeRequest函数
如果需要处理非200的请求重载onFailed函数
如果需要给请求加上签名设置beforeRequest, 函数类型请参考defaultBeforeRequest
如果需要处理非200的请求设置onFailed函数类型请参考defaultOnFailed
如果每一个请求的非200返回都需要单独处理使用addReq函数的onFailed参数
如果捕获Python内部错误例如网络连接失败等等重载onError函数
如果捕获Python内部错误例如网络连接失败等等设置onError函数类型请参考defaultOnError
"""
#----------------------------------------------------------------------
@ -92,6 +91,9 @@ class RestClient(object):
"""
self.urlBase = None # type: str
self.sessionProvider = requestsSessionProvider
self.beforeRequest = self.defaultBeforeRequest # 任何请求在发送之前都会经过这个函数,让其加工
self.onError = self.defaultOnError # Python内部错误处理
self.onFailed = self.defaultOnFailed # statusCode != 2xx 时触发
self._active = False
@ -178,8 +180,8 @@ class RestClient(object):
pass
#----------------------------------------------------------------------
@abstractmethod
def beforeRequest(self, req): # type: (Request)->Request
@staticmethod
def defaultBeforeRequest(req): # type: (Request)->Request
"""
所有请求在发送之前都会经过这个函数
签名之类的前奏可以在这里面实现
@ -189,7 +191,8 @@ class RestClient(object):
return req
#----------------------------------------------------------------------
def onFailed(self, httpStatusCode, req): # type:(int, Request)->None
@staticmethod
def defaultOnFailed(httpStatusCode, req): # type:(int, Request)->None
"""
请求失败处理函数HttpStatusCode!=200.
默认行为是打印到stderr
@ -207,7 +210,8 @@ class RestClient(object):
req._response.raw))
#----------------------------------------------------------------------
def onError(self, exceptionType, exceptionValue, tb, req):
@staticmethod
def defaultOnError(exceptionType, exceptionValue, tb, req):
"""
Python内部错误处理默认行为是仍给excepthook
"""

View File

@ -3,38 +3,46 @@
########################################################################
import json
import ssl
import sys
import time
from abc import abstractmethod
from threading import Thread, Lock
import ssl
import time
import websocket
from threading import Lock, Thread
class WebSocketClient(object):
"""
Websocket API
继承使用该类
实例化之后应调用start开始后台线程调用start()函数会自动连接websocket
若要终止后台线程请调用stop() stop()函数会顺便断开websocket
可以重写以下函数
该类默认打包方式为json若从服务器返回的数据不为json则会触发onError
可以覆盖以下回调
onConnected
onDisconnected
onPacket
onPacket # 数据回调只有在返回的数据帧为text并且内容为json时才会回调
onError
当然为了不让用户随意自定义用自己的init函数覆盖掉原本的init(host)也是个不错的选择
@note 继承使用该类
关于ping
在调用start()之后该类每60s会自动发送一个ping帧至服务器
"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
self.host = None # type: str
self.onConnected = self.defaultOnConnected
self.onDisconnected = self.onDisconnected
self.onPacket = self.defaultOnPacket
self.onError = self.defaultOnError
self.createConnection = websocket.create_connection
self._ws_lock = Lock()
self._ws = None # type: websocket.WebSocket
@ -42,8 +50,6 @@ class WebSocketClient(object):
self._workerThread = None # type: Thread
self._pingThread = None # type: Thread
self._active = False
self.createConnection = websocket.create_connection
#----------------------------------------------------------------------
def setCreateConnection(self, func):
@ -155,22 +161,24 @@ class WebSocketClient(object):
return self._get_ws().send('ping', websocket.ABNF.OPCODE_PING)
#----------------------------------------------------------------------
def onConnected(self):
@staticmethod
def defaultOnConnected():
"""
连接成功回调
"""
pass
#----------------------------------------------------------------------
def onDisconnected(self):
@staticmethod
def defaultOnDisconnected():
"""
连接断开回调
"""
pass
#----------------------------------------------------------------------
@abstractmethod
def onPacket(self, packet):
@staticmethod
def defaultOnPacket(packet):
"""
数据回调
只有在数据为json包的时候才会触发这个回调
@ -180,6 +188,7 @@ class WebSocketClient(object):
pass
#----------------------------------------------------------------------
def onError(self, exceptionType, exceptionValue, tb):
@staticmethod
def defaultOnError(exceptionType, exceptionValue, tb):
"""Python错误回调"""
return sys.excepthook(exceptionType, exceptionValue, tb)