apipilot 0.1.0__tar.gz

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.
Files changed (38) hide show
  1. apipilot-0.1.0/PKG-INFO +166 -0
  2. apipilot-0.1.0/README.md +147 -0
  3. apipilot-0.1.0/pyproject.toml +24 -0
  4. apipilot-0.1.0/setup.cfg +4 -0
  5. apipilot-0.1.0/src/apipilot/__init__.py +42 -0
  6. apipilot-0.1.0/src/apipilot/asgi/__init__.py +49 -0
  7. apipilot-0.1.0/src/apipilot/asgi/csrf.py +275 -0
  8. apipilot-0.1.0/src/apipilot/asgi/csrf_extras.py +164 -0
  9. apipilot-0.1.0/src/apipilot/asgi/diagnostics.py +131 -0
  10. apipilot-0.1.0/src/apipilot/asgi/middleware.py +492 -0
  11. apipilot-0.1.0/src/apipilot/asgi/observability.py +123 -0
  12. apipilot-0.1.0/src/apipilot/asgi/openapi.py +150 -0
  13. apipilot-0.1.0/src/apipilot/asgi/options.py +166 -0
  14. apipilot-0.1.0/src/apipilot/asgi/ratelimit.py +56 -0
  15. apipilot-0.1.0/src/apipilot/asgi/responses.py +143 -0
  16. apipilot-0.1.0/src/apipilot/asgi/serialization.py +146 -0
  17. apipilot-0.1.0/src/apipilot/content_negotiation.py +134 -0
  18. apipilot-0.1.0/src/apipilot/cookie_profile.py +106 -0
  19. apipilot-0.1.0/src/apipilot/correlation.py +83 -0
  20. apipilot-0.1.0/src/apipilot/csrf_binding.py +35 -0
  21. apipilot-0.1.0/src/apipilot/csrf_format.py +85 -0
  22. apipilot-0.1.0/src/apipilot/envelope.py +111 -0
  23. apipilot-0.1.0/src/apipilot/error_codes.py +90 -0
  24. apipilot-0.1.0/src/apipilot/fetch_metadata.py +82 -0
  25. apipilot-0.1.0/src/apipilot/field_key.py +42 -0
  26. apipilot-0.1.0/src/apipilot/origin.py +77 -0
  27. apipilot-0.1.0/src/apipilot/pagination.py +293 -0
  28. apipilot-0.1.0/src/apipilot/wsgi.py +654 -0
  29. apipilot-0.1.0/src/apipilot.egg-info/PKG-INFO +166 -0
  30. apipilot-0.1.0/src/apipilot.egg-info/SOURCES.txt +36 -0
  31. apipilot-0.1.0/src/apipilot.egg-info/dependency_links.txt +1 -0
  32. apipilot-0.1.0/src/apipilot.egg-info/requires.txt +15 -0
  33. apipilot-0.1.0/src/apipilot.egg-info/top_level.txt +1 -0
  34. apipilot-0.1.0/tests/test_asgi.py +679 -0
  35. apipilot-0.1.0/tests/test_consumer.py +204 -0
  36. apipilot-0.1.0/tests/test_core.py +727 -0
  37. apipilot-0.1.0/tests/test_csrf_extras.py +165 -0
  38. apipilot-0.1.0/tests/test_wsgi_smoke.py +83 -0
