python-wrap 0.2.1__py3-none-any.whl → 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_wrap/__init__.py +348 -1
- python_wrap/__main__.py +57 -0
- python_wrap/_backend.py +133 -0
- python_wrap/_buckets.py +162 -0
- python_wrap/_core.py +12 -3
- python_wrap/_data/agent_guide.md +103 -0
- python_wrap/_data/recipes.json +703 -0
- python_wrap/_fork.py +113 -0
- python_wrap/_integration.py +29 -0
- python_wrap/_runtime.py +157 -0
- python_wrap/adapters/__init__.py +101 -0
- python_wrap/adapters/_frameworks.py +251 -0
- python_wrap/adapters/_sdk.py +1978 -0
- python_wrap/adapters/anthropic.py +139 -0
- python_wrap/adapters/langchain.py +288 -0
- python_wrap/adapters/openai.py +169 -0
- python_wrap/adapters/openai_agents.py +468 -0
- python_wrap/adapters/pydantic_ai.py +459 -0
- python_wrap/adaptive.py +605 -0
- python_wrap/agents.py +2503 -0
- python_wrap/alerts.py +293 -0
- python_wrap/async_cache.py +745 -0
- python_wrap/batching.py +17 -4
- python_wrap/buffering.py +952 -0
- python_wrap/bulkhead.py +58 -6
- python_wrap/caching.py +425 -56
- python_wrap/celery.py +528 -0
- python_wrap/chaos.py +519 -0
- python_wrap/circuit.py +454 -23
- python_wrap/compat.py +267 -0
- python_wrap/concurrency.py +82 -1746
- python_wrap/config.py +25 -4
- python_wrap/coordination.py +638 -0
- python_wrap/credentials.py +126 -0
- python_wrap/dashboard.py +358 -0
- python_wrap/debounce.py +538 -0
- python_wrap/django.py +100 -0
- python_wrap/doctor.py +599 -0
- python_wrap/events.py +145 -4
- python_wrap/exporters.py +663 -0
- python_wrap/fastapi.py +81 -0
- python_wrap/flask.py +61 -0
- python_wrap/genai.py +1101 -0
- python_wrap/health.py +187 -0
- python_wrap/http.py +940 -0
- python_wrap/idempotency.py +446 -0
- python_wrap/introspect.py +738 -0
- python_wrap/lifecycle.py +620 -0
- python_wrap/llm.py +2574 -0
- python_wrap/llm_cassette.py +948 -0
- python_wrap/llm_http_chaos.py +326 -0
- python_wrap/llm_shared.py +772 -0
- python_wrap/llm_testing.py +990 -0
- python_wrap/llm_validate.py +383 -0
- python_wrap/local_state.py +395 -0
- python_wrap/logs.py +82 -26
- python_wrap/mcp.py +490 -0
- python_wrap/mcp_guard.py +357 -0
- python_wrap/metrics.py +6 -3
- python_wrap/migrate.py +1452 -0
- python_wrap/observability.py +484 -0
- python_wrap/periodic.py +24 -2
- python_wrap/policies.py +396 -0
- python_wrap/policy.py +14 -0
- python_wrap/predicates.py +276 -0
- python_wrap/presets.py +378 -0
- python_wrap/processing.py +227 -0
- python_wrap/propagation.py +62 -0
- python_wrap/pytest_plugin.py +85 -0
- python_wrap/quotas.py +319 -0
- python_wrap/ratelimit.py +560 -0
- python_wrap/redaction.py +228 -0
- python_wrap/retry.py +448 -70
- python_wrap/shared.py +653 -0
- python_wrap/singleflight.py +255 -0
- python_wrap/slow.py +72 -0
- python_wrap/stores.py +168 -0
- python_wrap/switches.py +164 -0
- python_wrap/testing.py +34 -8
- python_wrap/timeouts.py +874 -0
- python_wrap/timing.py +239 -17
- python_wrap/validation.py +187 -12
- python_wrap/web.py +723 -0
- {python_wrap-0.2.1.dist-info → python_wrap-0.4.0.dist-info}/METADATA +117 -7
- python_wrap-0.4.0.dist-info/RECORD +95 -0
- python_wrap-0.2.1.dist-info/RECORD +0 -29
- {python_wrap-0.2.1.dist-info → python_wrap-0.4.0.dist-info}/WHEEL +0 -0
- {python_wrap-0.2.1.dist-info → python_wrap-0.4.0.dist-info}/licenses/LICENSE +0 -0
python_wrap/fastapi.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Pure ASGI context middleware for FastAPI/Starlette, with no framework imports."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from python_wrap._integration import request_id, scope
|
|
9
|
+
from python_wrap._runtime import seconds
|
|
10
|
+
from python_wrap.health import health
|
|
11
|
+
from python_wrap.propagation import deadline_budget, header_names
|
|
12
|
+
|
|
13
|
+
__all__ = ["ContextMiddleware", "install"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ContextMiddleware:
|
|
17
|
+
"""Bind context around HTTP dispatch/streaming and restore on errors/cancellation."""
|
|
18
|
+
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
app: Any,
|
|
22
|
+
*,
|
|
23
|
+
user: Callable[[dict[str, Any]], Any] | None = None,
|
|
24
|
+
request_id_header: str = "X-Request-ID",
|
|
25
|
+
budget: float | None = None,
|
|
26
|
+
trust_deadline_header: bool = False,
|
|
27
|
+
) -> None:
|
|
28
|
+
budget = None if budget is None else seconds(budget, positive=False)
|
|
29
|
+
self.app, self.user, self.budget = app, user, budget
|
|
30
|
+
self.trust_deadline = trust_deadline_header
|
|
31
|
+
name = header_names()[0] if request_id_header == "X-Request-ID" else request_id_header
|
|
32
|
+
self.header = name.lower().encode("latin1")
|
|
33
|
+
|
|
34
|
+
async def __call__(self, asgi_scope: dict[str, Any], receive: Any, send: Any) -> None:
|
|
35
|
+
if asgi_scope["type"] != "http":
|
|
36
|
+
await self.app(asgi_scope, receive, send)
|
|
37
|
+
return
|
|
38
|
+
import inspect
|
|
39
|
+
|
|
40
|
+
headers = dict(asgi_scope.get("headers", []))
|
|
41
|
+
raw = headers.get(self.header, b"")
|
|
42
|
+
fields = {"request_id": request_id(raw.decode("latin1"))}
|
|
43
|
+
if self.user:
|
|
44
|
+
value = self.user(asgi_scope)
|
|
45
|
+
fields["user"] = await value if inspect.isawaitable(value) else value
|
|
46
|
+
raw_deadline = headers.get(header_names()[1].lower().encode("latin1"))
|
|
47
|
+
budget = deadline_budget(
|
|
48
|
+
None if raw_deadline is None else raw_deadline.decode("latin1"),
|
|
49
|
+
self.budget,
|
|
50
|
+
trust=self.trust_deadline,
|
|
51
|
+
)
|
|
52
|
+
with scope(fields, budget):
|
|
53
|
+
await self.app(asgi_scope, receive, send)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def install(
|
|
57
|
+
app: Any,
|
|
58
|
+
*,
|
|
59
|
+
user: Callable[[dict[str, Any]], Any] | None = None,
|
|
60
|
+
request_id_header: str = "X-Request-ID",
|
|
61
|
+
budget: float | None = None,
|
|
62
|
+
health_path: str | None = None,
|
|
63
|
+
trust_deadline_header: bool = False,
|
|
64
|
+
) -> None:
|
|
65
|
+
"""Install middleware before app startup; optional health endpoint returns JSON.
|
|
66
|
+
|
|
67
|
+
``trust_deadline_header=True`` lets an ``X-Request-Deadline`` header (remaining
|
|
68
|
+
milliseconds) shorten, never extend, ``budget``.
|
|
69
|
+
"""
|
|
70
|
+
if getattr(app.state, "_python_wrap_installed", False):
|
|
71
|
+
return
|
|
72
|
+
app.add_middleware(
|
|
73
|
+
ContextMiddleware,
|
|
74
|
+
user=user,
|
|
75
|
+
request_id_header=request_id_header,
|
|
76
|
+
budget=budget,
|
|
77
|
+
trust_deadline_header=trust_deadline_header,
|
|
78
|
+
)
|
|
79
|
+
if health_path:
|
|
80
|
+
app.get(health_path)(health.report)
|
|
81
|
+
app.state._python_wrap_installed = True
|
python_wrap/flask.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""Flask request context adapter; Flask is imported only by init_app()."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from python_wrap._integration import request_id, scope
|
|
9
|
+
from python_wrap._runtime import seconds
|
|
10
|
+
from python_wrap.health import health
|
|
11
|
+
from python_wrap.propagation import deadline_budget, header_names
|
|
12
|
+
|
|
13
|
+
__all__ = ["init_app"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def init_app(
|
|
17
|
+
app: Any,
|
|
18
|
+
*,
|
|
19
|
+
user: Callable[[], Any] | None = None,
|
|
20
|
+
request_id_header: str = "X-Request-ID",
|
|
21
|
+
budget: float | None = None,
|
|
22
|
+
health_path: str | None = None,
|
|
23
|
+
trust_deadline_header: bool = False,
|
|
24
|
+
) -> None:
|
|
25
|
+
"""Bind request/user context and an optional cooperative deadline.
|
|
26
|
+
|
|
27
|
+
Health routes are opt-in; apply your framework's authorization policy before
|
|
28
|
+
exposing dependency details. Install ContextFilter on app handlers separately.
|
|
29
|
+
Repeated initialization on the same app is a no-op. With ``trust_deadline_header=True``
|
|
30
|
+
an ``X-Request-Deadline`` header (remaining milliseconds) can shorten, never extend,
|
|
31
|
+
``budget``; enable it only for callers you trust.
|
|
32
|
+
"""
|
|
33
|
+
import importlib
|
|
34
|
+
|
|
35
|
+
flask = importlib.import_module("flask")
|
|
36
|
+
request = flask.request
|
|
37
|
+
|
|
38
|
+
if app.extensions.get("python_wrap"):
|
|
39
|
+
return
|
|
40
|
+
budget = None if budget is None else seconds(budget, positive=False)
|
|
41
|
+
|
|
42
|
+
def before() -> None:
|
|
43
|
+
id_header = header_names()[0] if request_id_header == "X-Request-ID" else request_id_header
|
|
44
|
+
fields: dict[str, Any] = {"request_id": request_id(request.headers.get(id_header))}
|
|
45
|
+
if user:
|
|
46
|
+
fields["user"] = user()
|
|
47
|
+
header = request.headers.get(header_names()[1])
|
|
48
|
+
stack = scope(fields, deadline_budget(header, budget, trust=trust_deadline_header))
|
|
49
|
+
request.environ["python_wrap.scope"] = stack
|
|
50
|
+
|
|
51
|
+
def after(error: Any = None) -> None:
|
|
52
|
+
stack = request.environ.pop("python_wrap.scope", None)
|
|
53
|
+
if stack is not None:
|
|
54
|
+
stack.close()
|
|
55
|
+
|
|
56
|
+
app.before_request(before)
|
|
57
|
+
app.teardown_request(after)
|
|
58
|
+
if health_path:
|
|
59
|
+
app.add_url_rule(health_path, "python_wrap_health", health.report, methods=["GET"])
|
|
60
|
+
|
|
61
|
+
app.extensions["python_wrap"] = True
|