Skip to content

Commit

Permalink
WIP: Add support for streaming requests
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Aug 17, 2021
1 parent 5d102cf commit e3148c1
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions aiohttp_client_cache/response.py
@@ -1,8 +1,12 @@
# TODO: CachedResponse may be better as a non-slotted subclass of ClientResponse.
# Will look into this when working on issue #67.
import asyncio
import json
from datetime import datetime
from http.cookies import SimpleCookie
from logging import getLogger
from typing import Any, Dict, Iterable, List, Mapping, Optional, Tuple, Union
from unittest.mock import Mock

import attr
from aiohttp import ClientResponse, ClientResponseError, hdrs, multipart
Expand Down Expand Up @@ -94,6 +98,10 @@ async def from_client_response(cls, client_response: ClientResponse, expires: da
)
return response

@property
def content(self) -> StreamReader:
return CachedStreamReader(self._body)

@property
def content_disposition(self) -> Optional[ContentDisposition]:
"""Get Content-Disposition headers, if any"""
Expand Down Expand Up @@ -214,6 +222,19 @@ async def terminate(self):
pass


class CachedStreamReader(StreamReader):
"""A StreamReader loaded from previously consumed response content. This feeds cached data into
the stream so it can support all the same behavior as the original stream: async iteration,
chunked reads, etc.
"""

def __init__(self, body: bytes):
protocol = Mock(_reading_paused=False)
super().__init__(protocol, limit=len(body), loop=asyncio.get_event_loop())
self.feed_data(body)
self.feed_eof()


AnyResponse = Union[ClientResponse, CachedResponse]


Expand Down

0 comments on commit e3148c1

Please sign in to comment.