Raw Response#

class requests_cache.models.raw_response.CachedHTTPResponse(body=None, **kwargs)[source]#

Bases: RichMixin, HTTPResponse

A wrapper class that emulates HTTPResponse.

This enables consistent behavior for streaming requests and generator usage in the following cases: * On an original response, after reading its content to write to the cache * On a cached response

Parameters:

body (Optional[bytes]) –

CONTENT_DECODERS = ['gzip', 'deflate']#
DECODER_ERROR_CLASSES = (<class 'OSError'>, <class 'zlib.error'>)#
REDIRECT_STATUSES = [301, 302, 303, 307, 308]#
close()#

Flush and close the IO object.

This method has no effect if the file is already closed.

property closed#
property connection#
property data#
decode_content: bool#
drain_conn()#

Read and discard any remaining HTTP response data in the response connection.

Unread data in the HTTPResponse connection blocks the connection from being released back to the pool.

fileno()#

Returns underlying file descriptor if one exists.

OSError is raised if the IO object does not use a file descriptor.

flush()#

Flush write buffers, if applicable.

This is not implemented for read-only and non-blocking streams.

classmethod from_cached_response(response)[source]#

Create a CachedHTTPResponse based on a cached response

Parameters:

response (CachedResponse) –

classmethod from_httplib(r, **response_kw)#

Given an http.client.HTTPResponse instance r, return a corresponding urllib3.response.HTTPResponse object.

Remaining parameters are passed to the HTTPResponse constructor, along with original_response=r.

classmethod from_response(response)[source]#

Create a CachedHTTPResponse based on an original response

Parameters:

response (Response) –

Return type:

CachedHTTPResponse

get_redirect_location()#

Should we redirect and where to?

Returns:

Truthy redirect location string if we got a redirect status code and valid location. None if redirect status and no location. False if not a redirect status code.

getheader(name, default=None)#
getheaders()#
geturl()#

Returns the URL that was the source of this response. If the request that generated this response redirected, this method will return the final redirect location.

headers: HTTPHeaderDict#
info()#
isatty()#

Return whether this is an ‘interactive’ stream.

Return False if it can’t be determined.

isclosed()#
read(amt=None, decode_content=None, **kwargs)[source]#

Simplified reader for cached content that emulates urllib3.response.HTTPResponse.read()

read_chunked(amt=None, decode_content=None)#

Similar to HTTPResponse.read(), but with an additional parameter: decode_content.

Parameters:
  • amt – How much of the content to read. If specified, caching is skipped because it doesn’t make sense to cache partial content as the full response.

  • decode_content – If True, will attempt to decode the body based on the ‘content-encoding’ header.

readable()#

Return whether object was opened for reading.

If False, read() will raise OSError.

readinto(b)#
readline(size=-1, /)#

Read and return a line from the stream.

If size is specified, at most size bytes will be read.

The line terminator is always b’n’ for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized.

readlines(hint=-1, /)#

Return a list of lines from the stream.

hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.

reason: str#
release_conn()[source]#

No-op for compatibility

request_url: str#
reset(body=None)[source]#

Reset raw response file pointer, and optionally update content

Parameters:

body (Optional[bytes]) –

seek()#

Change stream position.

Change the stream position to the given byte offset. The offset is interpreted relative to the position indicated by whence. Values for whence are:

  • 0 – start of stream (the default); offset should be zero or positive

  • 1 – current stream position; offset may be negative

  • 2 – end of stream; offset is usually negative

Return the new absolute position.

seekable()#

Return whether object supports random access.

If False, seek(), tell() and truncate() will raise OSError. This method may need to do a test seek().

set_content(body)[source]#
Parameters:

body (bytes) –

status: int#
stream(amt=None, **kwargs)[source]#

Simplified generator over cached content that emulates urllib3.response.HTTPResponse.stream()

strict: int#
supports_chunked_reads()#

Checks if the underlying file-like object looks like a http.client.HTTPResponse object. We do this by testing for the fp attribute. If it is present we assume it returns raw chunks as processed by read_chunked().

tell()#

Obtain the number of bytes pulled over the wire so far. May differ from the amount of content returned by :meth:urllib3.response.HTTPResponse.read if bytes are encoded on the wire (e.g, compressed).

truncate()#

Truncate file to size bytes.

File pointer is left unchanged. Size defaults to the current IO position as reported by tell(). Returns the new size.

version: int#
writable()#

Return whether object was opened for writing.

If False, write() will raise OSError.

writelines(lines, /)#

Write a list of lines to stream.

Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.