macss-modular-api 0.4.6__tar.gz → 0.4.8__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 (66) hide show
  1. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/LICENSE +9 -9
  2. {macss_modular_api-0.4.6/src/macss_modular_api.egg-info → macss_modular_api-0.4.8}/PKG-INFO +206 -148
  3. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/README.md +166 -113
  4. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/pyproject.toml +59 -55
  5. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/setup.cfg +4 -4
  6. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8/src/macss_modular_api.egg-info}/PKG-INFO +206 -148
  7. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/macss_modular_api.egg-info/SOURCES.txt +22 -0
  8. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/macss_modular_api.egg-info/requires.txt +6 -0
  9. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/__init__.py +109 -72
  10. macss_modular_api-0.4.8/src/modular_api/core/error_response_middleware.py +33 -0
  11. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/health/health_check.py +84 -84
  12. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/health/health_handler.py +30 -30
  13. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/health/health_service.py +113 -113
  14. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/logger/logger.py +183 -181
  15. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/logger/logging_middleware.py +130 -120
  16. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/metrics/metric.py +294 -294
  17. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/metrics/metric_registry.py +208 -208
  18. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/metrics/metrics_middleware.py +111 -111
  19. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/modular_api.py +281 -215
  20. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/module_builder.py +182 -182
  21. macss_modular_api-0.4.8/src/modular_api/core/official_plugins.py +309 -0
  22. macss_modular_api-0.4.8/src/modular_api/core/plugin.py +554 -0
  23. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/registry.py +65 -65
  24. macss_modular_api-0.4.8/src/modular_api/core/request_pipeline_audit.py +38 -0
  25. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/use_case_exception.py +51 -51
  26. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/usecase.py +202 -202
  27. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/usecase_handler.py +144 -144
  28. macss_modular_api-0.4.8/src/modular_api/graphql/__init__.py +135 -0
  29. macss_modular_api-0.4.8/src/modular_api/graphql/catalog/__init__.py +49 -0
  30. macss_modular_api-0.4.8/src/modular_api/graphql/catalog/graphql_catalog_builder.py +792 -0
  31. macss_modular_api-0.4.8/src/modular_api/graphql/metadata/__init__.py +25 -0
  32. macss_modular_api-0.4.8/src/modular_api/graphql/metadata/graphql_metadata_parser.py +503 -0
  33. macss_modular_api-0.4.8/src/modular_api/graphql/read/__init__.py +49 -0
  34. macss_modular_api-0.4.8/src/modular_api/graphql/read/sql_read_contract.py +157 -0
  35. macss_modular_api-0.4.8/src/modular_api/graphql/read/sqlserver_read_compiler.py +384 -0
  36. macss_modular_api-0.4.8/src/modular_api/graphql/runtime/__init__.py +33 -0
  37. macss_modular_api-0.4.8/src/modular_api/graphql/runtime/graphql_artifacts.py +589 -0
  38. macss_modular_api-0.4.8/src/modular_api/graphql/runtime/graphql_runtime_options.py +60 -0
  39. macss_modular_api-0.4.8/src/modular_api/graphql/runtime/graphql_runtime_plugin.py +1113 -0
  40. macss_modular_api-0.4.8/src/modular_api/graphql/schema/__init__.py +5 -0
  41. macss_modular_api-0.4.8/src/modular_api/graphql/schema/graphql_schema_sdl_generator.py +219 -0
  42. macss_modular_api-0.4.8/src/modular_api/graphql/sqlserver/__init__.py +25 -0
  43. macss_modular_api-0.4.8/src/modular_api/graphql/sqlserver/physical_model.py +43 -0
  44. macss_modular_api-0.4.8/src/modular_api/graphql/sqlserver/sql_server_connection_settings.py +48 -0
  45. macss_modular_api-0.4.8/src/modular_api/graphql/sqlserver/sql_server_metadata_reader.py +379 -0
  46. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/middlewares/cors.py +78 -78
  47. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/openapi/openapi.py +256 -256
  48. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/openapi/swagger_docs.py +52 -48
  49. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/tests/test_auto_schema.py +116 -116
  50. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/tests/test_field_example.py +90 -90
  51. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/tests/test_fromjson_validation.py +148 -148
  52. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/tests/test_modular_api.py +283 -283
  53. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/tests/test_module_builder.py +87 -87
  54. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/tests/test_schema_conformance.py +68 -68
  55. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/tests/test_use_case_exception.py +82 -82
  56. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/tests/test_usecase.py +124 -124
  57. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/tests/test_usecase_handler.py +284 -284
  58. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/macss_modular_api.egg-info/dependency_links.txt +0 -0
  59. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/macss_modular_api.egg-info/top_level.txt +0 -0
  60. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/__init__.py +0 -0
  61. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/health/__init__.py +0 -0
  62. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/logger/__init__.py +0 -0
  63. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/core/metrics/__init__.py +0 -0
  64. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/middlewares/__init__.py +0 -0
  65. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/openapi/__init__.py +0 -0
  66. {macss_modular_api-0.4.6 → macss_modular_api-0.4.8}/src/modular_api/py.typed +0 -0
