From 6e71c59c0648d4300d2b3dbf3e406c4a05548bfe Mon Sep 17 00:00:00 2001 From: nanoric Date: Wed, 10 Oct 2018 22:09:15 -0400 Subject: [PATCH] =?UTF-8?q?[Mod]=20onFailed=E5=8F=AA=E6=8E=A5=E6=94=B6?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E5=8F=82=E6=95=B0,=E5=8E=9F=E6=9D=A5?= =?UTF-8?q?=E7=9A=84data=E5=8F=82=E6=95=B0=E5=8F=AF=E4=BB=A5=E4=BB=8Ereq.r?= =?UTF-8?q?esponse.raw=E8=AF=BB=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/network/RestfulClientTest.py | 4 ++-- vnpy/network/RestClient.py | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/network/RestfulClientTest.py b/tests/network/RestfulClientTest.py index 7de57c1c..cba63063 100644 --- a/tests/network/RestfulClientTest.py +++ b/tests/network/RestfulClientTest.py @@ -30,7 +30,7 @@ class TestRestClient(RestClient): def onError(self, exceptionType, exceptionValue, tb, req): self.p.set_exception(exceptionValue) - def onFailed(self, httpStatusCode, data, req): + def onFailed(self, httpStatusCode, req): self.p.set_exception(FailedError("request failed")) @@ -71,7 +71,7 @@ class RestfulClientTest(unittest.TestCase): def callback(data, req): pass - self.c.addReq('POST', '/status/201', callback) + self.c.addReq('POST', '/status/401', callback) with self.assertRaises(FailedError): self.c.p.get(3) diff --git a/vnpy/network/RestClient.py b/vnpy/network/RestClient.py index 65014392..9905bc44 100644 --- a/vnpy/network/RestClient.py +++ b/vnpy/network/RestClient.py @@ -61,6 +61,11 @@ class Request(object): def error(self): return self._status == RequestStatus.error + #---------------------------------------------------------------------- + @property + def response(self): # type: ()->requests.Response + return self._response + #---------------------------------------------------------------------- def __str__(self): statusCode = 'not finished' @@ -171,12 +176,10 @@ class RestClient(object): return req #---------------------------------------------------------------------- - def onFailed(self, httpStatusCode, data, req): + def onFailed(self, httpStatusCode, req): # type:(int, Request)->None """ 请求失败处理函数(HttpStatusCode!=200). 默认行为是打印到stderr - - @:param data 这个data是原始数据,并不是dict。而且有可能为null。 """ print("reuqest : {} {} failed with {}: \n" "headers: {}\n" @@ -188,7 +191,7 @@ class RestClient(object): req.headers, req.params, req.data, - data)) + req._response.raw)) #---------------------------------------------------------------------- def onError(self, exceptionType, exceptionValue, tb, req): @@ -222,7 +225,7 @@ class RestClient(object): # 若没有onFailed或者没设置skipDefaultOnFailed,则调用默认的处理函数 if not req.onFailed or not req.skipDefaultOnFailed: - self.onFailed(httpStatusCode, response.raw, req) + self.onFailed(httpStatusCode, req) except: req._status = RequestStatus.error t, v, tb = sys.exc_info()