@@ -0,0 +1,166 @@
1
+ Metadata-Version: 2.4
2
+ Name: apipilot
3
+ Version: 0.1.0
4
+ Summary: ApiPilot for Python — framework-neutral core plus ASGI adapter. Envelopes, error codes, pagination, validation keys, origin, fetch-metadata, cookie rules, CSRF.
5
+ License: Apache-2.0
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+ Provides-Extra: asgi
9
+ Requires-Dist: fastapi>=0.110; extra == "asgi"
10
+ Requires-Dist: starlette>=0.37; extra == "asgi"
11
+ Requires-Dist: cryptography>=42; extra == "asgi"
12
+ Provides-Extra: wsgi
13
+ Requires-Dist: cryptography>=42; extra == "wsgi"
14
+ Provides-Extra: dev
15
+ Requires-Dist: pytest>=8.0; extra == "dev"
16
+ Requires-Dist: httpx>=0.27; extra == "dev"
17
+ Provides-Extra: docs
18
+ Requires-Dist: sphinx>=7.0; extra == "docs"
19
+
20
+ <!--
21
+ filepath: python/README.md
22
+ package: apipilot
23
+ since: v0.1.0
24
+ purpose: ApiPilot for Python — framework-neutral core plus ASGI adapter (FastAPI, Starlette, Django).
25
+ -->
26
+
27
+ # apipilot
28
+
29
+ ApiPilot for Python. A framework-neutral core implementing the full
30
+ ApiPilot wire contract — envelopes, error codes, pagination, validation
31
+ field keys, origin policy, fetch-metadata policy, cookie rules, and CSRF
32
+ protection — plus an ASGI adapter that works with **FastAPI, Starlette,
33
+ and any ASGI-capable framework including Django**.
34
+
35
+ The core has **zero dependencies**. The ASGI adapter requires
36
+ `cryptography` (AES-256-GCM for CSRF token protection).
37
+
38
+ ## Install
39
+
40
+ ```bash
41
+ pip install apipilot # core only
42
+ pip install "apipilot[asgi]" # core + ASGI adapter
43
+ ```
44
+
45
+ ## Why ASGI and not FastAPI-specific?
46
+
47
+ ApiPilot is a web-framework integration library — its job is middleware,
48
+ headers, cookies, and CSRF. Binding to one framework would repeat the
49
+ mistake of picking a single web stack. ASGI is the Python equivalent of
50
+ the C# `HttpContext` abstraction: one middleware covers FastAPI,
51
+ Starlette, Django (via `asgiref`), Litestar, and Quart. The core package
52
+ stays framework-neutral so non-HTTP uses (CLI validation, testing) can
53
+ import it without pulling in a web stack.
54
+
55
+ ## Quick start (FastAPI)
56
+
57
+ ```python
58
+ from fastapi import FastAPI
59
+ from apipilot.asgi import ApiPilotASGIMiddleware, ApiPilotOptions
60
+
61
+ app = FastAPI()
62
+ app.add_middleware(ApiPilotASGIMiddleware, options=ApiPilotOptions())
63
+ ```
64
+
65
+ ## Quick start (Starlette / raw ASGI / Django)
66
+
67
+ ```python
68
+ # Starlette
69
+ app.add_middleware(ApiPilotASGIMiddleware, options=ApiPilotOptions())
70
+
71
+ # Raw ASGI / Django (ASGI deployment — uvicorn/daphne/hypercorn)
72
+ application = ApiPilotASGIMiddleware(django_asgi_app, options=ApiPilotOptions())
73
+ ```
74
+
75
+ Runnable samples: `samples/fastapi_app.py`, `samples/starlette_app.py`,
76
+ `samples/django_app.py` (Django via `get_asgi_application()`).
77
+
78
+ ## WSGI frameworks (Flask, Django-WSGI, Bottle, Pyramid)
79
+
80
+ `apipilot.wsgi.ApiPilotWSGIMiddleware` implements the same pipeline for
81
+ WSGI apps — same options, same CSRF key ring, same binding resolvers
82
+ (the middleware builds an ASGI-compatible scope from the WSGI environ):
83
+
84
+ ```python
85
+ from apipilot.wsgi import ApiPilotWSGIMiddleware
86
+
87
+ # Flask
88
+ app.wsgi_app = ApiPilotWSGIMiddleware(app.wsgi_app, options=ApiPilotOptions())
89
+
90
+ # Django (WSGI deployment — gunicorn)
91
+ application = ApiPilotWSGIMiddleware(get_wsgi_application(), options=ApiPilotOptions())
92
+ ```
93
+
94
+ Requires `pip install "apipilot[wsgi]"` (cryptography for CSRF). Parsed
95
+ pagination is available at `environ["apipilot.pagination"]`.
96
+
97
+ ## CSRF protection
98
+
99
+ ```python
100
+ from apipilot.asgi import ApiPilotOptions, CsrfKeyRing, generate_key_material
101
+ import base64
102
+
103
+ options = ApiPilotOptions()
104
+ options.csrf.enabled = True
105
+
106
+ key_ring = CsrfKeyRing(
107
+ active_key_id="k1",
108
+ keys={"k1": base64.b64decode(generate_key_material())},
109
+ )
110
+ ```
111
+
112
+ Or via environment (multi-instance contract):
113
+
114
+ ```bash
115
+ export APIPILOT_CSRF_KEYS="k1:<base64-32-byte-key>"
116
+ export APIPILOT_CSRF_ACTIVE_KEY="k1"
117
+ ```
118
+
119
+ When enabled the middleware:
120
+
121
+ - registers `GET /api/csrf` returning `{ "token": "…" }` with
122
+ `Cache-Control: no-store`
123
+ - rejects unsafe methods missing `X-CSRF-Token` with
124
+ `CSRF_HEADER_MISSING` (403)
125
+ - validates signature, expiry, and binding fingerprint, returning
126
+ `CSRF_TOKEN_INVALID` / `CSRF_TOKEN_EXPIRED` on failure
127
+
128
+ Binding resolution order: subject extractor → session extractor →
129
+ pre-auth source (all configurable on `DefaultCsrfBindingResolver`).
130
+
131
+ ## Middleware order
132
+
133
+ Mirrors the C# pipeline:
134
+
135
+ 1. **Correlation** — `X-Request-Id` in/out, GUIDv7 generation, invalid-ID policy
136
+ 2. **Content negotiation** — 406/415 on Accept/Content-Type
137
+ 3. **Origin policy** — RFC 6454 matching on unsafe methods
138
+ 4. **Fetch metadata** — Sec-Fetch-Site/Mode evaluation
139
+ 5. **CSRF** — token validation on protected methods
140
+ 6. **Exception mapping** — exceptions → ApiPilot error envelopes
141
+
142
+ ## Core modules
143
+
144
+ | Module | Mirrors (C#) |
145
+ |---|---|
146
+ | `error_codes` | `ApiErrorCode` — 14 standard codes |
147
+ | `envelope` | `ApiResponse`/`ErrorResponse` builders |
148
+ | `pagination` | `PaginationOptions`, `PageRequest`, query parsing |
149
+ | `field_key` | `FieldKeyNormalizer` |
150
+ | `origin` | `OriginValidator` |
151
+ | `fetch_metadata` | `FetchMetadataEvaluator` |
152
+ | `cookie_profile` | `CookieProfileOptionsValidator` |
153
+ | `csrf_format` | `CsrfTokenFormat` (pipe-delimited payload) |
154
+ | `csrf_binding` | `CsrfBindingProvider.Fingerprint` |
155
+ | `correlation` | `CorrelationMiddleware` logic |
156
+ | `content_negotiation` | `ApiPilotContentNegotiationMiddleware` |
157
+
158
+ ## Conformance
159
+
160
+ `apipilot` passes all **107 ApiPilot conformance vectors (251 assertions)**
161
+ — the same vectors the C# and TypeScript ports run. See
162
+ `tests/conformance_runner.py`.
163
+
164
+ ## License
165
+
166
+ Apache-2.0 — same as ApiPilot.
@@ -0,0 +1,147 @@
1
+ <!--
2
+ filepath: python/README.md
3
+ package: apipilot
4
+ since: v0.1.0
5
+ purpose: ApiPilot for Python — framework-neutral core plus ASGI adapter (FastAPI, Starlette, Django).
6
+ -->
7
+
8
+ # apipilot
9
+
10
+ ApiPilot for Python. A framework-neutral core implementing the full
11
+ ApiPilot wire contract — envelopes, error codes, pagination, validation
12
+ field keys, origin policy, fetch-metadata policy, cookie rules, and CSRF
13
+ protection — plus an ASGI adapter that works with **FastAPI, Starlette,
14
+ and any ASGI-capable framework including Django**.
15
+
16
+ The core has **zero dependencies**. The ASGI adapter requires
17
+ `cryptography` (AES-256-GCM for CSRF token protection).
18
+
19
+ ## Install
20
+
21
+ ```bash
22
+ pip install apipilot # core only
23
+ pip install "apipilot[asgi]" # core + ASGI adapter
24
+ ```
25
+
26
+ ## Why ASGI and not FastAPI-specific?
27
+
28
+ ApiPilot is a web-framework integration library — its job is middleware,
29
+ headers, cookies, and CSRF. Binding to one framework would repeat the
30
+ mistake of picking a single web stack. ASGI is the Python equivalent of
31
+ the C# `HttpContext` abstraction: one middleware covers FastAPI,
32
+ Starlette, Django (via `asgiref`), Litestar, and Quart. The core package
33
+ stays framework-neutral so non-HTTP uses (CLI validation, testing) can
34
+ import it without pulling in a web stack.
35
+
36
+ ## Quick start (FastAPI)
37
+
38
+ ```python
39
+ from fastapi import FastAPI
40
+ from apipilot.asgi import ApiPilotASGIMiddleware, ApiPilotOptions
41
+
42
+ app = FastAPI()
43
+ app.add_middleware(ApiPilotASGIMiddleware, options=ApiPilotOptions())
44
+ ```
45
+
46
+ ## Quick start (Starlette / raw ASGI / Django)
47
+
48
+ ```python
49
+ # Starlette
50
+ app.add_middleware(ApiPilotASGIMiddleware, options=ApiPilotOptions())
51
+
52
+ # Raw ASGI / Django (ASGI deployment — uvicorn/daphne/hypercorn)
53
+ application = ApiPilotASGIMiddleware(django_asgi_app, options=ApiPilotOptions())
54
+ ```
55
+
56
+ Runnable samples: `samples/fastapi_app.py`, `samples/starlette_app.py`,
57
+ `samples/django_app.py` (Django via `get_asgi_application()`).
58
+
59
+ ## WSGI frameworks (Flask, Django-WSGI, Bottle, Pyramid)
60
+
61
+ `apipilot.wsgi.ApiPilotWSGIMiddleware` implements the same pipeline for
62
+ WSGI apps — same options, same CSRF key ring, same binding resolvers
63
+ (the middleware builds an ASGI-compatible scope from the WSGI environ):
64
+
65
+ ```python
66
+ from apipilot.wsgi import ApiPilotWSGIMiddleware
67
+
68
+ # Flask
69
+ app.wsgi_app = ApiPilotWSGIMiddleware(app.wsgi_app, options=ApiPilotOptions())
70
+
71
+ # Django (WSGI deployment — gunicorn)
72
+ application = ApiPilotWSGIMiddleware(get_wsgi_application(), options=ApiPilotOptions())
73
+ ```
74
+
75
+ Requires `pip install "apipilot[wsgi]"` (cryptography for CSRF). Parsed
76
+ pagination is available at `environ["apipilot.pagination"]`.
77
+
78
+ ## CSRF protection
79
+
80
+ ```python
81
+ from apipilot.asgi import ApiPilotOptions, CsrfKeyRing, generate_key_material
82
+ import base64
83
+
84
+ options = ApiPilotOptions()
85
+ options.csrf.enabled = True
86
+
87
+ key_ring = CsrfKeyRing(
88
+ active_key_id="k1",
89
+ keys={"k1": base64.b64decode(generate_key_material())},
90
+ )
91
+ ```
92
+
93
+ Or via environment (multi-instance contract):
94
+
95
+ ```bash
96
+ export APIPILOT_CSRF_KEYS="k1:<base64-32-byte-key>"
97
+ export APIPILOT_CSRF_ACTIVE_KEY="k1"
98
+ ```
99
+
100
+ When enabled the middleware:
101
+
102
+ - registers `GET /api/csrf` returning `{ "token": "…" }` with
103
+ `Cache-Control: no-store`
104
+ - rejects unsafe methods missing `X-CSRF-Token` with
105
+ `CSRF_HEADER_MISSING` (403)
106
+ - validates signature, expiry, and binding fingerprint, returning
107
+ `CSRF_TOKEN_INVALID` / `CSRF_TOKEN_EXPIRED` on failure
108
+
109
+ Binding resolution order: subject extractor → session extractor →
110
+ pre-auth source (all configurable on `DefaultCsrfBindingResolver`).
111
+
112
+ ## Middleware order
113
+
114
+ Mirrors the C# pipeline:
115
+
116
+ 1. **Correlation** — `X-Request-Id` in/out, GUIDv7 generation, invalid-ID policy
117
+ 2. **Content negotiation** — 406/415 on Accept/Content-Type
118
+ 3. **Origin policy** — RFC 6454 matching on unsafe methods
119
+ 4. **Fetch metadata** — Sec-Fetch-Site/Mode evaluation
120
+ 5. **CSRF** — token validation on protected methods
121
+ 6. **Exception mapping** — exceptions → ApiPilot error envelopes
122
+
123
+ ## Core modules
124
+
125
+ | Module | Mirrors (C#) |
126
+ |---|---|
127
+ | `error_codes` | `ApiErrorCode` — 14 standard codes |
128
+ | `envelope` | `ApiResponse`/`ErrorResponse` builders |
129
+ | `pagination` | `PaginationOptions`, `PageRequest`, query parsing |
130
+ | `field_key` | `FieldKeyNormalizer` |
131
+ | `origin` | `OriginValidator` |
132
+ | `fetch_metadata` | `FetchMetadataEvaluator` |
133
+ | `cookie_profile` | `CookieProfileOptionsValidator` |
134
+ | `csrf_format` | `CsrfTokenFormat` (pipe-delimited payload) |
135
+ | `csrf_binding` | `CsrfBindingProvider.Fingerprint` |
136
+ | `correlation` | `CorrelationMiddleware` logic |
137
+ | `content_negotiation` | `ApiPilotContentNegotiationMiddleware` |
138
+
139
+ ## Conformance
140
+
141
+ `apipilot` passes all **107 ApiPilot conformance vectors (251 assertions)**
142
+ — the same vectors the C# and TypeScript ports run. See
143
+ `tests/conformance_runner.py`.
144
+
145
+ ## License
146
+
147
+ Apache-2.0 — same as ApiPilot.
@@ -0,0 +1,24 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "apipilot"
7
+ version = "0.1.0"
8
+ description = "ApiPilot for Python — framework-neutral core plus ASGI adapter. Envelopes, error codes, pagination, validation keys, origin, fetch-metadata, cookie rules, CSRF."
9
+ readme = "README.md"
10
+ license = { text = "Apache-2.0" }
11
+ requires-python = ">=3.10"
12
+ dependencies = []
13
+
14
+ [project.optional-dependencies]
15
+ asgi = ["fastapi>=0.110", "starlette>=0.37", "cryptography>=42"]
16
+ wsgi = ["cryptography>=42"]
17
+ dev = ["pytest>=8.0", "httpx>=0.27"]
18
+ docs = ["sphinx>=7.0"]
19
+
20
+ [tool.setuptools.packages.find]
21
+ where = ["src"]
22
+
23
+ [tool.pytest.ini_options]
24
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,42 @@
1
+ """apipilot — ApiPilot for Python.
2
+
3
+ Framework-neutral core (envelopes, error codes, pagination, validation
4
+ keys, origin, fetch-metadata, cookie rules, CSRF format and binding) plus
5
+ an optional ASGI adapter (``apipilot.asgi``) covering FastAPI, Starlette,
6
+ and ASGI-capable Django.
7
+
8
+ The core has zero dependencies. Install the adapter with::
9
+
10
+ pip install "apipilot[asgi]"
11
+ """
12
+
13
+ from . import (
14
+ content_negotiation,
15
+ cookie_profile,
16
+ correlation,
17
+ csrf_binding,
18
+ csrf_format,
19
+ envelope,
20
+ error_codes,
21
+ fetch_metadata,
22
+ field_key,
23
+ origin,
24
+ pagination,
25
+ )
26
+
27
+ __version__ = "0.1.0"
28
+
29
+ __all__ = [
30
+ "__version__",
31
+ "content_negotiation",
32
+ "cookie_profile",
33
+ "correlation",
34
+ "csrf_binding",
35
+ "csrf_format",
36
+ "envelope",
37
+ "error_codes",
38
+ "fetch_metadata",
39
+ "field_key",
40
+ "origin",
41
+ "pagination",
42
+ ]
@@ -0,0 +1,49 @@
1
+ """apipilot.asgi — the ASGI adapter for ApiPilot.
2
+
3
+ Pure ASGI middleware covering FastAPI, Starlette, and any ASGI-capable
4
+ framework (including Django via its ASGI handler). The core logic lives
5
+ in ``apipilot`` (framework-neutral); this subpackage is the HTTP binding.
6
+
7
+ Install with::
8
+
9
+ pip install "apipilot[asgi]"
10
+ """
11
+
12
+ from . import csrf_extras, diagnostics, observability, openapi, serialization
13
+ from .csrf import (
14
+ CsrfBindingResolver,
15
+ CsrfKeyRing,
16
+ CsrfProtector,
17
+ CsrfService,
18
+ DefaultCsrfBindingResolver,
19
+ generate_key_material,
20
+ key_ring_from_env,
21
+ )
22
+ from .middleware import ApiPilotASGIMiddleware, ApiPilotOptions
23
+ from .responses import (
24
+ send_error,
25
+ send_paginated,
26
+ send_success,
27
+ send_validation_error,
28
+ )
29
+
30
+ __all__ = [
31
+ "ApiPilotASGIMiddleware",
32
+ "ApiPilotOptions",
33
+ "CsrfBindingResolver",
34
+ "CsrfKeyRing",
35
+ "CsrfProtector",
36
+ "CsrfService",
37
+ "DefaultCsrfBindingResolver",
38
+ "csrf_extras",
39
+ "diagnostics",
40
+ "generate_key_material",
41
+ "key_ring_from_env",
42
+ "observability",
43
+ "openapi",
44
+ "send_error",
45
+ "send_paginated",
46
+ "send_success",
47
+ "send_validation_error",
48
+ "serialization",
49
+ ]