[Mod] onFailed只接收两个参数,原来的data参数可以从req.response.raw读出

This commit is contained in:
nanoric 2018-10-10 22:09:15 -04:00
parent e6f7781a47
commit 6e71c59c06
2 changed files with 10 additions and 7 deletions

View File

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

View File

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