@@ -1,9 +1,9 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 @ccisnedev
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 @ccisnedev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,148 +1,206 @@
1
- Metadata-Version: 2.4
2
- Name: macss-modular-api
3
- Version: 0.4.6
4
- Summary: Use-case-centric toolkit for building modular APIs with Starlette. Define UseCase classes (input → validate → execute → output), connect them to HTTP routes, and expose OpenAPI documentation automatically.
5
- Author: ccisne.dev
6
- License-Expression: MIT
7
- Project-URL: Homepage, https://github.com/macss-dev/modular_api
8
- Project-URL: Repository, https://github.com/macss-dev/modular_api/tree/main/code/py
9
- Project-URL: Issues, https://github.com/macss-dev/modular_api/issues
10
- Project-URL: Documentation, https://github.com/macss-dev/modular_api/tree/main/code/py#readme
11
- Keywords: api,usecase,openapi,starlette,modular,macss
12
- Classifier: Development Status :: 4 - Beta
13
- Classifier: Framework :: AsyncIO
14
- Classifier: Intended Audience :: Developers
15
- Classifier: Programming Language :: Python :: 3
16
- Classifier: Programming Language :: Python :: 3.11
17
- Classifier: Programming Language :: Python :: 3.12
18
- Classifier: Programming Language :: Python :: 3.13
19
- Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
20
- Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
21
- Classifier: Typing :: Typed
22
- Requires-Python: >=3.11
23
- Description-Content-Type: text/markdown
24
- License-File: LICENSE
25
- Requires-Dist: starlette>=0.40
26
- Requires-Dist: pydantic>=2.0
27
- Provides-Extra: serve
28
- Requires-Dist: uvicorn>=0.30; extra == "serve"
29
- Provides-Extra: dev
30
- Requires-Dist: pytest>=8.0; extra == "dev"
31
- Requires-Dist: pytest-asyncio>=0.24; extra == "dev"
32
- Requires-Dist: httpx>=0.27; extra == "dev"
33
- Requires-Dist: uvicorn>=0.30; extra == "dev"
34
- Dynamic: license-file
35
-
36
- # modular-api
37
-
38
- Use-case-centric toolkit for building modular APIs with Starlette.
39
- Define `UseCase` classes (input → validate → execute → output), connect them to HTTP routes, and get automatic OpenAPI documentation.
40
-
41
- > Also available in **Dart**: [modular_api](https://pub.dev/packages/modular_api) · **TypeScript**: [@macss/modular-api](https://www.npmjs.com/package/@macss/modular-api)
42
-
43
- ---
44
-
45
- ## Quick start
46
-
47
- ```python
48
- from modular_api import ModularApi, ModuleBuilder
49
-
50
- # ─── Module builder (separate file in real projects) ──────────
51
- def build_greetings_module(m: ModuleBuilder) -> None:
52
- m.usecase("hello", HelloWorld)
53
-
54
- # ─── Server ───────────────────────────────────────────────────
55
- api = ModularApi(base_path="/api")
56
-
57
- api.module("greetings", build_greetings_module)
58
-
59
- api.serve(port=8080)
60
- ```
61
-
62
- ```bash
63
- curl -X POST http://localhost:8080/api/greetings/hello \
64
- -H "Content-Type: application/json" \
65
- -d '{"name":"World"}'
66
- ```
67
-
68
- ```json
69
- { "message": "Hello, World!" }
70
- ```
71
-
72
- **Docs** → `http://localhost:8080/docs`
73
- **Health** → `http://localhost:8080/health`
74
- **OpenAPI JSON** `http://localhost:8080/openapi.json` *(also /openapi.yaml)*
75
- **Metrics** → `http://localhost:8080/metrics` *(opt-in)*
76
-
77
- See `example/example.py` for the full implementation including Input, Output, UseCase with `validate()`, and the builder.
78
-
79
- ---
80
-
81
- ## Features
82
-
83
- - `UseCase[I, O]` — pure business logic, no HTTP concerns
84
- - `Input` / `Output` — DTOs with automatic OpenAPI schema generation via Pydantic `Field()`
85
- - `Output.status_code` — custom HTTP status codes per response
86
- - `UseCaseException` — structured error handling (status_code, message, error_code, details)
87
- - `ModularApi` + `ModuleBuilder` — module registration and routing
88
- - Constructor-based unit testing with fake dependency injection
89
- - `cors_middleware` — built-in CORS support
90
- - Scalar docs at `/docs` — auto-generated from registered use cases
91
- - OpenAPI spec at `/openapi.json` and `/openapi.yaml` raw spec download
92
- - Health check at `GET /health` — [IETF Health Check Response Format](doc/health_check_guide.md)
93
- - Prometheus metrics at `GET /metrics` [Prometheus exposition format](doc/metrics_guide.md)
94
- - Structured JSON logging Loki/Grafana compatible, [request-scoped with trace_id](doc/logger_guide.md)
95
- - All endpoints default to `POST` (configurable per use case)
96
- - Full type annotations with `py.typed` marker (PEP 561)
97
-
98
- ---
99
-
100
- ## Installation
101
-
102
- ```bash
103
- pip install modular-api
104
- ```
105
-
106
- With Uvicorn for `api.serve()`:
107
-
108
- ```bash
109
- pip install modular-api[serve]
110
- ```
111
-
112
- ---
113
-
114
- ## Error handling
115
-
116
- ```python
117
- async def execute(self) -> FoundUserOutput:
118
- user = await repository.find_by_id(self.input.user_id)
119
- if not user:
120
- raise UseCaseException(
121
- status_code=404,
122
- message="User not found",
123
- error_code="USER_NOT_FOUND",
124
- )
125
- return FoundUserOutput(name=user.name)
126
- ```
127
-
128
- ---
129
-
130
- ## Testing
131
-
132
- ```python
133
- async def test_hello_world():
134
- usecase = HelloWorld(HelloInput(name="World"))
135
- error = usecase.validate()
136
- assert error is None
137
-
138
- output = await usecase.execute()
139
- assert output.message == "Hello, World!"
140
- ```
141
-
142
- See [doc/testing_guide.md](doc/testing_guide.md) for the full testing guide.
143
-
144
- ---
145
-
146
- ## License
147
-
148
- MIT — see [LICENSE](LICENSE).
1
+ Metadata-Version: 2.4
2
+ Name: macss-modular-api
3
+ Version: 0.4.8
4
+ Summary: Use-case-centric toolkit for building modular APIs with Starlette. Define UseCase classes (input → validate → execute → output), connect them to HTTP routes, and expose OpenAPI documentation automatically.
5
+ Author: ccisne.dev
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/macss-dev/modular_api
8
+ Project-URL: Repository, https://github.com/macss-dev/modular_api/tree/main/code/py/modular_api
9
+ Project-URL: Issues, https://github.com/macss-dev/modular_api/issues
10
+ Project-URL: Documentation, https://github.com/macss-dev/modular_api/tree/main/code/py/modular_api#readme
11
+ Keywords: api,usecase,openapi,starlette,modular,macss
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Framework :: AsyncIO
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
20
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
21
+ Classifier: Typing :: Typed
22
+ Requires-Python: >=3.11
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: starlette>=0.40
26
+ Requires-Dist: pydantic>=2.0
27
+ Provides-Extra: serve
28
+ Requires-Dist: uvicorn>=0.30; extra == "serve"
29
+ Provides-Extra: graphql
30
+ Requires-Dist: json5>=0.9.14; extra == "graphql"
31
+ Requires-Dist: graphql-core<3.3,>=3.2; extra == "graphql"
32
+ Provides-Extra: dev
33
+ Requires-Dist: json5>=0.9.14; extra == "dev"
34
+ Requires-Dist: graphql-core<3.3,>=3.2; extra == "dev"
35
+ Requires-Dist: pytest>=8.0; extra == "dev"
36
+ Requires-Dist: pytest-asyncio>=0.24; extra == "dev"
37
+ Requires-Dist: httpx>=0.27; extra == "dev"
38
+ Requires-Dist: uvicorn>=0.30; extra == "dev"
39
+ Dynamic: license-file
40
+
41
+ # modular-api
42
+
43
+ Use-case-centric toolkit for building modular APIs with Starlette.
44
+ Define `UseCase` classes (input → validate → execute → output), connect them to HTTP routes, and get automatic OpenAPI documentation.
45
+
46
+ > Also available in **Dart**: [modular_api](https://pub.dev/packages/modular_api) · **TypeScript**: [@macss/modular-api](https://www.npmjs.com/package/@macss/modular-api)
47
+
48
+ ---
49
+
50
+ ## Quick start
51
+
52
+ ```python
53
+ from modular_api import ModularApi, ModuleBuilder
54
+
55
+ # ─── Module builder (separate file in real projects) ──────────
56
+ def build_greetings_module(m: ModuleBuilder) -> None:
57
+ m.usecase("hello", HelloWorld)
58
+
59
+ # ─── Server ───────────────────────────────────────────────────
60
+ api = ModularApi(base_path="/api")
61
+
62
+ api.module("greetings", build_greetings_module)
63
+
64
+ api.serve(port=8080)
65
+ ```
66
+
67
+ ```bash
68
+ curl -X POST http://localhost:8080/api/greetings/hello \
69
+ -H "Content-Type: application/json" \
70
+ -d '{"name":"World"}'
71
+ ```
72
+
73
+ ```json
74
+ { "message": "Hello, World!" }
75
+ ```
76
+
77
+ **Docs** `http://localhost:8080/api/docs`
78
+ **Health** → `http://localhost:8080/api/health`
79
+ **OpenAPI JSON** → `http://localhost:8080/api/openapi.json` *(also /api/openapi.yaml)*
80
+ **Metrics** → `http://localhost:8080/api/metrics` *(opt-in)*
81
+
82
+ See `example/example.py` for the full implementation including Input, Output, UseCase with `validate()`, and the builder.
83
+
84
+ ---
85
+
86
+ ## Features
87
+
88
+ - `UseCase[I, O]` pure business logic, no HTTP concerns
89
+ - `Input` / `Output` DTOs with automatic OpenAPI schema generation via Pydantic `Field()`
90
+ - `Output.status_code`custom HTTP status codes per response
91
+ - `UseCaseException` structured error handling (status_code, message, error_code, details)
92
+ - `ModularApi` + `ModuleBuilder` — module registration and routing
93
+ - Constructor-based unit testing with fake dependency injection
94
+ - `cors_middleware`built-in CORS support
95
+ - All public endpoints resolve under the configured `base_path`.
96
+ - Scalar docs at `/{basePath}/docs` auto-generated from registered use cases
97
+ - OpenAPI spec at `/{basePath}/openapi.json` and `/{basePath}/openapi.yaml` — raw spec download
98
+ - Health check at `GET /{basePath}/health` — [IETF Health Check Response Format](doc/health_check_guide.md)
99
+ - Prometheus metrics at `GET /{basePath}/metrics` — [Prometheus exposition format](doc/metrics_guide.md)
100
+ - Structured JSON logging — Loki/Grafana compatible, [request-scoped with trace_id](doc/logger_guide.md)
101
+ - All endpoints default to `POST` (configurable per use case)
102
+ - Full type annotations with `py.typed` marker (PEP 561)
103
+
104
+ ---
105
+
106
+ ## Plugin host
107
+
108
+ The public plugin contract is available from the package exports and is already
109
+ used by the official health, metrics, OpenAPI, and docs plugins.
110
+
111
+ Current lifecycle behavior:
112
+
113
+ - `api.plugin(...)` registers a plugin instance without running setup yet
114
+ - `setup(host)` runs during `build()` in dependency order
115
+ - `validate(host)` runs after registration freeze and can abort startup
116
+ - `shutdown()` runs in reverse setup order on normal shutdown and on partial
117
+ startup rollback
118
+ - plugin routes always resolve under the configured `base_path`
119
+ - all three public middleware slots are active with deterministic ordering
120
+
121
+ ```python
122
+ from modular_api import ModularApi, Plugin, PluginHost, PluginManifest, PluginRoute
123
+
124
+
125
+ class HelloPlugin(Plugin):
126
+ manifest = PluginManifest(
127
+ id="acme.hello",
128
+ display_name="Hello Plugin",
129
+ version="0.1.0",
130
+ host_api_version=">=0.1.0 <0.2.0",
131
+ )
132
+
133
+ def setup(self, host: PluginHost) -> None:
134
+ host.register_route(
135
+ PluginRoute(
136
+ id="hello-plugin",
137
+ method="GET",
138
+ path="/hello-plugin",
139
+ visibility="custom",
140
+ handler=lambda _: {
141
+ "status": 200,
142
+ "body": {"ok": True, "basePath": host.metadata().base_path},
143
+ },
144
+ )
145
+ )
146
+
147
+ def validate(self, host: PluginHost):
148
+ return []
149
+
150
+
151
+ api = ModularApi(base_path="/api")
152
+ api.plugin(HelloPlugin())
153
+ app = api.build()
154
+ ```
155
+
156
+ ---
157
+
158
+ ## Installation
159
+
160
+ ```bash
161
+ pip install macss-modular-api
162
+ ```
163
+
164
+ With Uvicorn for `api.serve()`:
165
+
166
+ ```bash
167
+ pip install macss-modular-api[serve]
168
+ ```
169
+
170
+ ---
171
+
172
+ ## Error handling
173
+
174
+ ```python
175
+ async def execute(self) -> FoundUserOutput:
176
+ user = await repository.find_by_id(self.input.user_id)
177
+ if not user:
178
+ raise UseCaseException(
179
+ status_code=404,
180
+ message="User not found",
181
+ error_code="USER_NOT_FOUND",
182
+ )
183
+ return FoundUserOutput(name=user.name)
184
+ ```
185
+
186
+ ---
187
+
188
+ ## Testing
189
+
190
+ ```python
191
+ async def test_hello_world():
192
+ usecase = HelloWorld(HelloInput(name="World"))
193
+ error = usecase.validate()
194
+ assert error is None
195
+
196
+ output = await usecase.execute()
197
+ assert output.message == "Hello, World!"
198
+ ```
199
+
200
+ See [doc/testing_guide.md](doc/testing_guide.md) for the full testing guide.
201
+
202
+ ---
203
+
204
+ ## License
205
+
206
+ MIT — see [LICENSE](LICENSE).
@@ -1,113 +1,166 @@
1
- # modular-api
2
-
3
- Use-case-centric toolkit for building modular APIs with Starlette.
4
- Define `UseCase` classes (input → validate → execute → output), connect them to HTTP routes, and get automatic OpenAPI documentation.
5
-
6
- > Also available in **Dart**: [modular_api](https://pub.dev/packages/modular_api) · **TypeScript**: [@macss/modular-api](https://www.npmjs.com/package/@macss/modular-api)
7
-
8
- ---
9
-
10
- ## Quick start
11
-
12
- ```python
13
- from modular_api import ModularApi, ModuleBuilder
14
-
15
- # ─── Module builder (separate file in real projects) ──────────
16
- def build_greetings_module(m: ModuleBuilder) -> None:
17
- m.usecase("hello", HelloWorld)
18
-
19
- # ─── Server ───────────────────────────────────────────────────
20
- api = ModularApi(base_path="/api")
21
-
22
- api.module("greetings", build_greetings_module)
23
-
24
- api.serve(port=8080)
25
- ```
26
-
27
- ```bash
28
- curl -X POST http://localhost:8080/api/greetings/hello \
29
- -H "Content-Type: application/json" \
30
- -d '{"name":"World"}'
31
- ```
32
-
33
- ```json
34
- { "message": "Hello, World!" }
35
- ```
36
-
37
- **Docs** → `http://localhost:8080/docs`
38
- **Health** → `http://localhost:8080/health`
39
- **OpenAPI JSON** → `http://localhost:8080/openapi.json` *(also /openapi.yaml)*
40
- **Metrics** → `http://localhost:8080/metrics` *(opt-in)*
41
-
42
- See `example/example.py` for the full implementation including Input, Output, UseCase with `validate()`, and the builder.
43
-
44
- ---
45
-
46
- ## Features
47
-
48
- - `UseCase[I, O]` — pure business logic, no HTTP concerns
49
- - `Input` / `Output` — DTOs with automatic OpenAPI schema generation via Pydantic `Field()`
50
- - `Output.status_code` — custom HTTP status codes per response
51
- - `UseCaseException` — structured error handling (status_code, message, error_code, details)
52
- - `ModularApi` + `ModuleBuilder` — module registration and routing
53
- - Constructor-based unit testing with fake dependency injection
54
- - `cors_middleware` — built-in CORS support
55
- - Scalar docs at `/docs` auto-generated from registered use cases
56
- - OpenAPI spec at `/openapi.json` and `/openapi.yaml` raw spec download
57
- - Health check at `GET /health` — [IETF Health Check Response Format](doc/health_check_guide.md)
58
- - Prometheus metrics at `GET /metrics` — [Prometheus exposition format](doc/metrics_guide.md)
59
- - Structured JSON logging Loki/Grafana compatible, [request-scoped with trace_id](doc/logger_guide.md)
60
- - All endpoints default to `POST` (configurable per use case)
61
- - Full type annotations with `py.typed` marker (PEP 561)
62
-
63
- ---
64
-
65
- ## Installation
66
-
67
- ```bash
68
- pip install modular-api
69
- ```
70
-
71
- With Uvicorn for `api.serve()`:
72
-
73
- ```bash
74
- pip install modular-api[serve]
75
- ```
76
-
77
- ---
78
-
79
- ## Error handling
80
-
81
- ```python
82
- async def execute(self) -> FoundUserOutput:
83
- user = await repository.find_by_id(self.input.user_id)
84
- if not user:
85
- raise UseCaseException(
86
- status_code=404,
87
- message="User not found",
88
- error_code="USER_NOT_FOUND",
89
- )
90
- return FoundUserOutput(name=user.name)
91
- ```
92
-
93
- ---
94
-
95
- ## Testing
96
-
97
- ```python
98
- async def test_hello_world():
99
- usecase = HelloWorld(HelloInput(name="World"))
100
- error = usecase.validate()
101
- assert error is None
102
-
103
- output = await usecase.execute()
104
- assert output.message == "Hello, World!"
105
- ```
106
-
107
- See [doc/testing_guide.md](doc/testing_guide.md) for the full testing guide.
108
-
109
- ---
110
-
111
- ## License
112
-
113
- MIT see [LICENSE](LICENSE).
1
+ # modular-api
2
+
3
+ Use-case-centric toolkit for building modular APIs with Starlette.
4
+ Define `UseCase` classes (input → validate → execute → output), connect them to HTTP routes, and get automatic OpenAPI documentation.
5
+
6
+ > Also available in **Dart**: [modular_api](https://pub.dev/packages/modular_api) · **TypeScript**: [@macss/modular-api](https://www.npmjs.com/package/@macss/modular-api)
7
+
8
+ ---
9
+
10
+ ## Quick start
11
+
12
+ ```python
13
+ from modular_api import ModularApi, ModuleBuilder
14
+
15
+ # ─── Module builder (separate file in real projects) ──────────
16
+ def build_greetings_module(m: ModuleBuilder) -> None:
17
+ m.usecase("hello", HelloWorld)
18
+
19
+ # ─── Server ───────────────────────────────────────────────────
20
+ api = ModularApi(base_path="/api")
21
+
22
+ api.module("greetings", build_greetings_module)
23
+
24
+ api.serve(port=8080)
25
+ ```
26
+
27
+ ```bash
28
+ curl -X POST http://localhost:8080/api/greetings/hello \
29
+ -H "Content-Type: application/json" \
30
+ -d '{"name":"World"}'
31
+ ```
32
+
33
+ ```json
34
+ { "message": "Hello, World!" }
35
+ ```
36
+
37
+ **Docs** → `http://localhost:8080/api/docs`
38
+ **Health** → `http://localhost:8080/api/health`
39
+ **OpenAPI JSON** → `http://localhost:8080/api/openapi.json` *(also /api/openapi.yaml)*
40
+ **Metrics** → `http://localhost:8080/api/metrics` *(opt-in)*
41
+
42
+ See `example/example.py` for the full implementation including Input, Output, UseCase with `validate()`, and the builder.
43
+
44
+ ---
45
+
46
+ ## Features
47
+
48
+ - `UseCase[I, O]` — pure business logic, no HTTP concerns
49
+ - `Input` / `Output` — DTOs with automatic OpenAPI schema generation via Pydantic `Field()`
50
+ - `Output.status_code` — custom HTTP status codes per response
51
+ - `UseCaseException` — structured error handling (status_code, message, error_code, details)
52
+ - `ModularApi` + `ModuleBuilder` — module registration and routing
53
+ - Constructor-based unit testing with fake dependency injection
54
+ - `cors_middleware` — built-in CORS support
55
+ - All public endpoints resolve under the configured `base_path`.
56
+ - Scalar docs at `/{basePath}/docs` auto-generated from registered use cases
57
+ - OpenAPI spec at `/{basePath}/openapi.json` and `/{basePath}/openapi.yaml` — raw spec download
58
+ - Health check at `GET /{basePath}/health` — [IETF Health Check Response Format](doc/health_check_guide.md)
59
+ - Prometheus metrics at `GET /{basePath}/metrics` [Prometheus exposition format](doc/metrics_guide.md)
60
+ - Structured JSON logging Loki/Grafana compatible, [request-scoped with trace_id](doc/logger_guide.md)
61
+ - All endpoints default to `POST` (configurable per use case)
62
+ - Full type annotations with `py.typed` marker (PEP 561)
63
+
64
+ ---
65
+
66
+ ## Plugin host
67
+
68
+ The public plugin contract is available from the package exports and is already
69
+ used by the official health, metrics, OpenAPI, and docs plugins.
70
+
71
+ Current lifecycle behavior:
72
+
73
+ - `api.plugin(...)` registers a plugin instance without running setup yet
74
+ - `setup(host)` runs during `build()` in dependency order
75
+ - `validate(host)` runs after registration freeze and can abort startup
76
+ - `shutdown()` runs in reverse setup order on normal shutdown and on partial
77
+ startup rollback
78
+ - plugin routes always resolve under the configured `base_path`
79
+ - all three public middleware slots are active with deterministic ordering
80
+
81
+ ```python
82
+ from modular_api import ModularApi, Plugin, PluginHost, PluginManifest, PluginRoute
83
+
84
+
85
+ class HelloPlugin(Plugin):
86
+ manifest = PluginManifest(
87
+ id="acme.hello",
88
+ display_name="Hello Plugin",
89
+ version="0.1.0",
90
+ host_api_version=">=0.1.0 <0.2.0",
91
+ )
92
+
93
+ def setup(self, host: PluginHost) -> None:
94
+ host.register_route(
95
+ PluginRoute(
96
+ id="hello-plugin",
97
+ method="GET",
98
+ path="/hello-plugin",
99
+ visibility="custom",
100
+ handler=lambda _: {
101
+ "status": 200,
102
+ "body": {"ok": True, "basePath": host.metadata().base_path},
103
+ },
104
+ )
105
+ )
106
+
107
+ def validate(self, host: PluginHost):
108
+ return []
109
+
110
+
111
+ api = ModularApi(base_path="/api")
112
+ api.plugin(HelloPlugin())
113
+ app = api.build()
114
+ ```
115
+
116
+ ---
117
+
118
+ ## Installation
119
+
120
+ ```bash
121
+ pip install macss-modular-api
122
+ ```
123
+
124
+ With Uvicorn for `api.serve()`:
125
+
126
+ ```bash
127
+ pip install macss-modular-api[serve]
128
+ ```
129
+
130
+ ---
131
+
132
+ ## Error handling
133
+
134
+ ```python
135
+ async def execute(self) -> FoundUserOutput:
136
+ user = await repository.find_by_id(self.input.user_id)
137
+ if not user:
138
+ raise UseCaseException(
139
+ status_code=404,
140
+ message="User not found",
141
+ error_code="USER_NOT_FOUND",
142
+ )
143
+ return FoundUserOutput(name=user.name)
144
+ ```
145
+
146
+ ---
147
+
148
+ ## Testing
149
+
150
+ ```python
151
+ async def test_hello_world():
152
+ usecase = HelloWorld(HelloInput(name="World"))
153
+ error = usecase.validate()
154
+ assert error is None
155
+
156
+ output = await usecase.execute()
157
+ assert output.message == "Hello, World!"
158
+ ```
159
+
160
+ See [doc/testing_guide.md](doc/testing_guide.md) for the full testing guide.
161
+
162
+ ---
163
+
164
+ ## License
165
+
166
+ MIT — see [LICENSE](LICENSE).