Skip to content

Commit

Permalink
Workaround aiohttp request with filelike objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kxepal committed Sep 17, 2014
1 parent 7066803 commit e574d19
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions aiocouchdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import asyncio
import aiohttp
import io
import json
import mimetypes
import types
import urllib.parse
from .errors import maybe_raise_error
Expand All @@ -24,13 +26,34 @@ class HttpRequest(aiohttp.client.ClientRequest):
'ACCEPT-ENCODING': 'gzip, deflate',
'CONTENT-TYPE': 'application/json'
}
CHUNK_SIZE = 8192

def update_body_from_data(self, data):
"""Encodes ``data`` as JSON if `Content-Type`
is :mimetype:`application/json`."""
if self.headers.get('CONTENT-TYPE') == 'application/json':
if not (isinstance(data, types.GeneratorType)
or hasattr(data, 'read')):
if isinstance(data, io.IOBase):
assert not isinstance(data, io.StringIO), \
'attempt to send text data instead of binary'
self.body = data
self.chunked = True
if hasattr(data, 'mode'):
if data.mode == 'r':
raise ValueError('file {!r} should be open in binary mode'
''.format(data))
if hasattr(data, 'name'):
mime = mimetypes.guess_type(data.name)[0]
mime = 'application/octet-stream' if mime is None else mime
self.headers['CONTENT-TYPE'] = mime

def reader(data):
chunk = data.read(self.CHUNK_SIZE)
while chunk:
yield chunk
chunk = data.read(self.CHUNK_SIZE)
data = reader(data)

elif self.headers.get('CONTENT-TYPE') == 'application/json':
if not (isinstance(data, types.GeneratorType)):
data = json.dumps(data)
return super().update_body_from_data(data)

Expand Down

0 comments on commit e574d19

Please sign in to comment.