Skip to content

Latest commit

 

History

History
648 lines (497 loc) · 19.5 KB

guide.md

File metadata and controls

648 lines (497 loc) · 19.5 KB

eggfetch Python API Guide

eggfetch is a fast, modern HTTP client for Python powered by a Rust core. It supports sync and async APIs, streaming responses, HTTP/2, HTTP/3, cookies, proxies, retries, and multipart uploads.

Installation

pip install eggfetch

Requires Python 3.10 through 3.15. Wheels are interpreter-specific; ABI3 is not enabled or claimed.

Free-threaded CPython (PEP 703) is supported for Python 3.14 and 3.15. The release matrix includes cp314-cp314t and cp315-cp315t wheels alongside the GIL-enabled ones, and importing eggfetch leaves the interpreter's GIL disabled. On a free-threaded interpreter the binding does not serialize the Python callbacks it invokes: a trace callback or a request-body iterator supplied by the caller may be entered by two requests at the same time, so state shared between them must be synchronized by the caller.

A free-threaded interpreter also stops serializing the Python work your application does between responses. With eight concurrent callers and a realistic per-response workload, the free-threaded build sustained 2.5x-3.8x the throughput of the GIL-enabled build, which plateaus at about two callers because it still serializes that work. The measurement, its method and its limits are in the free-threading performance evidence.

The native package ships a PEP 561 py.typed marker and reviewed stubs for the public eggfetch surface. Client accepts lazy synchronous iterable request bodies; AsyncClient also accepts lazy asynchronous iterables of bytes or str and consumes them with transport backpressure.

eggfetch is the supported native import surface. Its explicit eggfetch.__all__ list is the release contract and includes the documented exception hierarchy, including decompression and HTTP/3 errors, plus the concrete NetworkStream and AsyncNetworkStream classes returned for 101 upgrade responses. eggfetch._native is a private implementation module; do not import it or rely on names registered there. The runtime package version is derived from the native release metadata and wheel validation compares it with importlib.metadata.version("eggfetch").

Typing

The native package is PEP 561 typed. The reviewed __init__.pyi and _native.pyi files describe the supported eggfetch.__all__ surface, and the repository qualifies them with mypy. The versioned compatibility facades (eggfetch.compat.httpx and eggfetch.compat.httpx2) have concise public entry-point stubs; their underscore-prefixed implementation modules are not a typed API promise. Native and facade types should not be mixed interchangeably.

Request-body typing follows runtime behavior: Client and synchronous helper functions accept buffered bytes-like/text content or a synchronous iterable; AsyncClient additionally accepts an AsyncIterable[bytes | str]. An async-only iterable passed to a synchronous API is rejected statically by the reviewed stubs and at runtime before dispatch. JSON input and JSON response values remain broadly typed because they follow Python JSON serialization.

The reviewed native contract also types Client.is_closed and Client.cookies, plus the corresponding AsyncClient members and its close()/aclose() lifecycle methods. A sync NetworkStream.start_tls() returns a new NetworkStream; awaiting async start_tls() returns a new AsyncNetworkStream. Both stream classes expose is_upgraded: bool. verify= accepts bool, a CA path string, ssl.SSLContext, or a concrete list[bytes] of DER certificates. Other sequence shapes are not widened into the typing contract when the runtime rejects them.

Top-Level Functions

The quickest way to make requests. Each function creates a short-lived client internally.

import eggfetch

# GET
response = eggfetch.get("https://example.com")

# POST with JSON
response = eggfetch.post(
    "https://api.example.com/data",
    json={"key": "value"},
)

# PUT, PATCH, DELETE, HEAD, OPTIONS
response = eggfetch.put("https://api.example.com/resource", content=b"data")
response = eggfetch.patch("https://api.example.com/resource", json={"update": True})
response = eggfetch.delete("https://api.example.com/resource/1")
response = eggfetch.head("https://example.com")
response = eggfetch.options("https://example.com")

# Arbitrary method
response = eggfetch.request("PURGE", "https://example.com")

All top-level functions accept keyword arguments for headers, params, timeout, auth, cookies, proxy, verify, cert, retries, follow_redirects, max_redirects, and decompress.

Client Class (Sync)

A reusable client with connection pooling and shared configuration.

import eggfetch

client = eggfetch.Client(
    headers={"User-Agent": "my-app/1.0"},
    timeout=30,
    follow_redirects=True,
    max_redirects=10,
    auth=eggfetch.BearerAuth("my-token"),
    limits=eggfetch.Limits(max_connections=100, max_keepalive_connections=20),
)

response = client.get("https://api.example.com/data")
response = client.post("https://api.example.com/data", json={"key": "value"})

client.close()

