Browse Source

Fix send_file to actually work with aiohttp.

pull/57/head
Rapptz 9 years ago
parent
commit
0009225d08
  1. 24
      discord/client.py

24
discord/client.py

@ -838,8 +838,6 @@ class Client:
Raises Raises
------- -------
InvalidArgument
If ``fp.name`` is an invalid default for ``filename``.
HTTPException HTTPException
Sending the file failed. Sending the file failed.
@ -852,24 +850,22 @@ class Client:
channel_id = self._resolve_destination(destination) channel_id = self._resolve_destination(destination)
url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id) url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id)
files = aiohttp.FormData()
# we don't want the content-type json in this request
headers = {
'authorization': self.token
}
try: try:
# attempt to open the file and send the request # attempt to open the file and send the request
with open(fp, 'rb') as f: with open(fp, 'rb') as f:
files = { files.add_field('file', f, filename=filename)
'file': (fp if filename is None else filename, f) response = yield from self.session.post(url, data=files, headers=headers)
}
except TypeError: except TypeError:
# if we got a TypeError then this is probably a file-like object files.add_field('file', fp, filename=filename)
fname = getattr(fp, 'name', None) if filename is None else filename response = yield from self.session.post(url, data=files, headers=headers)
if fname is None:
raise InvalidArgument('file-like object has no name attribute and no filename was specified')
files = {
'file': (fname, fp)
}
response = yield from self.session.post(url, data=files, headers=self.headers)
log.debug(request_logging_format.format(method='POST', response=response)) log.debug(request_logging_format.format(method='POST', response=response))
yield from utils._verify_successful_response(response) yield from utils._verify_successful_response(response)
data = yield from response.json() data = yield from response.json()

Loading…
Cancel
Save