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: tuple[type[Exception], ...] = (<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.

Return type:

None

property closed: bool#
property connection: urllib3.connection.HTTPConnection | None#
property data: bytes#
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.

Return type:

None

fileno()#

Returns underlying file descriptor if one exists.

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

Return type:

int

flush()#

Flush write buffers, if applicable.

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

Return type:

None

classmethod from_cached_response(response)[source]#

Create a CachedHTTPResponse based on a cached response

Parameters:

response (CachedResponse) –

classmethod from_response(response)[source]#

Create a CachedHTTPResponse based on an original response, and restore the response to its previous state.

Parameters:

response (Response) –

Return type:

CachedHTTPResponse

get_redirect_location()#

Should we redirect and where to?

Return type:

str | None | Literal[False]

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)#
Parameters:
Return type:

Optional[str]

getheaders()#
Return type:

HTTPHeaderDict

geturl()#
Return type:

Optional[str]

headers: HTTPHeaderDict#
info()#
Return type:

HTTPHeaderDict

isatty()#

Return whether this is an ‘interactive’ stream.

Return False if it can’t be determined.

isclosed()#
Return type:

bool

json()#

Parses the body of the HTTP response as JSON.

To use a custom JSON decoder pass the result of HTTPResponse.data to the decoder.

This method can raise either UnicodeDecodeError or json.JSONDecodeError.

Read more here.

Return type:

Any

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

Simplified reader for cached content that emulates urllib3.response.HTTPResponse.read(), but does not need to read from a socket or decode content.

read_chunked(amt=None, decode_content=None)#

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

Parameters:
  • amt (Optional[int]) – 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 (Optional[bool]) – If True, will attempt to decode the body based on the ‘content-encoding’ header.

Return type:

Generator[bytes, None, None]

readable()#

Return whether object was opened for reading.

If False, read() will raise OSError.

Return type:

bool

readinto(b)#
Parameters:

b (bytearray) –

Return type:

int

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]) –

property retries: urllib3.util.retry.Retry | None#
seek(offset, whence=0, /)#

Change the stream position to the given byte offset.

offset

The stream position, relative to ‘whence’.

whence

The relative position to seek from.

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

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

  • os.SEEK_CUR or 1 – current stream position; offset may be negative

  • os.SEEK_END or 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().

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

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

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().

Return type:

bool

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).

Return type:

int

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.

property url: str | None#

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.

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.