The sync client releases the GIL during network I/O so other Python threads can run while a request is in progress.

Synchronous clients and top-level synchronous helpers accept lazy synchronous iterables for content=. They reject an async-only iterable immediately with TypeError, before opening a network request.

AsyncClient Class (asyncio)

An async client for use with asyncio. Returns coroutines instead of blocking.

import asyncio
import eggfetch

async def main():
    async with eggfetch.AsyncClient(
        timeout=30,
        follow_redirects=True,
    ) as client:
        response = await client.get("https://example.com")
        print(response.status_code)

        # Concurrent requests
        responses = await asyncio.gather(
            client.get("https://api.example.com/a"),
            client.get("https://api.example.com/b"),
        )

asyncio.run(main())

AsyncClient additionally accepts a lazy AsyncIterable[bytes | str] body. The iterator is advanced on transport demand, so request preparation does not buffer the complete body:

async def chunks():
    yield b"part 1"
    yield "part 2"

async with eggfetch.AsyncClient() as client:
    response = await client.post("https://example.com/upload", content=chunks())

Async producer exceptions and invalid chunk types are reported as BodyError. Cancelling the request cancels a pending __anext__() operation and leaves the client usable for later requests.

Response Objects (Buffered)

Top-level functions and the Client return buffered Response objects with all data loaded in memory.

response = eggfetch.get("https://example.com")

# Status and metadata
print(response.status_code)    # 200
print(response.reason_phrase)  # "OK"
print(response.http_version)   # "HTTP/2"
print(response.url)            # "https://example.com/"
print(response.encoding)       # "utf-8" (from Content-Type charset)

# Headers (case-insensitive dict-like)
print(response.headers["content-type"])

# Body
print(response.text)           # decoded string
print(response.content)        # raw bytes

# Cookies set by the server
for cookie in response.cookies:
    print(f"{cookie.name}={cookie.value}")

# Redirect history
for entry in response.history:
    print(f"  {entry.status_code} -> {entry.url}")

StreamingResponse Objects

Use client.stream() for streaming responses without buffering the full body.

async with eggfetch.AsyncClient() as client:
    response = await client.stream("GET", "https://example.com/large")
    # Iterate over byte chunks
    async for chunk in response.aiter_bytes():
        process(chunk)

    # Or iterate over lines
    async for line in response.aiter_text():
        print(line)

Streaming responses also support a buffered fallback:

async with eggfetch.AsyncClient() as client:
    response = await client.stream("GET", "https://example.com/data")
    # Buffer the entire body into text (sync call; releases the GIL)
    text = response.text()

The sync client supports streaming too:

with eggfetch.Client() as client:
    with client.stream("GET", "https://example.com/large") as response:
        for chunk in response.iter_bytes():
            process(chunk)

Headers

Headers are case-insensitive and returned as Headers objects on responses.

response = eggfetch.get("https://example.com")

# Access by name
ct = response.headers["content-type"]

# Check presence
if "x-request-id" in response.headers:
    print(response.headers["x-request-id"])

# Iterate
for name, value in response.headers.items():
    print(f"{name}: {value}")

Timeouts

Accept a float (seconds) or a Timeout object for fine-grained control.

import eggfetch

# Scalar timeout: applies to pool, connect, write, and read phases
response = eggfetch.get("https://example.com", timeout=5.0)

# Timeout object (scalar)
timeout = eggfetch.Timeout(10.0)
response = eggfetch.get("https://example.com", timeout=timeout)

# Per-phase timeout (HTTPX-compatible)
timeout = eggfetch.Timeout(pool=2.0, connect=5.0, read=30.0, total=60.0)
response = eggfetch.get("https://example.com", timeout=timeout)

Auth

BasicAuth

auth = eggfetch.BasicAuth("username", "password")
response = eggfetch.get("https://api.example.com", auth=auth)

BearerAuth

auth = eggfetch.BearerAuth("my-token")
response = eggfetch.get("https://api.example.com", auth=auth)

Tuple shorthand

Pass a (username, password) tuple directly:

response = eggfetch.get("https://api.example.com", auth=("user", "pass"))

Disable auth for one request

client = eggfetch.Client(auth=eggfetch.BearerAuth("token"))

# This request skips the client-level auth
response = client.get("https://public.example.com", auth=eggfetch.NOAUTH)

Cookies

Client-level cookies

client = eggfetch.Client(cookies={"session": "abc123"})
response = client.get("https://example.com")

Per-request cookies

response = eggfetch.get("https://example.com", cookies={"lang": "en"})

Inspecting response cookies

