Skip to main content

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)

Metadata

Release files for lowhaio-redirect 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lowhaio-redirect 0.0.1
File Size Uploaded
lowhaio_redirect-0.0.1.tar.gz 3.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lowhaio-redirect 0.0.1
File Interpreter ABI Platform
lowhaio_redirect-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.7 kB

Release files / lowhaio_redirect-0.0.1.tar.gz

Download URL lowhaio_redirect-0.0.1.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
83904c220d4467c7b105b46387cd72931c8f6d4487d13730e4a1b6cfe1ed345b
BLAKE2b-256 checksum
How to use checksums
2fc6a46234e87f913ad3cd76ec5f8e5396df7b51f167cfa918ad19026a7f5bb3
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / lowhaio_redirect-0.0.1-py3-none-any.whl

Download URL lowhaio_redirect-0.0.1-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f9628e5b91e4bdb7510cdf806accd6bafd49413a213c754290116af2e88f2ca1
BLAKE2b-256 checksum
How to use checksums
4772e7de9f7f5ffc4b0f50e9cb3b44f14179af0498e8858d8a6f39e47ba062a2
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page