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
- CONTENT_DECODERS = ['gzip', 'x-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:
- property connection: urllib3.connection.HTTPConnection | None#
- 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:
- fileno()#
Returns underlying file descriptor if one exists.
OSError is raised if the IO object does not use a file descriptor.
- Return type:
- flush()#
Flush write buffers, if applicable.
This is not implemented for read-only and non-blocking streams.
- Return type:
- 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:
- get_redirect_location()#
Should we redirect and where to?
- getheader(name, default=None)#
- getheaders()#
- Return type:
HTTPHeaderDict
-
headers:
HTTPHeaderDict
#
- info()#
- Return type:
HTTPHeaderDict
- isatty()#
Return whether this is an ‘interactive’ stream.
Return False if it can’t be determined.
- json()#
Deserializes the body of the HTTP response as a Python object.
The body of the HTTP response must be encoded using UTF-8, as per RFC 8529 Section 8.1.
To use a custom JSON decoder pass the result of
HTTPResponse.data
to your custom decoder instead.If the body of the HTTP response is not decodable to UTF-8, a UnicodeDecodeError will be raised. If the body of the HTTP response is not a valid JSON document, a json.JSONDecodeError will be raised.
Read more here.
- Return type:
- Returns:
The body of the HTTP response as a Python object.
- 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.
- read1(amt=None, decode_content=None)#
Similar to
http.client.HTTPResponse.read1
and documented inio.BufferedReader.read1()
, but with an additional parameter:decode_content
.
- read_chunked(amt=None, decode_content=None)#
Similar to
HTTPResponse.read()
, but with an additional parameter:decode_content
.- Parameters:
- Return type:
- readable()#
Return whether object was opened for reading.
If False, read() will raise OSError.
- Return type:
- 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.
- 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().
- 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:
- 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:
- 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.
- 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.