From 713c183a1494e29273d79af7c3c8ded4958b0baa Mon Sep 17 00:00:00 2001 From: nanoric Date: Tue, 16 Oct 2018 00:37:19 -0400 Subject: [PATCH] =?UTF-8?q?[Mod]=20=E5=B0=86RestClient=E5=92=8CWebSocketCl?= =?UTF-8?q?ient=E7=9A=84=E9=BB=98=E8=AE=A4=E5=9B=9E=E8=B0=83=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=8F=AF=E8=B5=8B=E5=80=BC=E7=9A=84=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/okexfuture/vnokexFuture.py | 6 ----- vnpy/api/rest/RestClient.py | 20 ++++++++------ vnpy/api/websocket/WebSocketClient.py | 39 ++++++++++++++++----------- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/vnpy/api/okexfuture/vnokexFuture.py b/vnpy/api/okexfuture/vnokexFuture.py index eb5f125e..6f9ea6b4 100644 --- a/vnpy/api/okexfuture/vnokexFuture.py +++ b/vnpy/api/okexfuture/vnokexFuture.py @@ -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 diff --git a/vnpy/api/rest/RestClient.py b/vnpy/api/rest/RestClient.py index 420637b1..b9906bce 100644 --- a/vnpy/api/rest/RestClient.py +++ b/vnpy/api/rest/RestClient.py @@ -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 """ diff --git a/vnpy/api/websocket/WebSocketClient.py b/vnpy/api/websocket/WebSocketClient.py index dd29a738..1a702a9e 100644 --- a/vnpy/api/websocket/WebSocketClient.py +++ b/vnpy/api/websocket/WebSocketClient.py @@ -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)