macss-modular-api 0.4.1__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.1/LICENSE +9 -0
- macss_modular_api-0.4.1/PKG-INFO +148 -0
- macss_modular_api-0.4.1/README.md +113 -0
- macss_modular_api-0.4.1/pyproject.toml +55 -0
- macss_modular_api-0.4.1/setup.cfg +4 -0
- macss_modular_api-0.4.1/src/macss_modular_api.egg-info/PKG-INFO +148 -0
- macss_modular_api-0.4.1/src/macss_modular_api.egg-info/SOURCES.txt +38 -0
- macss_modular_api-0.4.1/src/macss_modular_api.egg-info/dependency_links.txt +1 -0
- macss_modular_api-0.4.1/src/macss_modular_api.egg-info/requires.txt +11 -0
- macss_modular_api-0.4.1/src/macss_modular_api.egg-info/top_level.txt +1 -0
- macss_modular_api-0.4.1/src/modular_api/__init__.py +70 -0
- macss_modular_api-0.4.1/src/modular_api/core/__init__.py +0 -0
- macss_modular_api-0.4.1/src/modular_api/core/health/__init__.py +0 -0
- macss_modular_api-0.4.1/src/modular_api/core/health/health_check.py +84 -0
- macss_modular_api-0.4.1/src/modular_api/core/health/health_handler.py +30 -0
- macss_modular_api-0.4.1/src/modular_api/core/health/health_service.py +113 -0
- macss_modular_api-0.4.1/src/modular_api/core/logger/__init__.py +0 -0
- macss_modular_api-0.4.1/src/modular_api/core/logger/logger.py +181 -0
- macss_modular_api-0.4.1/src/modular_api/core/logger/logging_middleware.py +120 -0
- macss_modular_api-0.4.1/src/modular_api/core/metrics/__init__.py +0 -0
- macss_modular_api-0.4.1/src/modular_api/core/metrics/metric.py +294 -0
- macss_modular_api-0.4.1/src/modular_api/core/metrics/metric_registry.py +208 -0
- macss_modular_api-0.4.1/src/modular_api/core/metrics/metrics_middleware.py +111 -0
- macss_modular_api-0.4.1/src/modular_api/core/modular_api.py +211 -0
- macss_modular_api-0.4.1/src/modular_api/core/module_builder.py +134 -0
- macss_modular_api-0.4.1/src/modular_api/core/registry.py +65 -0
- macss_modular_api-0.4.1/src/modular_api/core/use_case_exception.py +51 -0
- macss_modular_api-0.4.1/src/modular_api/core/usecase.py +96 -0
- macss_modular_api-0.4.1/src/modular_api/core/usecase_handler.py +104 -0
- macss_modular_api-0.4.1/src/modular_api/middlewares/__init__.py +0 -0
- macss_modular_api-0.4.1/src/modular_api/middlewares/cors.py +78 -0
- macss_modular_api-0.4.1/src/modular_api/openapi/__init__.py +0 -0
- macss_modular_api-0.4.1/src/modular_api/openapi/openapi.py +255 -0
- macss_modular_api-0.4.1/src/modular_api/openapi/swagger_docs.py +228 -0
- macss_modular_api-0.4.1/src/modular_api/py.typed +0 -0
- macss_modular_api-0.4.1/tests/test_modular_api.py +252 -0
- macss_modular_api-0.4.1/tests/test_module_builder.py +131 -0
- macss_modular_api-0.4.1/tests/test_use_case_exception.py +82 -0
- macss_modular_api-0.4.1/tests/test_usecase.py +160 -0
- macss_modular_api-0.4.1/tests/test_usecase_handler.py +257 -0
|
@@ -0,0 +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.
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: macss-modular-api
|
|
3
|
+
Version: 0.4.1
|
|
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_py
|
|
8
|
+
Project-URL: Repository, https://github.com/macss-dev/modular_api_py
|
|
9
|
+
Project-URL: Issues, https://github.com/macss-dev/modular_api_py/issues
|
|
10
|
+
Project-URL: Documentation, https://github.com/macss-dev/modular_api_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 `to_json()` and `to_schema()` for automatic OpenAPI
|
|
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) -> None:
|
|
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
|
+
self._output = FoundUserOutput(name=user.name)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Testing
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
def test_hello_world():
|
|
134
|
+
usecase = HelloWorld(HelloInput(name="World"))
|
|
135
|
+
error = usecase.validate()
|
|
136
|
+
assert error is None
|
|
137
|
+
|
|
138
|
+
await usecase.execute()
|
|
139
|
+
assert usecase.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).
|
|
@@ -0,0 +1,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/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 `to_json()` and `to_schema()` for automatic OpenAPI
|
|
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) -> None:
|
|
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
|
+
self._output = FoundUserOutput(name=user.name)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## Testing
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
def test_hello_world():
|
|
99
|
+
usecase = HelloWorld(HelloInput(name="World"))
|
|
100
|
+
error = usecase.validate()
|
|
101
|
+
assert error is None
|
|
102
|
+
|
|
103
|
+
await usecase.execute()
|
|
104
|
+
assert usecase.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).
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "macss-modular-api"
|
|
7
|
+
version = "0.4.1"
|
|
8
|
+
description = "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."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
authors = [{ name = "ccisne.dev" }]
|
|
13
|
+
keywords = ["api", "usecase", "openapi", "starlette", "modular", "macss"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Framework :: AsyncIO",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Topic :: Internet :: WWW/HTTP :: HTTP Servers",
|
|
23
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"starlette>=0.40",
|
|
28
|
+
"pydantic>=2.0",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
serve = ["uvicorn>=0.30"]
|
|
33
|
+
dev = [
|
|
34
|
+
"pytest>=8.0",
|
|
35
|
+
"pytest-asyncio>=0.24",
|
|
36
|
+
"httpx>=0.27",
|
|
37
|
+
"uvicorn>=0.30",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Homepage = "https://github.com/macss-dev/modular_api_py"
|
|
42
|
+
Repository = "https://github.com/macss-dev/modular_api_py"
|
|
43
|
+
Issues = "https://github.com/macss-dev/modular_api_py/issues"
|
|
44
|
+
Documentation = "https://github.com/macss-dev/modular_api_py#readme"
|
|
45
|
+
|
|
46
|
+
[tool.setuptools.packages.find]
|
|
47
|
+
where = ["src"]
|
|
48
|
+
|
|
49
|
+
[tool.pytest.ini_options]
|
|
50
|
+
testpaths = ["tests"]
|
|
51
|
+
asyncio_mode = "auto"
|
|
52
|
+
|
|
53
|
+
[tool.pyright]
|
|
54
|
+
pythonVersion = "3.11"
|
|
55
|
+
typeCheckingMode = "strict"
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: macss-modular-api
|
|
3
|
+
Version: 0.4.1
|
|
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_py
|
|
8
|
+
Project-URL: Repository, https://github.com/macss-dev/modular_api_py
|
|
9
|
+
Project-URL: Issues, https://github.com/macss-dev/modular_api_py/issues
|
|
10
|
+
Project-URL: Documentation, https://github.com/macss-dev/modular_api_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 `to_json()` and `to_schema()` for automatic OpenAPI
|
|
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) -> None:
|
|
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
|
+
self._output = FoundUserOutput(name=user.name)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
## Testing
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
def test_hello_world():
|
|
134
|
+
usecase = HelloWorld(HelloInput(name="World"))
|
|
135
|
+
error = usecase.validate()
|
|
136
|
+
assert error is None
|
|
137
|
+
|
|
138
|
+
await usecase.execute()
|
|
139
|
+
assert usecase.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).
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/macss_modular_api.egg-info/PKG-INFO
|
|
5
|
+
src/macss_modular_api.egg-info/SOURCES.txt
|
|
6
|
+
src/macss_modular_api.egg-info/dependency_links.txt
|
|
7
|
+
src/macss_modular_api.egg-info/requires.txt
|
|
8
|
+
src/macss_modular_api.egg-info/top_level.txt
|
|
9
|
+
src/modular_api/__init__.py
|
|
10
|
+
src/modular_api/py.typed
|
|
11
|
+
src/modular_api/core/__init__.py
|
|
12
|
+
src/modular_api/core/modular_api.py
|
|
13
|
+
src/modular_api/core/module_builder.py
|
|
14
|
+
src/modular_api/core/registry.py
|
|
15
|
+
src/modular_api/core/use_case_exception.py
|
|
16
|
+
src/modular_api/core/usecase.py
|
|
17
|
+
src/modular_api/core/usecase_handler.py
|
|
18
|
+
src/modular_api/core/health/__init__.py
|
|
19
|
+
src/modular_api/core/health/health_check.py
|
|
20
|
+
src/modular_api/core/health/health_handler.py
|
|
21
|
+
src/modular_api/core/health/health_service.py
|
|
22
|
+
src/modular_api/core/logger/__init__.py
|
|
23
|
+
src/modular_api/core/logger/logger.py
|
|
24
|
+
src/modular_api/core/logger/logging_middleware.py
|
|
25
|
+
src/modular_api/core/metrics/__init__.py
|
|
26
|
+
src/modular_api/core/metrics/metric.py
|
|
27
|
+
src/modular_api/core/metrics/metric_registry.py
|
|
28
|
+
src/modular_api/core/metrics/metrics_middleware.py
|
|
29
|
+
src/modular_api/middlewares/__init__.py
|
|
30
|
+
src/modular_api/middlewares/cors.py
|
|
31
|
+
src/modular_api/openapi/__init__.py
|
|
32
|
+
src/modular_api/openapi/openapi.py
|
|
33
|
+
src/modular_api/openapi/swagger_docs.py
|
|
34
|
+
tests/test_modular_api.py
|
|
35
|
+
tests/test_module_builder.py
|
|
36
|
+
tests/test_use_case_exception.py
|
|
37
|
+
tests/test_usecase.py
|
|
38
|
+
tests/test_usecase_handler.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
modular_api
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""modular_api — Use-case-centric toolkit for building modular APIs with Starlette."""
|
|
2
|
+
|
|
3
|
+
from modular_api.core.health.health_check import (
|
|
4
|
+
HealthCheck,
|
|
5
|
+
HealthCheckResult,
|
|
6
|
+
HealthStatus,
|
|
7
|
+
)
|
|
8
|
+
from modular_api.core.health.health_handler import health_handler
|
|
9
|
+
from modular_api.core.health.health_service import HealthResponse, HealthService
|
|
10
|
+
from modular_api.core.logger.logger import (
|
|
11
|
+
LogLevel,
|
|
12
|
+
ModularLogger,
|
|
13
|
+
RequestScopedLogger,
|
|
14
|
+
)
|
|
15
|
+
from modular_api.core.logger.logging_middleware import logging_middleware
|
|
16
|
+
from modular_api.core.metrics.metric import Counter, Gauge, Histogram, MetricSample
|
|
17
|
+
from modular_api.core.modular_api import ModularApi
|
|
18
|
+
from modular_api.core.metrics.metric_registry import MetricRegistry, MetricsRegistrar
|
|
19
|
+
from modular_api.core.metrics.metrics_middleware import metrics_handler, metrics_middleware
|
|
20
|
+
from modular_api.core.module_builder import ModuleBuilder
|
|
21
|
+
from modular_api.core.registry import ApiRegistry, UseCaseDocMeta, UseCaseRegistration, api_registry
|
|
22
|
+
from modular_api.core.use_case_exception import UseCaseException
|
|
23
|
+
from modular_api.core.usecase import Input, Output, UseCase
|
|
24
|
+
from modular_api.core.usecase_handler import usecase_handler
|
|
25
|
+
from modular_api.middlewares.cors import cors_middleware
|
|
26
|
+
from modular_api.openapi.openapi import (
|
|
27
|
+
build_openapi_spec,
|
|
28
|
+
json_to_yaml,
|
|
29
|
+
openapi_json_handler,
|
|
30
|
+
openapi_yaml_handler,
|
|
31
|
+
)
|
|
32
|
+
from modular_api.openapi.swagger_docs import swagger_docs_handler
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"ApiRegistry",
|
|
36
|
+
"Counter",
|
|
37
|
+
"Gauge",
|
|
38
|
+
"HealthCheck",
|
|
39
|
+
"HealthCheckResult",
|
|
40
|
+
"HealthResponse",
|
|
41
|
+
"HealthService",
|
|
42
|
+
"HealthStatus",
|
|
43
|
+
"Histogram",
|
|
44
|
+
"Input",
|
|
45
|
+
"LogLevel",
|
|
46
|
+
"MetricRegistry",
|
|
47
|
+
"MetricSample",
|
|
48
|
+
"MetricsRegistrar",
|
|
49
|
+
"ModularApi",
|
|
50
|
+
"ModularLogger",
|
|
51
|
+
"ModuleBuilder",
|
|
52
|
+
"Output",
|
|
53
|
+
"RequestScopedLogger",
|
|
54
|
+
"UseCase",
|
|
55
|
+
"UseCaseDocMeta",
|
|
56
|
+
"UseCaseException",
|
|
57
|
+
"UseCaseRegistration",
|
|
58
|
+
"api_registry",
|
|
59
|
+
"build_openapi_spec",
|
|
60
|
+
"cors_middleware",
|
|
61
|
+
"health_handler",
|
|
62
|
+
"json_to_yaml",
|
|
63
|
+
"logging_middleware",
|
|
64
|
+
"metrics_handler",
|
|
65
|
+
"metrics_middleware",
|
|
66
|
+
"openapi_json_handler",
|
|
67
|
+
"openapi_yaml_handler",
|
|
68
|
+
"swagger_docs_handler",
|
|
69
|
+
"usecase_handler",
|
|
70
|
+
]
|
|
File without changes
|
|
File without changes
|