Skip to main content

Lowhaio wrapper that follows HTTP redirects

Project description

lowhaio-redirect CircleCI

Wrapper of lowhaio that follows HTTP redirects

Installation

pip install lowhaio lowhaio_redirect

Usage

The request function returned from lowhaio.Pool must be wrapped with lowhaio_redirect.redirectable, as in the below example.

import os
from lowhaio import Pool
from lowhaio_redirect import redirectable

request, _ = Pool()

redirectable_request = redirectable(request)
code, headers, body = await redirectable_request(b'GET', 'https://example.com/path')

async for chunk in body:
    print(chunk)

Method and body changing

The default behavior is as below.

  • 301, 302 redirects from a POST are converted to GETs with empty bodies; all other requests do not change methods and bodies.

  • 303 redirects, all non HEAD and GET requests are converted to GETs with empty bodies.

  • 307 and 308 redirects, the method and bodies are always unchanged.

Note however, that an unchanged body is actually not guaranteed by this wrapper. For each request the function passed as the body argument is called, and it may return different things on each call. It is up the the developer to handle this case as needed: there is no one-size-fits all approach, since for streaming requests, the body may not be available again. In many cases, a redirect that expects a resubmission of a large upload may be an error; or if an API is never expected to return a redirect, not using this wrapper at all may be a viable option, and instead a few lines of code that raises an exception if a redirect is returned from the server.

Customise redirects

It is possible to customise which redirects are followed, and how they affect the method and body. As an example, to recreate the default behaviour explicitly, the below code could be used.

def get(method, body, body_args, body_kwargs, headers):
    # Asynchronous generator that end immediately, and results in an empty body
    async def empty_body():
        while False:
            yield
    next_request_headers = tuple(
        (key, value)
        for key, value in headers if key.lower() not in (b'content-length', b'transfer-encoding')
    )

    return (b'GET', empty_body, (), (), next_request_headers)


def unchanged(method, body, body_args, body_kwargs, headers):
    return (method, body, body_args, body_kwargs, headers)


redirectable_request = redirectable(request, redirects=(
    (b'301', lambda method: unchanged if method != b'POST' else get),
    (b'302', lambda method: unchanged if method != b'POST' else get),
    (b'303', lambda method: unchanged if method in (b'GET', b'HEAD') else get),
    (b'307', lambda method: unchanged),
    (b'308', lambda method: unchanged),
))

Authorization header

By default, the Authorization header is not passed to if redirected to another domain. This can be customised. As an example, to recreate the default behaviour explicitly, the below code can be used.

def strip_authorization_if_different_host(request_headers, request_host, redirect_host):
    forbidden = \
        (b'authorization',) if request_host != redirect_host else \
        ()
    return tuple(
        (key, value)
        for key, value in request_headers if key.lower() not in forbidden
    )

redirectable_request = redirectable(request, transform_headers=strip_authorization_if_different_host)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lowhaio_redirect-0.0.1.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lowhaio_redirect-0.0.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file lowhaio_redirect-0.0.1.tar.gz.

File metadata

  • Download URL: lowhaio_redirect-0.0.1.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for lowhaio_redirect-0.0.1.tar.gz
Algorithm Hash digest
SHA256 83904c220d4467c7b105b46387cd72931c8f6d4487d13730e4a1b6cfe1ed345b
MD5 bc99273869f9083a5c5cba3f687659d7
BLAKE2b-256 2fc6a46234e87f913ad3cd76ec5f8e5396df7b51f167cfa918ad19026a7f5bb3

See more details on using hashes here.

File details

Details for the file lowhaio_redirect-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: lowhaio_redirect-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for lowhaio_redirect-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9628e5b91e4bdb7510cdf806accd6bafd49413a213c754290116af2e88f2ca1
MD5 9cb9230d988e9f458c33d5402a612b1f
BLAKE2b-256 4772e7de9f7f5ffc4b0f50e9cb3b44f14179af0498e8858d8a6f39e47ba062a2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page