def asend(self, *, sess=None, timeout=10.0):
'''
Sends the request to the server.
This method is a coroutine.
'''
assert self.method in self._allowed_methods
if sess is None:
sess = aiohttp.ClientSession()
else:
assert isinstance(sess, aiohttp.ClientSession)
with sess:
if self.content_type == 'multipart/form-data':
with aiohttp.MultipartWriter('mixed') as mpwriter:
for file in self._content:
part = mpwriter.append(file.file)
part.set_content_disposition('attachment',
filename=file.filename)
data = mpwriter
else:
data = self._content
self._sign()
reqfunc = getattr(sess, self.method.lower())
try:
with _timeout(timeout):
resp = await reqfunc(self.build_url(),
data=data,
headers=self.headers)
async with resp:
body = await resp.read()
return Response(resp.status, resp.reason, body,
resp.content_type,
len(body))
except Exception as e:
msg = 'Request to the API endpoint has failed.\n' \
'Check your network connection and/or the server status.'
raise BackendClientError(msg) from e
评论列表
文章目录