Ejemplo n.º 1
0
async def wrap_async(response: ClientResponse) -> Response:
    """Build a ``requests`` response from a ``aiohttp`` response.

    A ``requests.Response`` instance is built to provide synchronous
    access to the original response's data. Note that the returned
    response does not have proper data for :attr:``elapsed`` or
    :attr:``request``.

    The response will be consumed if it has not already.
    """

    # Ensure the response data is read so that the wrapped response
    # does not require any async methods.
    await response.read()

    wrapped = Response()
    wrapped._content = response._body  # type: ignore
    wrapped._content_consumed = True  # type: ignore
    wrapped.status_code = response.status
    wrapped.headers = CaseInsensitiveDict(response.headers)
    wrapped.url = str(response.url)  # `aiohttp` uses a `URL` object.
    wrapped.encoding = response.get_encoding()
    wrapped.history = [await wrap_async(rsp) for rsp in response.history]
    wrapped.reason = response.reason or ""
    wrapped.cookies = cookiejar_from_dict(response.cookies)
    return wrapped
Ejemplo n.º 2
0
def FakeResponse(req, url, status_code, content, headers={}, cookies={}):
    response = req
    if response is None:
        response = Response()

    response.url = url
    response.status_code = status_code
    response._content = content.encode('UTF-8') if content else ''
    response.headers = headers
    response.cookies = cookiejar_from_dict(cookies)

    return response
Ejemplo n.º 3
0
    def fake_response(url: str, status_code: int, content, headers: dict, cookies: dict) -> Response:
        content = content if isinstance(content, bytes) else content.encode('UTF-8')
        cookies = {} if cookies is None else cookies
        headers = {} if headers is None else headers

        response = Response()
        response.url = url
        response.status_code = status_code
        response._content = content
        response.headers = headers
        response.cookies = cookiejar_from_dict(cookies)

        return response