macss-modular-api 0.4.5__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.
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/LICENSE +9 -9
- {macss_modular_api-0.4.5/src/macss_modular_api.egg-info → macss_modular_api-0.4.8}/PKG-INFO +206 -148
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/README.md +166 -113
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/pyproject.toml +59 -55
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/setup.cfg +4 -4
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8/src/macss_modular_api.egg-info}/PKG-INFO +206 -148
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/macss_modular_api.egg-info/SOURCES.txt +22 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/macss_modular_api.egg-info/requires.txt +6 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/__init__.py +109 -72
- macss_modular_api-0.4.8/src/modular_api/core/error_response_middleware.py +33 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/health/health_check.py +84 -84
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/health/health_handler.py +30 -30
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/health/health_service.py +113 -113
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/logger/logger.py +183 -181
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/logger/logging_middleware.py +130 -120
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/metrics/metric.py +294 -294
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/metrics/metric_registry.py +208 -208
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/metrics/metrics_middleware.py +111 -111
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/modular_api.py +281 -215
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/module_builder.py +182 -182
- macss_modular_api-0.4.8/src/modular_api/core/official_plugins.py +309 -0
- macss_modular_api-0.4.8/src/modular_api/core/plugin.py +554 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/registry.py +65 -65
- macss_modular_api-0.4.8/src/modular_api/core/request_pipeline_audit.py +38 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/use_case_exception.py +51 -51
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/usecase.py +202 -202
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/usecase_handler.py +144 -144
- macss_modular_api-0.4.8/src/modular_api/graphql/__init__.py +135 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/catalog/__init__.py +49 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/catalog/graphql_catalog_builder.py +792 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/metadata/__init__.py +25 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/metadata/graphql_metadata_parser.py +503 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/read/__init__.py +49 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/read/sql_read_contract.py +157 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/read/sqlserver_read_compiler.py +384 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/runtime/__init__.py +33 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/runtime/graphql_artifacts.py +589 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/runtime/graphql_runtime_options.py +60 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/runtime/graphql_runtime_plugin.py +1113 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/schema/__init__.py +5 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/schema/graphql_schema_sdl_generator.py +219 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/sqlserver/__init__.py +25 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/sqlserver/physical_model.py +43 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/sqlserver/sql_server_connection_settings.py +48 -0
- macss_modular_api-0.4.8/src/modular_api/graphql/sqlserver/sql_server_metadata_reader.py +379 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/middlewares/cors.py +78 -78
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/openapi/openapi.py +256 -256
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/openapi/swagger_docs.py +52 -48
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/tests/test_auto_schema.py +116 -116
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/tests/test_field_example.py +90 -90
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/tests/test_fromjson_validation.py +148 -148
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/tests/test_modular_api.py +283 -283
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/tests/test_module_builder.py +87 -87
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/tests/test_schema_conformance.py +68 -68
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/tests/test_use_case_exception.py +82 -82
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/tests/test_usecase.py +124 -124
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/tests/test_usecase_handler.py +284 -284
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/macss_modular_api.egg-info/dependency_links.txt +0 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/macss_modular_api.egg-info/top_level.txt +0 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/__init__.py +0 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/health/__init__.py +0 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/logger/__init__.py +0 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/core/metrics/__init__.py +0 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/middlewares/__init__.py +0 -0
- {macss_modular_api-0.4.5 → macss_modular_api-0.4.8}/src/modular_api/openapi/__init__.py +0 -0
- {macss_modular_api-0.4.5 → 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.
|
|
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/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/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:
|
|
30
|
-
Requires-Dist:
|
|
31
|
-
Requires-Dist:
|
|
32
|
-
|
|
33
|
-
Requires-Dist:
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
-
|
|
89
|
-
- `
|
|
90
|
-
-
|
|
91
|
-
-
|
|
92
|
-
-
|
|
93
|
-
-
|
|
94
|
-
-
|
|
95
|
-
- All endpoints
|
|
96
|
-
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
-
|
|
56
|
-
-
|
|
57
|
-
-
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
```python
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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).
|