response = eggfetch.get("https://example.com")
for cookie in response.cookies:
    print(f"{cookie.name}={cookie.value} (domain={cookie.domain})")
    print(f"  secure={cookie.is_secure}, http_only={cookie.is_http_only}")
    print(f"  same_site={cookie.same_site}")
    print(f"  expires={cookie.expires}")

Cookies class

The Cookies class provides dict-like access:

cookies = response.cookies
print(cookies["session"])        # by name
print("session" in cookies)      # membership test

Proxy Configuration

# Set proxy per request
response = eggfetch.get(
    "https://example.com",
    proxy="http://proxy:8080",
)

# Set proxy on client
client = eggfetch.Client(proxy="http://proxy:8080")
response = client.get("https://example.com")

# Disable proxy for one request
response = client.get("https://internal.example.com", proxy=False)

eggfetch reads HTTP_PROXY, HTTPS_PROXY, ALL_PROXY, and NO_PROXY environment variables when trust_env=True (the default). Set trust_env=False to disable environment variable discovery. (The Rust core and CLI are explicit-only: Rust takes Proxy configuration in code, the CLI takes --proxy/EGGFETCH_PROXY.)

TLS/SSL

Disable certificate verification

response = eggfetch.get("https://self-signed.example.com", verify=False)

Custom CA bundle

response = eggfetch.get("https://example.com", verify="/path/to/ca-bundle.pem")

Client certificate (mTLS)

# Combined PEM file
response = eggfetch.get("https://example.com", cert="/path/to/combined.pem")

# Separate cert and key
response = eggfetch.get("https://example.com", cert=("/path/to/cert.pem", "/path/to/key.pem"))

Retry

import eggfetch

# Simple retry (3 attempts, default backoff)
response = eggfetch.get("https://api.example.com", retries=True)

# Custom retry policy
retry = eggfetch.Retry(
    max_attempts=5,
    backoff_factor=0.3,
    max_delay=60.0,
    initial_delay=1.0,
    statuses={429, 502, 503, 504},
    respect_retry_after=True,
    max_elapsed=300.0,
    allow_post=True,
)
response = eggfetch.get("https://api.example.com", retries=retry)

# Disable retries for one request
response = eggfetch.get("https://api.example.com", retries=False)

Multipart Uploads

Use the files= keyword with file paths or the File class.

File paths

response = eggfetch.post(
    "https://upload.example.com",
    files={"document": "/path/to/report.pdf"},
)

File with metadata

f = eggfetch.File("/path/to/photo.jpg", filename="avatar.jpg", content_type="image/jpeg")
response = eggfetch.post("https://upload.example.com", files={"photo": f})

Multiple files with form data

response = eggfetch.post(
    "https://upload.example.com",
    data={"description": "My photo"},
    files={
        "photo1": "/path/to/photo1.jpg",
        "photo2": eggfetch.File("/path/to/photo2.jpg", filename="second.jpg"),
    },
)

Resource Limits

Control connection pool concurrency and keep-alive behavior:

# HTTPX-compatible defaults
limits = eggfetch.Limits(
    max_connections=100,
    max_keepalive_connections=20,
    keepalive_expiry=5.0,
)

client = eggfetch.Client(limits=limits)

# Unlimited (native default)
client = eggfetch.Client()  # no limits configured
Parameter Description HTTPX default
max_connections Maximum concurrent in-flight requests (logical concurrency, not physical TCP connections; H2/H3 multiplex many requests over one connection) 100
max_keepalive_connections Maximum idle keep-alive connections 20
keepalive_expiry Seconds before idle connections close 5.0
max_connections_per_host Maximum in-flight requests per host (logical, not physical connections) None

These bound logical in-flight request concurrency (one permit per request). The native Rust API prefers the max_in_flight_requests* names (max_connections* are pre-1.0 aliases where the new name wins); the Python facade keeps the max_connections naming for HTTPX compatibility.

Environment Trust

By default, eggfetch reads proxy environment variables (HTTP_PROXY, HTTPS_PROXY, ALL_PROXY, NO_PROXY). Disable this with trust_env=False:

# Trust environment variables (default)
client = eggfetch.Client(trust_env=True)

# Ignore all environment proxy settings
client = eggfetch.Client(trust_env=False)

HTTP/2 and HTTP/3

Enable HTTP/2 or HTTP/3 on the client:

# HTTP/2 (auto-negotiate; falls back to HTTP/1.1 if the server does not
# support h2)
client = eggfetch.Client(http2=True)
response = client.get("https://example.com")

# HTTP/2 only. Refuses to fall back to HTTP/1.1 and fails with a
# ConnectError / RequestError if the server does not negotiate h2.
# Over cleartext TCP, sends the H2 client preface directly (h2c prior
# knowledge) when the server accepts it.
client = eggfetch.Client(http1=False, http2=True)
response = client.get("https://example.com")

