Skip to main content

Lightweight sync/async HTTP client with pooling built on h11.

Project description

fasthttp

A small, fast, and extensible HTTP/1.1 client library with both synchronous and asynchronous APIs, connection pooling, streaming support, retry/backoff, and automatic decoding of compressed responses.


Installation 🚀

# For development
pip install -e .[dev]

# Normal install (when published):
# pip install pyfasthttp

Note: Brotli (br) support is optional and available via the brotli package (included in the dev extras). Install with pip install -e .[dev] or pip install brotli.


Quick Examples

Synchronous usage

from fasthttp import Client
from fasthttp.timeouts import Timeout

with Client(timeout=Timeout(connect=5, read=5)) as client:
    resp = client.get("https://httpbin.org/get")
    print(resp.status_code)
    print(resp.json())  # Decodes according to Content-Encoding and charset

Asynchronous usage

import asyncio
from fasthttp import AsyncClient
from fasthttp.timeouts import Timeout

async def main():
    async with AsyncClient(timeout=Timeout(connect=5, read=5)) as client:
        resp = await client.get("https://httpbin.org/get")
        print(resp.status_code)
        async for chunk in resp.aiter_bytes():
            print(chunk)

asyncio.run(main())

Key Features ✨

  • Sync & Async: Parallel APIs for synchronous and asynchronous usage.
  • Connection pooling to improve throughput and reduce connection churn.
  • Streaming support for reading chunked responses.
  • Automatic compression support: gzip always, optional brotli (br) when installed.
  • Reliable JSON decoding via Response.json() which handles decompression and charset decoding.
  • Retry & Backoff policies for transient failures.
  • Circuit Breaker (optional) to fail fast for hosts exhibiting repeated failures.

Circuit Breaker 🔧

Enable the circuit breaker by configuring RetryPolicy:

from fasthttp import Client
from fasthttp.retry import RetryPolicy

rp = RetryPolicy(max_attempts=1, circuit_breaker=True, cb_failure_threshold=3, cb_recovery_seconds=60)
with Client(base_url="https://api.example.com", retry=rp) as client:
    resp = client.get("/resource")

Parameters:

  • cb_failure_threshold: number of consecutive failures required to open the circuit.
  • cb_recovery_seconds: seconds to wait before attempting requests again after the circuit is open.

JSON decoding & charset behavior 🧾

Response.json() does the following:

  • First, it decodes the response body according to Content-Encoding (gzip, br).
  • Then it decodes text according to the charset from the Content-Type header (default: utf-8).
  • If the body is empty or not valid JSON, a json.decoder.JSONDecodeError is raised.

Development & Testing 🧪

# Install development dependencies
pip install -e .[dev]

# Run tests
python -m pytest -q

The repository includes a GitHub Actions workflow that runs tests on multiple Python versions and also runs ruff and mypy checks.


Contributing 🤝

Please open an issue for bugs or feature requests, or send a pull request with tests and documentation updates. See CONTRIBUTING.md for guidelines.


License

This project is suitable for release under the MIT License (add a LICENSE file if you want to publish it).


Changelog (summary)

  • Added optional Circuit Breaker and tests.
  • Improved Response.json() to correctly decode compressed and charset-encoded payloads.
  • Added async test coverage to ensure parity with the sync Client.
  • Optional brotli (br) support when brotli package is installed.

If you'd like, I can also:

  • Convert the bundled sync/async tests into pytest-style files under tests/ (with pytest-asyncio), or
  • Add a CI matrix job that installs brotli and validates br support.

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

pyfasthttp-0.1.0.tar.gz (54.0 kB view details)

Uploaded Source

Built Distribution

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

pyfasthttp-0.1.0-py3-none-any.whl (43.6 kB view details)

Uploaded Python 3

File details

Details for the file pyfasthttp-0.1.0.tar.gz.

File metadata

  • Download URL: pyfasthttp-0.1.0.tar.gz
  • Upload date:
  • Size: 54.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pyfasthttp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d92ed167f54b8e0af8b556653eb80887620826386e8c4a42b7e933b588e3f603
MD5 4bbff7f353160527f64b77dee6f4556d
BLAKE2b-256 ed1d93f3978a8b57d14f0549ca34cbf1c3d74bfb1eb25f3e33ada8c6c9dd4063

See more details on using hashes here.

File details

Details for the file pyfasthttp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyfasthttp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 43.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pyfasthttp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cecb80423111f21cb1ff5469b34d1ddc99aad0e7b559c7e8dcf8b805f9e22a8d
MD5 582acc49884088c01ae7d867f86d3446
BLAKE2b-256 f6681e28f9b44ad67b1f3c8860c4781dccdbb714669089b39273c995619ae77c

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