Requests

Extensions and helpers patching kiwi code standards for requests library.

Monkey Patching

kw.platform.requests.monkey.patch()[source]

Apply all patches for requests module.

This will automatically apply:

  • kw.platform.requests.patch.patch_with_user_agent()

kw.platform.requests.monkey.patch_with_user_agent(user_agent=None)[source]

Patch requests.Session.request() with User-Agent.

In case User-Agent header has not been provided directly to request. Add User-Agent string constructed by kw.platform.requests.patch.construct_user_agent() as User-Agent header.

Parameters

user_agent – (optional) User-Agent string that will be used as User-Agent header.

Session

class kw.platform.requests.session.KiwiSession[source]

Custom requests.Session with all patches applied.

Usage:

from kw.platform.requests import KiwiSession
session = KiwiSession()
session.get('https://kiwi.com')
request(method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None)

Constructs a Request, prepares it and sends it. Returns Response object.

Parameters
  • method – method for the new Request object.

  • url – URL for the new Request object.

  • params – (optional) Dictionary or bytes to be sent in the query string for the Request.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • json – (optional) json to send in the body of the Request.

  • headers – (optional) Dictionary of HTTP Headers to send with the Request.

  • cookies – (optional) Dict or CookieJar object to send with the Request.

  • files – (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.

  • auth – (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth.

  • timeout (float or tuple) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.

  • allow_redirects (bool) – (optional) Set to True by default.

  • proxies – (optional) Dictionary mapping protocol or protocol and hostname to the URL of the proxy.

  • stream – (optional) whether to immediately download the response content. Defaults to False.

  • verify – (optional) Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to True.

  • cert – (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.

Return type

requests.Response