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()