RequestHandler
Constructor
hentai.RequestHandler(object)
Defines a synchronous request handler class that provides methods and
properties for working with REST APIs.
This implementation uses the built-in request library. See also the
Advanced Request
documentation for more details. Other useful sources are
this bog post
and
this quick start guide,
which is also from the request library.
RequestHandler.__init__(self,
timeout: Tuple[float, float],
total: int,
status_forcelist: List[int],
backoff_factor: int) -> RequestHandler
Instantiates a new request handler object. Refer to the fields section for an explanation of these arguments.
Fields
RequestHandler._timeout -> Tuple[float, float]=(5, 5)
The timeout field defines how many seconds your request should wait to receive a response. The first value of the tuple is issued to the connection timeout, while the second value is assigned to the read timeout.
RequestHandler._total=5 -> int
The total number of retry attempts to make. If the number of failed
requests or redirects exceeds this number the client will throw the
urllib3.exceptions.MaxRetryError
.
RequestHandler._status_forcelist=[413, 429, 500, 502, 503, 504] -> List[int]
The HTTP response codes to retry on. Always retry on 429 because the urllib library should by default incrementally backoff on failed requests.
RequestHandler._backoff_factor=1 -> int
Defines how long the processes will sleep between failed requests.
This value is determined by the sequence
\[ \{a_k\} = 2^{k-1} \cdot b \]
where \(b\) denotes the backoff factor in seconds and \(k\) the
current retry iteration. So, for \(b=1\) and \(n=5\) as the total
number of retries we have a sleep sequence of
\[ \{a_n\}_{n\in[0,5]} = \left\{ \tfrac{1}{2}, 1, 2, 4, 6, 16 \right\} \]
and a maximum duration of
\[ b \sum_{k=0}^{n=5} 2^{k-1} = 31.5 \]
Therefore, it would take about \(31.5\) seconds before a
MaxRetryError
will be thrown or the connection times out.
Properties
self.retry_strategy -> Retry
The retry strategy returns the retry configuration made up of the number of total retries, the status forcelist as well as the backoff factor. It is used in the session property where these values are passed to the HTTPAdapter.
self.session -> Session
Creates a custom session object. A request session provides cookie
persistence, connection-pooling, and further configuration options
that are exposed in the RequestHandler
methods in form
of parameters and keyword arguments.
Methods
self.get(url: str, params: dict=None, **kwargs) -> Response
Returns the GET request encoded in utf-8
. Adds proxies
to this session on the fly if urllib is able to pick up the system's
proxy settings.
from hentai import Hentai, RequestHandler
from urllib.parse import urljoin
metamorphosis = urljoin(Hentai._API, str(177013))
response = RequestHandler().get(url=metamorphosis)
# 177013
print(response.json().get('id'))