[Mod] 不用简写:req改名为request

This commit is contained in:
nanoric 2018-10-16 02:19:14 -04:00
parent 713c183a14
commit 995d994e8e

View File

@ -159,21 +159,21 @@ class RestClient(object):
:return: Request :return: Request
""" """
req = Request(method, path, callback, params, data, headers) request = Request(method, path, callback, params, data, headers)
req.onFailed = onFailed request.onFailed = onFailed
req.skipDefaultOnFailed = skipDefaultOnFailed request.skipDefaultOnFailed = skipDefaultOnFailed
req.extra = extra request.extra = extra
self._queue.put(req) self._queue.put(request)
return req return request
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def _run(self): def _run(self):
session = self.sessionProvider() session = self.sessionProvider()
while self._active: while self._active:
try: try:
req = self._queue.get(timeout=1) request = self._queue.get(timeout=1)
try: try:
self._processRequest(req, session) self._processRequest(request, session)
finally: finally:
self._queue.task_done() self._queue.task_done()
except Empty: except Empty:
@ -181,18 +181,18 @@ class RestClient(object):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
@staticmethod @staticmethod
def defaultBeforeRequest(req): # type: (Request)->Request def defaultBeforeRequest(request): # type: (Request)->Request
""" """
所有请求在发送之前都会经过这个函数 所有请求在发送之前都会经过这个函数
签名之类的前奏可以在这里面实现 签名之类的前奏可以在这里面实现
需要对request进行什么修改就做什么修改吧 需要对request进行什么修改就做什么修改吧
@:return (req) @:return (request)
""" """
return req return request
#---------------------------------------------------------------------- #----------------------------------------------------------------------
@staticmethod @staticmethod
def defaultOnFailed(httpStatusCode, req): # type:(int, Request)->None def defaultOnFailed(httpStatusCode, request): # type:(int, Request)->None
""" """
请求失败处理函数HttpStatusCode!=200. 请求失败处理函数HttpStatusCode!=200.
默认行为是打印到stderr 默认行为是打印到stderr
@ -203,52 +203,52 @@ class RestClient(object):
"data: {}\n" "data: {}\n"
"response:" "response:"
"{}\n" "{}\n"
.format(req.method, req.path, httpStatusCode, .format(request.method, request.path, httpStatusCode,
req.headers, request.headers,
req.params, request.params,
req.data, request.data,
req._response.raw)) request._response.raw))
#---------------------------------------------------------------------- #----------------------------------------------------------------------
@staticmethod @staticmethod
def defaultOnError(exceptionType, exceptionValue, tb, req): def defaultOnError(exceptionType, exceptionValue, tb, request):
""" """
Python内部错误处理默认行为是仍给excepthook Python内部错误处理默认行为是仍给excepthook
""" """
print("error in req : {}\n".format(req)) print("error in request : {}\n".format(request))
sys.excepthook(exceptionType, exceptionValue, tb) sys.excepthook(exceptionType, exceptionValue, tb)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def _processRequest(self, req, session): # type: (Request, requests.Session)->None def _processRequest(self, request, session): # type: (Request, requests.Session)->None
""" """
用于内部将请求发送出去 用于内部将请求发送出去
""" """
try: try:
req = self.beforeRequest(req) request = self.beforeRequest(request)
url = self.makeFullUrl(req.path) url = self.makeFullUrl(request.path)
response = session.request(req.method, url, headers=req.headers, params=req.params, data=req.data) response = session.request(request.method, url, headers=request.headers, params=request.params, data=request.data)
req._response = response request._response = response
httpStatusCode = response.status_code httpStatusCode = response.status_code
if httpStatusCode/100 == 2: if httpStatusCode/100 == 2:
jsonBody = response.json() jsonBody = response.json()
req.callback(jsonBody, req) request.callback(jsonBody, request)
req._status = RequestStatus.success request._status = RequestStatus.success
else: else:
req._status = RequestStatus.failed request._status = RequestStatus.failed
if req.onFailed: if request.onFailed:
req.onFailed(httpStatusCode, response.raw, req) request.onFailed(httpStatusCode, response.raw, request)
# 若没有onFailed或者没设置skipDefaultOnFailed则调用默认的处理函数 # 若没有onFailed或者没设置skipDefaultOnFailed则调用默认的处理函数
if not req.onFailed or not req.skipDefaultOnFailed: if not request.onFailed or not request.skipDefaultOnFailed:
self.onFailed(httpStatusCode, req) self.onFailed(httpStatusCode, request)
except: except:
req._status = RequestStatus.error request._status = RequestStatus.error
t, v, tb = sys.exc_info() t, v, tb = sys.exc_info()
self.onError(t, v, tb, req) self.onError(t, v, tb, request)
def makeFullUrl(self, path): def makeFullUrl(self, path):
url = self.urlBase + path url = self.urlBase + path