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

kw.platform.requests.monkey.patch_with_sentry(sunset_header=True, deprecated_usage_header=True)[source]

Patch requests.Session.request() to create events in Sentry.

If the HTTP response contains, for example, the Sunset HTTP header, an event is sent to Sentry containing details about the sunset.

Parameters
  • sunset_header – (optional) Whether to report the presence of the Sunset header, True by default.

  • deprecated_usage_header – (optional) Whether to report the presence of the Deprecated-Usage header, True by default.

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(*args, **kwargs)[source]

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