Kaynağa Gözat

Add support for only reading part of the response into memory

tags/v0.2.2
JustAnotherArchivist 4 yıl önce
ebeveyn
işleme
8358ba9131
1 değiştirilmiş dosya ile 14 ekleme ve 2 silme
  1. +14
    -2
      qwarc/aiohttp.py

+ 14
- 2
qwarc/aiohttp.py Dosyayı Görüntüle

@@ -116,7 +116,7 @@ class ClientResponse(aiohttp.client_reqrep.ClientResponse):
def iter_all(self): def iter_all(self):
return itertools.chain(self.history, (self,)) return itertools.chain(self.history, (self,))


async def _read(self):
async def _read(self, nbytes = None):
#FIXME: This uses internal undocumented APIs of aiohttp #FIXME: This uses internal undocumented APIs of aiohttp
payload = Payload() payload = Payload()
self._rawData.responseData.seek(0) self._rawData.responseData.seek(0)
@@ -135,6 +135,10 @@ class ClientResponse(aiohttp.client_reqrep.ClientResponse):
if not chunk: if not chunk:
break break
eof, data = parser.feed_data(chunk) eof, data = parser.feed_data(chunk)
if nbytes is not None and payload.data.tell() >= nbytes:
if payload.exc:
raise Exception from payload.exc
return payload.data.getvalue()[:nbytes]
# data can only not be None if eof is True, so there is no need to actually do anything about it # data can only not be None if eof is True, so there is no need to actually do anything about it
if eof: if eof:
break break
@@ -145,8 +149,16 @@ class ClientResponse(aiohttp.client_reqrep.ClientResponse):
raise Exception from payload.exc raise Exception from payload.exc
return payload.data.getvalue() return payload.data.getvalue()


async def read(self):
async def read(self, nbytes = None):
'''
Read up to nbytes from the response payload, or the entire response if nbytes is None.
Note that this method always starts from the beginning of the response even if called repeatedly.
'''
#FIXME: Uses internal aiohttp attribute _content #FIXME: Uses internal aiohttp attribute _content
if nbytes is not None:
if self._content is not None:
return self._content[:nbytes]
return (await self._read(nbytes))
if self._content is None: if self._content is None:
self._content = await self._read() self._content = await self._read()
return self._content return self._content


Yükleniyor…
İptal
Kaydet