usecaseapi 1.0.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.
- usecaseapi-1.0.0/.gitignore +15 -0
- usecaseapi-1.0.0/CODE_OF_CONDUCT.md +5 -0
- usecaseapi-1.0.0/CONTRIBUTING.md +29 -0
- usecaseapi-1.0.0/LICENSE +21 -0
- usecaseapi-1.0.0/PKG-INFO +192 -0
- usecaseapi-1.0.0/README.md +158 -0
- usecaseapi-1.0.0/RELEASE.md +59 -0
- usecaseapi-1.0.0/SECURITY.md +7 -0
- usecaseapi-1.0.0/docs/agent-tools.md +15 -0
- usecaseapi-1.0.0/docs/api-reference.md +79 -0
- usecaseapi-1.0.0/docs/design.md +53 -0
- usecaseapi-1.0.0/docs/integrations.md +33 -0
- usecaseapi-1.0.0/docs/pypi-publishing.md +56 -0
- usecaseapi-1.0.0/docs/quickstart.md +91 -0
- usecaseapi-1.0.0/docs/scaffold.md +67 -0
- usecaseapi-1.0.0/docs/testing.md +37 -0
- usecaseapi-1.0.0/docs/v1-scope.md +36 -0
- usecaseapi-1.0.0/docs/versioning.md +51 -0
- usecaseapi-1.0.0/examples/basic/README.md +9 -0
- usecaseapi-1.0.0/examples/basic/app/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/composition.py +38 -0
- usecaseapi-1.0.0/examples/basic/app/contracts/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/contracts/checkout/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/contracts/checkout/checkout/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/contracts/checkout/checkout/v1.py +41 -0
- usecaseapi-1.0.0/examples/basic/app/contracts/inventory/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/contracts/inventory/check_availability/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/contracts/inventory/check_availability/v1.py +41 -0
- usecaseapi-1.0.0/examples/basic/app/contracts/orders/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/contracts/orders/place_order/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/contracts/orders/place_order/v1.py +71 -0
- usecaseapi-1.0.0/examples/basic/app/usecases/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/usecases/checkout/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/usecases/checkout/checkout.py +38 -0
- usecaseapi-1.0.0/examples/basic/app/usecases/inventory/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/usecases/inventory/check_availability.py +33 -0
- usecaseapi-1.0.0/examples/basic/app/usecases/orders/__init__.py +1 -0
- usecaseapi-1.0.0/examples/basic/app/usecases/orders/place_order.py +28 -0
- usecaseapi-1.0.0/pyproject.toml +209 -0
- usecaseapi-1.0.0/scripts/verify_distribution.py +88 -0
- usecaseapi-1.0.0/scripts/write_coverage_pr_comment.py +191 -0
- usecaseapi-1.0.0/scripts/write_coverage_summary.py +50 -0
- usecaseapi-1.0.0/src/usecaseapi/__init__.py +50 -0
- usecaseapi-1.0.0/src/usecaseapi/api.py +289 -0
- usecaseapi-1.0.0/src/usecaseapi/cli.py +182 -0
- usecaseapi-1.0.0/src/usecaseapi/contracts.py +103 -0
- usecaseapi-1.0.0/src/usecaseapi/docs.py +88 -0
- usecaseapi-1.0.0/src/usecaseapi/errors.py +75 -0
- usecaseapi-1.0.0/src/usecaseapi/model.py +15 -0
- usecaseapi-1.0.0/src/usecaseapi/py.typed +0 -0
- usecaseapi-1.0.0/src/usecaseapi/scaffold.py +298 -0
- usecaseapi-1.0.0/src/usecaseapi/snapshot.py +173 -0
- usecaseapi-1.0.0/tests/test_call.py +150 -0
- usecaseapi-1.0.0/tests/test_coverage_pr_comment.py +143 -0
- usecaseapi-1.0.0/tests/test_full_service_validation.py +616 -0
- usecaseapi-1.0.0/tests/test_snapshot_docs_scaffold.py +176 -0
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
UseCaseAPI follows a simple contributor expectation: be respectful, precise, and constructive.
|
|
4
|
+
|
|
5
|
+
Harassment, discrimination, personal attacks, and deliberately disruptive behavior are not welcome. Maintainers may remove content or restrict participation to keep the project healthy.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Thank you for helping improve UseCaseAPI.
|
|
4
|
+
|
|
5
|
+
## Development setup
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
uv sync --extra dev
|
|
9
|
+
uv run pytest
|
|
10
|
+
uv run mypy
|
|
11
|
+
uv run ruff check .
|
|
12
|
+
uv run ruff format .
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Expectations
|
|
16
|
+
|
|
17
|
+
- Keep the public API small.
|
|
18
|
+
- Preserve the no-DI-container, no-transaction-manager scope.
|
|
19
|
+
- Keep all implementation fully typed and mypy-clean under strict mode.
|
|
20
|
+
- Add tests for behavior changes.
|
|
21
|
+
- Update docs when behavior or public API changes.
|
|
22
|
+
|
|
23
|
+
## Pull request checklist
|
|
24
|
+
|
|
25
|
+
- [ ] Tests pass.
|
|
26
|
+
- [ ] Mypy passes.
|
|
27
|
+
- [ ] Ruff passes.
|
|
28
|
+
- [ ] Docs are updated.
|
|
29
|
+
- [ ] New features do not add hidden serialization, hidden DI, or hidden transaction behavior.
|
usecaseapi-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 UseCaseAPI Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: usecaseapi
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: FastAPI-style contracts for Python application use cases.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Wisteria30/usecaseapi
|
|
6
|
+
Project-URL: Documentation, https://github.com/Wisteria30/usecaseapi/tree/main/docs
|
|
7
|
+
Project-URL: Repository, https://github.com/Wisteria30/usecaseapi
|
|
8
|
+
Project-URL: Issues, https://github.com/Wisteria30/usecaseapi/issues
|
|
9
|
+
Author: UseCaseAPI Contributors
|
|
10
|
+
Maintainer: UseCaseAPI Contributors
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: ai-agents,application-api,contracts,protocol,pydantic,usecase,workflow
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: <3.15,>=3.12
|
|
25
|
+
Requires-Dist: pydantic<3,>=2.12.4
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: build>=1.3.0; extra == 'dev'
|
|
28
|
+
Requires-Dist: coverage>=7.12.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: mypy>=1.18.2; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest>=9.0.1; extra == 'dev'
|
|
31
|
+
Requires-Dist: ruff>=0.14.6; extra == 'dev'
|
|
32
|
+
Requires-Dist: twine>=6.2.0; extra == 'dev'
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# UseCaseAPI
|
|
36
|
+
|
|
37
|
+
FastAPI-style contracts for Python application use cases.
|
|
38
|
+
|
|
39
|
+
UseCaseAPI brings code-first contract clarity to same-process Python use cases. Define contracts with Python Protocols, Pydantic models, and domain exceptions, then bind them to implementations without making HTTP the boundary.
|
|
40
|
+
|
|
41
|
+
Add it when your application has internal use case nodes that need stable names, versions, input/output schemas, declared dependencies, and contract snapshots. It helps teams and AI coding agents see what can be called, what can fail, and whether a change broke an application-layer contract before that break ships.
|
|
42
|
+
|
|
43
|
+
It is inspired by the developer experience of FastAPI, but it is not a web framework. FastAPI turns Python type hints into a public HTTP API contract. UseCaseAPI turns Python type definitions into public contracts for your application layer, without forcing HTTP, RPC, serialization, or a dependency-injection container into the hot path.
|
|
44
|
+
|
|
45
|
+
UseCaseAPI is designed for applications with many internal use case nodes: workflow-like systems, ROS-like node composition, AI-agent tool surfaces, batch workers, CLIs, FastAPI/Django backends, and systems where AI-assisted coding makes accidental breaking changes easier than before.
|
|
46
|
+
|
|
47
|
+
## What it gives you
|
|
48
|
+
|
|
49
|
+
- Code-first usecase contracts using `Protocol`, `Model`, and normal Python exceptions.
|
|
50
|
+
- Same-process direct calls with no JSON serialization in the hot path.
|
|
51
|
+
- Explicit binding from contract token to implementation factory.
|
|
52
|
+
- Declared usecase dependency graph with strict undeclared-call protection.
|
|
53
|
+
- Versioned contract identity: `name@vN`.
|
|
54
|
+
- Contract snapshots, conservative diffing, Markdown docs, Mermaid graph export, and CLI scaffolding.
|
|
55
|
+
- No DI container, no transaction manager, no HTTP/RPC runtime.
|
|
56
|
+
|
|
57
|
+
## Install
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
uv add usecaseapi
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
For local development:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
uv sync --extra dev
|
|
67
|
+
uv run pytest
|
|
68
|
+
uv run mypy
|
|
69
|
+
uv run ruff check .
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Python support: 3.12, 3.13, and 3.14. Pydantic v2 is required.
|
|
73
|
+
|
|
74
|
+
## Quick example
|
|
75
|
+
|
|
76
|
+
Define a contract:
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from __future__ import annotations
|
|
80
|
+
|
|
81
|
+
from typing import ClassVar, Literal, Protocol
|
|
82
|
+
|
|
83
|
+
from usecaseapi import Contract, Model, UseCase, UseCaseError, UseCaseRef, define_usecase
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class Input(Model):
|
|
87
|
+
user_id: str
|
|
88
|
+
sku_id: str
|
|
89
|
+
quantity: int
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class Output(Model):
|
|
93
|
+
order_id: str
|
|
94
|
+
status: Literal["accepted"]
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class PlaceOrderError(UseCaseError):
|
|
98
|
+
code: ClassVar[str] = "orders.place_order"
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class InventoryShortage(PlaceOrderError):
|
|
102
|
+
code: ClassVar[str] = "orders.place_order.inventory_shortage"
|
|
103
|
+
|
|
104
|
+
def __init__(self, *, sku_id: str, requested: int, available: int) -> None:
|
|
105
|
+
self.sku_id = sku_id
|
|
106
|
+
self.requested = requested
|
|
107
|
+
self.available = available
|
|
108
|
+
super().__init__(
|
|
109
|
+
f"inventory shortage: sku_id={sku_id}, requested={requested}, available={available}"
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class PlaceOrder(UseCase[Input, Output], Protocol):
|
|
114
|
+
async def __call__(self, input: Input, /) -> Output:
|
|
115
|
+
...
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
PLACE_ORDER: UseCaseRef[Input, Output] = define_usecase(
|
|
119
|
+
PlaceOrder,
|
|
120
|
+
Contract(
|
|
121
|
+
name="orders.place_order",
|
|
122
|
+
version=1,
|
|
123
|
+
input=Input,
|
|
124
|
+
output=Output,
|
|
125
|
+
raises=(PlaceOrderError,),
|
|
126
|
+
known_errors=(InventoryShortage,),
|
|
127
|
+
),
|
|
128
|
+
)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Implement it:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
from app.contracts.orders.place_order.v1 import Input, Output, PlaceOrder
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class PlaceOrderImpl:
|
|
138
|
+
async def __call__(self, input: Input, /) -> Output:
|
|
139
|
+
return Output(order_id="ord_123", status="accepted")
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
_impl: PlaceOrder = PlaceOrderImpl()
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Bind and call it:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from dataclasses import dataclass
|
|
149
|
+
|
|
150
|
+
from usecaseapi import UseCaseAPI
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@dataclass(frozen=True)
|
|
154
|
+
class AppContext:
|
|
155
|
+
tenant_id: str
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
usecases = UseCaseAPI[AppContext]()
|
|
159
|
+
usecases.bind(PLACE_ORDER, lambda caller: PlaceOrderImpl())
|
|
160
|
+
|
|
161
|
+
output = await usecases.caller(AppContext(tenant_id="t1")).call(PLACE_ORDER, input)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Scaffold
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
usecaseapi scaffold orders.place_order --version 1
|
|
168
|
+
|
|
169
|
+
# Create the next major contract version by scanning app/contracts/orders/place_order/v*.py
|
|
170
|
+
usecaseapi scaffold orders.place_order --next
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
This creates:
|
|
174
|
+
|
|
175
|
+
```text
|
|
176
|
+
app/contracts/orders/place_order/v1.py
|
|
177
|
+
app/usecases/orders/place_order.py
|
|
178
|
+
tests/test_orders_place_order_v1.py
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Why not just write classes yourself?
|
|
182
|
+
|
|
183
|
+
You can, and for small projects you should. UseCaseAPI becomes useful when you have many internal nodes and you need to know:
|
|
184
|
+
|
|
185
|
+
- which functions are public application-layer contracts;
|
|
186
|
+
- which version each caller depends on;
|
|
187
|
+
- which usecase dependencies are allowed;
|
|
188
|
+
- which domain exceptions are part of the contract;
|
|
189
|
+
- whether AI-assisted changes silently broke a stable contract;
|
|
190
|
+
- what the application graph looks like.
|
|
191
|
+
|
|
192
|
+
UseCaseAPI is a small runtime plus guardrails for that problem.
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# UseCaseAPI
|
|
2
|
+
|
|
3
|
+
FastAPI-style contracts for Python application use cases.
|
|
4
|
+
|
|
5
|
+
UseCaseAPI brings code-first contract clarity to same-process Python use cases. Define contracts with Python Protocols, Pydantic models, and domain exceptions, then bind them to implementations without making HTTP the boundary.
|
|
6
|
+
|
|
7
|
+
Add it when your application has internal use case nodes that need stable names, versions, input/output schemas, declared dependencies, and contract snapshots. It helps teams and AI coding agents see what can be called, what can fail, and whether a change broke an application-layer contract before that break ships.
|
|
8
|
+
|
|
9
|
+
It is inspired by the developer experience of FastAPI, but it is not a web framework. FastAPI turns Python type hints into a public HTTP API contract. UseCaseAPI turns Python type definitions into public contracts for your application layer, without forcing HTTP, RPC, serialization, or a dependency-injection container into the hot path.
|
|
10
|
+
|
|
11
|
+
UseCaseAPI is designed for applications with many internal use case nodes: workflow-like systems, ROS-like node composition, AI-agent tool surfaces, batch workers, CLIs, FastAPI/Django backends, and systems where AI-assisted coding makes accidental breaking changes easier than before.
|
|
12
|
+
|
|
13
|
+
## What it gives you
|
|
14
|
+
|
|
15
|
+
- Code-first usecase contracts using `Protocol`, `Model`, and normal Python exceptions.
|
|
16
|
+
- Same-process direct calls with no JSON serialization in the hot path.
|
|
17
|
+
- Explicit binding from contract token to implementation factory.
|
|
18
|
+
- Declared usecase dependency graph with strict undeclared-call protection.
|
|
19
|
+
- Versioned contract identity: `name@vN`.
|
|
20
|
+
- Contract snapshots, conservative diffing, Markdown docs, Mermaid graph export, and CLI scaffolding.
|
|
21
|
+
- No DI container, no transaction manager, no HTTP/RPC runtime.
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
uv add usecaseapi
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
For local development:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
uv sync --extra dev
|
|
33
|
+
uv run pytest
|
|
34
|
+
uv run mypy
|
|
35
|
+
uv run ruff check .
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Python support: 3.12, 3.13, and 3.14. Pydantic v2 is required.
|
|
39
|
+
|
|
40
|
+
## Quick example
|
|
41
|
+
|
|
42
|
+
Define a contract:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from __future__ import annotations
|
|
46
|
+
|
|
47
|
+
from typing import ClassVar, Literal, Protocol
|
|
48
|
+
|
|
49
|
+
from usecaseapi import Contract, Model, UseCase, UseCaseError, UseCaseRef, define_usecase
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class Input(Model):
|
|
53
|
+
user_id: str
|
|
54
|
+
sku_id: str
|
|
55
|
+
quantity: int
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class Output(Model):
|
|
59
|
+
order_id: str
|
|
60
|
+
status: Literal["accepted"]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class PlaceOrderError(UseCaseError):
|
|
64
|
+
code: ClassVar[str] = "orders.place_order"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class InventoryShortage(PlaceOrderError):
|
|
68
|
+
code: ClassVar[str] = "orders.place_order.inventory_shortage"
|
|
69
|
+
|
|
70
|
+
def __init__(self, *, sku_id: str, requested: int, available: int) -> None:
|
|
71
|
+
self.sku_id = sku_id
|
|
72
|
+
self.requested = requested
|
|
73
|
+
self.available = available
|
|
74
|
+
super().__init__(
|
|
75
|
+
f"inventory shortage: sku_id={sku_id}, requested={requested}, available={available}"
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class PlaceOrder(UseCase[Input, Output], Protocol):
|
|
80
|
+
async def __call__(self, input: Input, /) -> Output:
|
|
81
|
+
...
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
PLACE_ORDER: UseCaseRef[Input, Output] = define_usecase(
|
|
85
|
+
PlaceOrder,
|
|
86
|
+
Contract(
|
|
87
|
+
name="orders.place_order",
|
|
88
|
+
version=1,
|
|
89
|
+
input=Input,
|
|
90
|
+
output=Output,
|
|
91
|
+
raises=(PlaceOrderError,),
|
|
92
|
+
known_errors=(InventoryShortage,),
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Implement it:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from app.contracts.orders.place_order.v1 import Input, Output, PlaceOrder
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class PlaceOrderImpl:
|
|
104
|
+
async def __call__(self, input: Input, /) -> Output:
|
|
105
|
+
return Output(order_id="ord_123", status="accepted")
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
_impl: PlaceOrder = PlaceOrderImpl()
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Bind and call it:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from dataclasses import dataclass
|
|
115
|
+
|
|
116
|
+
from usecaseapi import UseCaseAPI
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@dataclass(frozen=True)
|
|
120
|
+
class AppContext:
|
|
121
|
+
tenant_id: str
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
usecases = UseCaseAPI[AppContext]()
|
|
125
|
+
usecases.bind(PLACE_ORDER, lambda caller: PlaceOrderImpl())
|
|
126
|
+
|
|
127
|
+
output = await usecases.caller(AppContext(tenant_id="t1")).call(PLACE_ORDER, input)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Scaffold
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
usecaseapi scaffold orders.place_order --version 1
|
|
134
|
+
|
|
135
|
+
# Create the next major contract version by scanning app/contracts/orders/place_order/v*.py
|
|
136
|
+
usecaseapi scaffold orders.place_order --next
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
This creates:
|
|
140
|
+
|
|
141
|
+
```text
|
|
142
|
+
app/contracts/orders/place_order/v1.py
|
|
143
|
+
app/usecases/orders/place_order.py
|
|
144
|
+
tests/test_orders_place_order_v1.py
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Why not just write classes yourself?
|
|
148
|
+
|
|
149
|
+
You can, and for small projects you should. UseCaseAPI becomes useful when you have many internal nodes and you need to know:
|
|
150
|
+
|
|
151
|
+
- which functions are public application-layer contracts;
|
|
152
|
+
- which version each caller depends on;
|
|
153
|
+
- which usecase dependencies are allowed;
|
|
154
|
+
- which domain exceptions are part of the contract;
|
|
155
|
+
- whether AI-assisted changes silently broke a stable contract;
|
|
156
|
+
- what the application graph looks like.
|
|
157
|
+
|
|
158
|
+
UseCaseAPI is a small runtime plus guardrails for that problem.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Release Process
|
|
2
|
+
|
|
3
|
+
UseCaseAPI uses uv for packaging and GitHub Actions for publishing to PyPI.
|
|
4
|
+
|
|
5
|
+
## Local release validation
|
|
6
|
+
|
|
7
|
+
Run the same checks the release workflow runs before creating a tag:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
uv sync --extra dev
|
|
11
|
+
uv run coverage run -m pytest
|
|
12
|
+
uv run coverage report -m
|
|
13
|
+
uv run mypy
|
|
14
|
+
uv run ruff check .
|
|
15
|
+
uv build
|
|
16
|
+
uv run twine check dist/*
|
|
17
|
+
uv run --isolated --no-project --with dist/*.whl scripts/verify_distribution.py
|
|
18
|
+
uv run --isolated --no-project --with dist/*.tar.gz scripts/verify_distribution.py
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The distribution verification script imports the installed package, calls a real usecase, and checks that the `usecaseapi` CLI can create a versioned scaffold.
|
|
22
|
+
|
|
23
|
+
## Versioning
|
|
24
|
+
|
|
25
|
+
Version numbers follow semantic versioning. Contract-runtime behavior changes require careful release notes because downstream applications may rely on strict guardrail behavior.
|
|
26
|
+
|
|
27
|
+
`pyproject.toml` `[project].version` is the release source of truth.
|
|
28
|
+
|
|
29
|
+
The `src/usecaseapi/__init__.py` module intentionally does not define
|
|
30
|
+
`__version__`; installed package metadata is the source for published versions.
|
|
31
|
+
|
|
32
|
+
## Publishing
|
|
33
|
+
|
|
34
|
+
Publishing is handled by `.github/workflows/release.yml`.
|
|
35
|
+
|
|
36
|
+
On `main`, the workflow detects changes to `pyproject.toml` `[project].version`,
|
|
37
|
+
validates the package, creates the annotated `v{version}` tag, and publishes to
|
|
38
|
+
PyPI. Pushing a matching `v*` tag or running the workflow manually also publishes
|
|
39
|
+
the current package version.
|
|
40
|
+
|
|
41
|
+
The workflow expects PyPI Trusted Publishing:
|
|
42
|
+
|
|
43
|
+
- GitHub environment: `pypi`
|
|
44
|
+
- Repository owner: `Wisteria30`
|
|
45
|
+
- Repository name: `usecaseapi`
|
|
46
|
+
- Workflow filename: `release.yml`
|
|
47
|
+
|
|
48
|
+
The workflow uses GitHub OIDC and `uv publish`; no PyPI API token should be stored in repository secrets.
|
|
49
|
+
|
|
50
|
+
## Human release checklist
|
|
51
|
+
|
|
52
|
+
1. Confirm the package name `usecaseapi` is still available on PyPI and TestPyPI.
|
|
53
|
+
2. Create or log in to the PyPI account that will own the project.
|
|
54
|
+
3. Configure PyPI Trusted Publishing for this repository and workflow.
|
|
55
|
+
4. Create the `pypi` GitHub environment and require approval if desired.
|
|
56
|
+
5. Review the generated package metadata and README rendering locally.
|
|
57
|
+
6. Update `pyproject.toml` `[project].version` and changelog or GitHub release notes.
|
|
58
|
+
7. Merge the version bump to `main`; GitHub Actions creates the annotated tag and publishes.
|
|
59
|
+
8. Confirm the GitHub Actions release workflow completed and the PyPI project page is correct.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
UseCaseAPI is a local contract runtime. It does not expose a network listener or authentication system.
|
|
4
|
+
|
|
5
|
+
Please report security issues privately to the maintainers before public disclosure. Include reproduction steps, affected versions, and potential impact.
|
|
6
|
+
|
|
7
|
+
Do not use public issues for vulnerabilities until a fix or mitigation is available.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# AI Agent Tool Catalogs
|
|
2
|
+
|
|
3
|
+
UseCaseAPI can describe internal tools without turning them into HTTP endpoints.
|
|
4
|
+
|
|
5
|
+
Use `snapshot_from_api(api)` to list:
|
|
6
|
+
|
|
7
|
+
- contract name and version;
|
|
8
|
+
- input and output model schema;
|
|
9
|
+
- public error boundary;
|
|
10
|
+
- known leaf errors;
|
|
11
|
+
- declared graph edges.
|
|
12
|
+
|
|
13
|
+
This is useful for agent runtimes that need a tool surface. The runtime can map selected tools back to same-process `caller.call(USECASE_REF, input)` calls.
|
|
14
|
+
|
|
15
|
+
UseCaseAPI does not directly integrate with any LLM vendor. It provides a stable contract catalog that an adapter can consume.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# API Reference
|
|
2
|
+
|
|
3
|
+
## `Model`
|
|
4
|
+
|
|
5
|
+
Base class for contract input and output objects. It extends Pydantic v2 `BaseModel` with `extra="forbid"` and `frozen=True`.
|
|
6
|
+
|
|
7
|
+
## `UseCaseError`
|
|
8
|
+
|
|
9
|
+
Base class for domain errors that are part of public usecase contracts. Subclasses should define a stable `code: ClassVar[str]`.
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
class PlaceOrderError(UseCaseError):
|
|
13
|
+
code: ClassVar[str] = "orders.place_order"
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## `UseCase[InputT, OutputT]`
|
|
17
|
+
|
|
18
|
+
Structural Protocol for async callable usecase implementations.
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
class PlaceOrder(UseCase[Input, Output], Protocol):
|
|
22
|
+
async def __call__(self, input: Input, /) -> Output:
|
|
23
|
+
...
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## `Contract`
|
|
27
|
+
|
|
28
|
+
Runtime metadata for a contract.
|
|
29
|
+
|
|
30
|
+
Important fields:
|
|
31
|
+
|
|
32
|
+
- `name`: stable dotted name.
|
|
33
|
+
- `version`: major version integer.
|
|
34
|
+
- `input`: input model class.
|
|
35
|
+
- `output`: output model class.
|
|
36
|
+
- `raises`: public exception handling boundary.
|
|
37
|
+
- `known_errors`: documented leaf errors.
|
|
38
|
+
- `stable`: whether the contract is stable.
|
|
39
|
+
- `deprecated`: whether new code should avoid it.
|
|
40
|
+
- `superseded_by`: replacement key, if any.
|
|
41
|
+
|
|
42
|
+
## `define_usecase(protocol, contract)`
|
|
43
|
+
|
|
44
|
+
Creates a typed `UseCaseRef` token.
|
|
45
|
+
|
|
46
|
+
## `UseCaseAPI[ContextT]`
|
|
47
|
+
|
|
48
|
+
Registry and runtime.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
api = UseCaseAPI[AppContext]()
|
|
52
|
+
api.register(PLACE_ORDER)
|
|
53
|
+
api.bind(PLACE_ORDER, lambda caller: PlaceOrderImpl())
|
|
54
|
+
api.validate()
|
|
55
|
+
caller = api.caller(context)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## `Caller[ContextT]`
|
|
59
|
+
|
|
60
|
+
Context-bound call surface.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
output = await caller.call(PLACE_ORDER, input)
|
|
64
|
+
results = await caller.gather(caller.call(A, a), caller.call(B, b))
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`Caller.gather` uses `asyncio.TaskGroup`, so multiple failures preserve Python `ExceptionGroup` behavior.
|
|
68
|
+
|
|
69
|
+
## `snapshot_from_api(api)`
|
|
70
|
+
|
|
71
|
+
Creates a JSON-serializable snapshot for CI and docs.
|
|
72
|
+
|
|
73
|
+
## `diff_snapshots(old, new)`
|
|
74
|
+
|
|
75
|
+
Performs conservative breaking-change detection.
|
|
76
|
+
|
|
77
|
+
## `render_markdown(api)` / `render_mermaid(api)`
|
|
78
|
+
|
|
79
|
+
Renders human-readable docs and graph diagrams.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Design
|
|
2
|
+
|
|
3
|
+
UseCaseAPI is built around one idea: a usecase should be treated as a versioned application API even when it is only called inside the same process.
|
|
4
|
+
|
|
5
|
+
## Core principles
|
|
6
|
+
|
|
7
|
+
1. **Python code is the source of truth.** Contracts are ordinary Python modules. No external IDL is required.
|
|
8
|
+
2. **Protocol-first.** The callable shape is represented by `typing.Protocol`, not by decorators or subclassing requirements.
|
|
9
|
+
3. **Exceptions are exceptions.** Domain errors are real `Exception` subclasses, so stack traces, inheritance, `except`, `except*`, and `ExceptionGroup` remain useful.
|
|
10
|
+
4. **No DI container.** Host frameworks decide where request context, database sessions, transactions, tenants, actors, and credentials are created.
|
|
11
|
+
5. **Same-process direct calls.** UseCaseAPI does not introduce HTTP, RPC, queues, or JSON serialization into the hot path.
|
|
12
|
+
6. **Graph-aware.** Large systems need declared usecase dependencies, graph export, snapshots, and breaking-change detection.
|
|
13
|
+
|
|
14
|
+
## The contract shape
|
|
15
|
+
|
|
16
|
+
A contract module contains:
|
|
17
|
+
|
|
18
|
+
- `Input`: a Pydantic v2 model.
|
|
19
|
+
- `Output`: a Pydantic v2 model.
|
|
20
|
+
- a base domain exception derived from `UseCaseError`.
|
|
21
|
+
- optional leaf domain exceptions.
|
|
22
|
+
- a `Protocol` derived from `UseCase[Input, Output]`.
|
|
23
|
+
- a `UseCaseRef` token created by `define_usecase`.
|
|
24
|
+
|
|
25
|
+
The `UseCaseRef` is used for binding and calling. It carries runtime metadata while preserving type information.
|
|
26
|
+
|
|
27
|
+
## Why not store metadata on the Protocol?
|
|
28
|
+
|
|
29
|
+
Putting metadata directly on the Protocol would make structural implementations appear to require those metadata attributes. UseCaseAPI keeps the Protocol pure and attaches runtime metadata to the `UseCaseRef` token instead.
|
|
30
|
+
|
|
31
|
+
## Dependency composition
|
|
32
|
+
|
|
33
|
+
UseCaseAPI does not construct dependency graphs. A binding is a function:
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
Callable[[Caller[ContextT]], UseCase[InputT, OutputT]]
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The host application controls the `ContextT` object. A FastAPI app may put request-scoped state there. A Django app may use middleware-created state. A worker may use job context. An AI agent runtime may use agent session context.
|
|
40
|
+
|
|
41
|
+
## Declared usecase graph
|
|
42
|
+
|
|
43
|
+
A usecase binding can declare which other usecases it may call:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
usecases.bind(CHECKOUT, lambda caller: CheckoutImpl(caller), uses=(PLACE_ORDER,))
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
In strict mode, calling an undeclared usecase from inside a handler raises `UndeclaredUseCaseDependencyError`.
|
|
50
|
+
|
|
51
|
+
## Version identity
|
|
52
|
+
|
|
53
|
+
A contract identity is `name@vN`, for example `orders.place_order@v1`. There is no implicit `latest`. Callers import the version they use.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Integrations
|
|
2
|
+
|
|
3
|
+
UseCaseAPI is intentionally runtime-agnostic.
|
|
4
|
+
|
|
5
|
+
## FastAPI
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
@app.post("/orders")
|
|
9
|
+
async def endpoint(input: Input, request: Request) -> Output:
|
|
10
|
+
ctx = AppContext(session=request.state.session, actor=request.state.actor)
|
|
11
|
+
return await usecases.caller(ctx).call(PLACE_ORDER, input)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
FastAPI remains responsible for request lifecycle, dependency injection, authentication, and transaction setup.
|
|
15
|
+
|
|
16
|
+
## Django
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
async def view(request: HttpRequest) -> JsonResponse:
|
|
20
|
+
ctx = AppContext(request=request)
|
|
21
|
+
output = await usecases.caller(ctx).call(PLACE_ORDER, input)
|
|
22
|
+
return JsonResponse(output.model_dump())
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Django middleware and transaction management remain outside UseCaseAPI.
|
|
26
|
+
|
|
27
|
+
## Workers and CLIs
|
|
28
|
+
|
|
29
|
+
Create a context at job or command start, then call usecases through the caller.
|
|
30
|
+
|
|
31
|
+
## AI agents
|
|
32
|
+
|
|
33
|
+
UseCaseAPI contracts can be exported as a catalog for an agent runtime. The catalog is documentation and selection metadata; it is not the source of truth.
|