HTTP delegate base class with retry support
Project description
csrd-delegate
HTTP client delegate base class with retry support for FastAPI microservices.
Package: csrd.delegate · Import: from csrd.delegate import BaseDelegate
What's included
BaseDelegate— async HTTP client with header forwarding, retry via tenacity, and lifecycle (close()/async with)- Response parsing via
csrd.models.model_parser - Configurable retry profiles (
conservative,aggressive,resilient) - httpx-specific response types (
ResponseHandler,ResponseHandlerMap)
Installation
uv pip install "csrd-delegate @ git+ssh://git@github.com/csrd-api/fastapi-common.git#subdirectory=packages/delegate"
Dependencies
csrd-models,csrd-context(Tier 2)
Consumer Feature Matrix (BaseDelegate)
| Capability area | Common expectation | Actual support | How to use / notes |
|---|---|---|---|
| HTTP methods | Standard verb helpers exist | ✅ Built-in | get, post, put, patch, delete, head, options |
| Retry support | Automatic retries for transient failures | ✅ Built-in + configurable | Set default in constructor (retry_profile / retry_*) or per call |
| Retry presets | Named retry policies | ✅ Built-in | conservative, aggressive, resilient |
| Auth forwarding | Bearer token from incoming request is forwarded | ✅ Built-in | Uses request context headers; can be filtered explicitly |
| Header filtering | Hop-by-hop headers are removed safely | ✅ Built-in | RFC-hop headers filtered by default; body requests additionally filter content-length |
| Response parsing | Auto-parse JSON to typed model | ✅ Built-in + configurable | Use response_model and/or model_handler |
| Status handling | Non-2xx statuses raise exceptions | ✅ Built-in + overridable | Default raises HTTPException; override specific statuses with response_handlers |
| Per-status custom behavior | Custom handling for 404/409/etc | ✅ Built-in | Pass response_handlers={404: ...} style map |
| Client lifecycle | Safe async with usage and close management |
✅ Built-in | Delegate-owned client closes automatically; injected client is not closed |
| Per-call transport overrides | Override timeout/headers/auth per request | ✅ Built-in | timeout, headers, auth, follow_redirects, etc. on each call |
| Circuit breaker | Open/half-open breaker semantics | ❌ Not built-in | Add externally (middleware/proxy/custom wrapper) |
| Rate limiting | Outbound request throttling | ❌ Not built-in | Add externally (gateway/proxy/custom transport) |
| Service discovery | Dynamic upstream resolution/registry | ❌ Not built-in | Provide concrete service_host at wiring time |
Inter-Service Auth Propagation
In multi-service clusters, delegates automatically forward authentication headers from the inbound request to upstream service calls. This enables transparent claim propagation through delegate chains.
How It Works
- Client → Service A: Client sends JWT via
Authorization: Bearer <token>header - Service A extracts and verifies claims using
VerifiedTokenDep(or similar) - Service A → Service B (via
BaseDelegate): Delegate automatically includesAuthorizationheader from inbound request - Service B receives and verifies same JWT from Service A's outbound call
- Service B → Service C (if needed): Claim propagation continues transitively
Example: Multi-Hop Orchestration
# quote-service orchestrates inventory + pricing calls
class QuoteDelegate(BaseDelegate):
def __init__(self) -> None:
super().__init__(settings.quote_service_url, retry_profile="conservative")
async def compose_quote(self, item_id: str, quantity: int):
# All three calls automatically include Authorization header
inventory_data = await self.get(f"/api/inventory/{item_id}")
pricing_data = await self.get(f"/api/pricing/{item_id}")
return {
"item_id": item_id,
"quantity": quantity,
"stock_available": inventory_data["available_qty"],
"price": pricing_data["base_price"],
}
# On inbound request with JWT:
@router.post("/api/quotes")
async def create_quote(
item_id: str,
quantity: int,
verified: VerifiedTokenDep, # JWT verified here
quote_delegate: QuoteDelegate,
) -> dict:
# QuoteDelegate.compose_quote() calls use verified user's JWT
return await quote_delegate.compose_quote(item_id, quantity)
Header Filtering Behavior
- Hop-by-hop headers (RFC 7230): Automatically filtered (connection, keep-alive, etc.)
- Authorization header: Always forwarded unless explicitly filtered
- Content-Length: Re-computed by httpx for each request
- Custom headers: Passed through by default; filter via
header_filter_listif needed
Pattern Constraints
- No auth bypass: Each hop validates the JWT independently
- Claim consistency: All services see the same
sub,roles, etc. - No credential injection: Delegates don't add credentials; they forward inbound claims only
- No header injection: Custom headers must be passed explicitly; inbound headers only forwarded if present
When to Use Manual Header Control
Override the automatic forwarding only in specific cases:
class InternalDelegate(BaseDelegate):
def __init__(self) -> None:
super().__init__(
settings.internal_service_url,
header_filter_list=["authorization", "x-tenant-id"], # Don't forward these
ignore_incoming_headers=False, # Still use get_headers() for others
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file csrd_delegate-0.3.76.tar.gz.
File metadata
- Download URL: csrd_delegate-0.3.76.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a9c578ce9d86cca7b4cd4ef5f6e27653c1d827460500ae55d4cf880e55074fa
|
|
| MD5 |
5a24f3caf48d6e877fbf6eb63212040e
|
|
| BLAKE2b-256 |
3f8bb809e9ffadccaaadec696d8bbfaeb7594ac145f1143c6ef1c528bdd664f2
|
Provenance
The following attestation bundles were made for csrd_delegate-0.3.76.tar.gz:
Publisher:
release.yml on csrd-api/fastapi-common
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
csrd_delegate-0.3.76.tar.gz -
Subject digest:
4a9c578ce9d86cca7b4cd4ef5f6e27653c1d827460500ae55d4cf880e55074fa - Sigstore transparency entry: 1435980958
- Sigstore integration time:
-
Permalink:
csrd-api/fastapi-common@a02f225a687615cf2dcc46bf3f802bbb6393a239 -
Branch / Tag:
refs/tags/v0.3.76 - Owner: https://github.com/csrd-api
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a02f225a687615cf2dcc46bf3f802bbb6393a239 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file csrd_delegate-0.3.76-py3-none-any.whl.
File metadata
- Download URL: csrd_delegate-0.3.76-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a44796dc38b8725f826823051f39e69f9fd7ddffb5bc2e9f7e1c10acbb91cf39
|
|
| MD5 |
9f8d361918d1377d9627539b2bd09b9a
|
|
| BLAKE2b-256 |
636a3c799a59b480160840dc5cc82e9ab78de39d0ffc74f5e77bbb43f1f92b7b
|
Provenance
The following attestation bundles were made for csrd_delegate-0.3.76-py3-none-any.whl:
Publisher:
release.yml on csrd-api/fastapi-common
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
csrd_delegate-0.3.76-py3-none-any.whl -
Subject digest:
a44796dc38b8725f826823051f39e69f9fd7ddffb5bc2e9f7e1c10acbb91cf39 - Sigstore transparency entry: 1435980999
- Sigstore integration time:
-
Permalink:
csrd-api/fastapi-common@a02f225a687615cf2dcc46bf3f802bbb6393a239 -
Branch / Tag:
refs/tags/v0.3.76 - Owner: https://github.com/csrd-api
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a02f225a687615cf2dcc46bf3f802bbb6393a239 -
Trigger Event:
workflow_dispatch
-
Statement type: