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
-------
InvalidArgument
If ``fp.name`` is an invalid default for ``filename``.
HTTPException
Sending the file failed.
@ -852,24 +850,22 @@ class Client:
channel_id = self._resolve_destination(destination)
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:
# attempt to open the file and send the request
with open(fp, 'rb') as f:
files = {
'file': (fp if filename is None else filename, f)
}
files.add_field('file', f, filename=filename)
response = yield from self.session.post(url, data=files, headers=headers)
except TypeError:
# if we got a TypeError then this is probably a file-like object
fname = getattr(fp, 'name', None) if filename is None else filename
if fname is None:
raise InvalidArgument('file-like object has no name attribute and no filename was specified')
files = {
'file': (fname, fp)
}
files.add_field('file', fp, filename=filename)
response = yield from self.session.post(url, data=files, headers=headers)
response = yield from self.session.post(url, data=files, headers=self.headers)
log.debug(request_logging_format.format(method='POST', response=response))
yield from utils._verify_successful_response(response)
data = yield from response.json()

Loading…
Cancel
Save