# HTTP/3 (QUIC, experimental)
client = eggfetch.Client(http3=True)
response = client.get("https://example.com")

HTTP/3 remains experimental; the graduation gate and named blockers live in docs/architecture/core-tls-proxy-protocols.md (§ "Production Graduation Decision"). The native http3=True switch selects discovery mode (Auto { allow_http3: true }): H1/H2 unless a fresh authenticated Alt-Svc h3 entry is cached for the origin. Strict H3-only, proxy precedence (H3 never bypasses proxy rules), pre-commit replayable-only fallback, per-origin suppression/backoff, GOAWAY draining, and QUIC idle/stream-limit mapping all follow the core policy documented there. Transport events are observable via Client::transport_metrics() on the Rust side; per-response H3 metadata stays None.

The stream_id metadata field exposed by HTTPX is intentionally absent for H2 responses. See docs/residual-differences.md for the classification of HTTPX gaps and the differential tests that pin each behavior.

Versioned compatibility facades are separate contracts: eggfetch.compat.httpx targets HTTPX 0.28.1 (Stage C) and eggfetch.compat.httpx2 targets httpx2 2.12.0 (Stage C); HTTPX 1.0 pre-releases are preview-only with no parity claim. Current SHAs and evidence live in compat/httpx/0.28.1/profile.toml, compat/httpx2/2.12.0/profile.toml, and plans/httpx-parity-correction-status.md.

For SDKs that require a genuine httpx.Client while using EggFetch for network I/O, see the optional HTTPX transport integration.

Error Handling

eggfetch provides a rich exception hierarchy:

import eggfetch

try:
    response = eggfetch.get("https://example.com", timeout=5)
except eggfetch.TimeoutException:
    print("Request timed out")
except eggfetch.NetworkError:
    print("Connection failed")
except eggfetch.TooManyRedirects:
    print("Too many redirects")
except eggfetch.EggfetchError as e:
    print(f"Request error: {e}")

Exception hierarchy

EggfetchError
  +-- RequestError
  |     +-- InvalidUrl
  |     +-- TimeoutException
  |     |     +-- PoolTimeout
  |     |     +-- ConnectTimeout
  |     |     +-- ReadTimeout
  |     |     +-- WriteTimeout
  |     +-- NetworkError
  |     +-- ProtocolError
  |     +-- BodyError
  |     +-- TooManyRedirects
  |     +-- DecompressionError
  |     +-- UnsupportedContentEncoding
  |     +-- ProxyError
  |     |     +-- ProxyConnectError
  |     |     +-- ProxyAuthError
  |     +-- BodyNotReplayableForRetry
  |     +-- RetryBudgetExhausted
  |     +-- RetryNotConfigured
  |     +-- Http2Error
  |     |     +-- Http2GoAway
  |     |     +-- Http2StreamReset
  |     |     +-- Http2FlowControlError
  |     +-- H3Error
  |           +-- H3ConnectError
  |           +-- H3ProtocolError
  +-- HTTPStatusError
  +-- UnsupportedKwarg
  +-- StreamConsumed
  +-- StreamClosed
  +-- ResponseNotRead

Context Managers

Both Client and AsyncClient support context managers for automatic cleanup.

# Sync
with eggfetch.Client() as client:
    response = client.get("https://example.com")

# Async
async with eggfetch.AsyncClient() as client:
    response = await client.get("https://example.com")

Streaming responses also support context managers:

async with eggfetch.AsyncClient() as client:
    response = await client.stream("GET", "https://example.com")
    async for chunk in response.aiter_bytes():
        process(chunk)

Full Example

import eggfetch

# Simple GET
response = eggfetch.get("https://httpbin.org/get", timeout=10)
print(response.status_code, response.text)

# POST with JSON and auth
response = eggfetch.post(
    "https://httpbin.org/post",
    json={"hello": "world"},
    auth=eggfetch.BearerAuth("my-token"),
    headers={"X-Custom": "value"},
)
print(response.json())

# Reusable client with connection pooling
with eggfetch.Client(
    headers={"User-Agent": "my-app/1.0"},
    timeout=30,
    follow_redirects=True,
    auth=eggfetch.BearerAuth("api-key"),
    retries=eggfetch.Retry(max_attempts=3),
) as client:
    # Multiple requests reuse the connection pool
    r1 = client.get("https://api.example.com/users")
    r2 = client.get("https://api.example.com/posts")
    print(r1.json(), r2.json())

# Streaming download
with eggfetch.Client() as client:
    with client.stream("GET", "https://example.com/large-file") as resp:
        with open("download.bin", "wb") as f:
            for chunk in resp.iter_bytes():
                f.write(chunk)