function-api-builder 0.1.0__py3-none-any.whl → 0.1.1__py3-none-any.whl

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.
@@ -0,0 +1,391 @@
1
+ Metadata-Version: 2.4
2
+ Name: function-api-builder
3
+ Version: 0.1.1
4
+ Summary: Generate complete FastAPI applications from decorated Python business functions.
5
+ Author: Sushruth Samson
6
+ License-Expression: MIT
7
+ Keywords: api-builder,api-generator,backend,code-generation,fastapi,pydantic
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Environment :: Console
10
+ Classifier: Framework :: FastAPI
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Software Development :: Code Generators
19
+ Requires-Python: >=3.11
20
+ Requires-Dist: fastapi>=0.115
21
+ Requires-Dist: pydantic>=2.7
22
+ Requires-Dist: uvicorn>=0.30
23
+ Provides-Extra: dev
24
+ Requires-Dist: build>=1.2; extra == 'dev'
25
+ Requires-Dist: httpx>=0.27; extra == 'dev'
26
+ Requires-Dist: mypy>=1.13; extra == 'dev'
27
+ Requires-Dist: pytest>=8; extra == 'dev'
28
+ Requires-Dist: ruff>=0.8; extra == 'dev'
29
+ Requires-Dist: twine>=6; extra == 'dev'
30
+ Description-Content-Type: text/markdown
31
+
32
+ <p align="center">
33
+ <img src="assets/api-builder.svg" alt="API Builder" width="180">
34
+ </p>
35
+
36
+ <h1 align="center">API Builder</h1>
37
+
38
+ <p align="center">
39
+ Generate a complete FastAPI application from decorated Python business functions.
40
+ </p>
41
+
42
+ ## Status
43
+
44
+ This project is alpha software. The intended PyPI distribution name is `api-builder`.
45
+ PyPI currently rejects that exact name as too similar to an existing project, so the
46
+ first published release is temporarily available as `function-api-builder` while the
47
+ name issue is resolved.
48
+
49
+ ## Install
50
+
51
+ Intended install command:
52
+
53
+ ```bash
54
+ pip install api-builder
55
+ ```
56
+
57
+ Current published fallback:
58
+
59
+ ```bash
60
+ pip install function-api-builder
61
+ ```
62
+
63
+ Both distributions expose the same Python packages and the same `api-builder` command.
64
+
65
+ ## What It Does
66
+
67
+ API Builder turns plain, typed business functions into a runnable FastAPI service.
68
+ You write the business logic once. The builder discovers those functions, validates
69
+ their metadata and Pydantic models, then generates an `app/` directory containing:
70
+
71
+ - public service routes
72
+ - optional local development routes for individual functions
73
+ - request validation through Pydantic
74
+ - service sequencing
75
+ - dependency and request-context containers
76
+ - standard error responses
77
+ - health checks
78
+ - OpenAPI documentation
79
+ - generated smoke tests
80
+
81
+ The generated application is ordinary Python code. After generation, run it with
82
+ Uvicorn or edit the generated app as part of your application workflow.
83
+
84
+ ## End-to-End Flow
85
+
86
+ The complete flow is:
87
+
88
+ ```text
89
+ functions/**/*.py
90
+ |
91
+ | @function metadata + type annotations
92
+ v
93
+ discovery and import
94
+ |
95
+ | signature, models, steps, routes, dependencies
96
+ v
97
+ validation
98
+ |
99
+ | generated Python modules and manifest
100
+ v
101
+ app/ directory
102
+ |
103
+ | FastAPI startup imports generated routers
104
+ v
105
+ HTTP API + OpenAPI
106
+ ```
107
+
108
+ ### 1. Write a business function
109
+
110
+ Business functions live under `functions/`. Every function exposed by the builder
111
+ uses the same three-argument contract:
112
+
113
+ ```python
114
+ def business_function(payload, deps, context) -> OutputModel:
115
+ ...
116
+ ```
117
+
118
+ - `payload` is a Pydantic input model.
119
+ - `deps` is the generated dependency container.
120
+ - `context` contains request metadata and the current service execution context.
121
+ - The return annotation is a Pydantic output model.
122
+
123
+ The function may be synchronous or asynchronous.
124
+
125
+ ### 2. Decorate it with service metadata
126
+
127
+ ```python
128
+ from pydantic import BaseModel
129
+ from api_builder_runtime import function
130
+
131
+
132
+ class EchoInput(BaseModel):
133
+ message: str
134
+
135
+
136
+ class EchoOutput(BaseModel):
137
+ message: str
138
+
139
+
140
+ @function(service="echo", step=1)
141
+ def echo(payload: EchoInput, deps, context) -> EchoOutput:
142
+ return EchoOutput(message=payload.message)
143
+ ```
144
+
145
+ The decorator records the metadata needed by the builder:
146
+
147
+ - `service`: the public service name
148
+ - `step`: execution order inside the service
149
+ - `description`: optional function description
150
+ - `dependencies`: optional named dependencies
151
+ - `errors`: optional application error codes and HTTP statuses
152
+
153
+ The decorator does not execute the function. It marks the function for discovery.
154
+
155
+ ### 3. Run the builder
156
+
157
+ From the root of the project containing `functions/`:
158
+
159
+ ```bash
160
+ api-builder
161
+ ```
162
+
163
+ Useful options:
164
+
165
+ ```bash
166
+ api-builder --check # validate without writing app/
167
+ api-builder --force # overwrite generated files
168
+ api-builder --service echo # generate one service
169
+ api-builder --no-dev-routes # omit local function routes
170
+ api-builder --project-root ./demo # build another project
171
+ ```
172
+
173
+ ### 4. Run the generated API
174
+
175
+ ```bash
176
+ uvicorn app.main:app --reload
177
+ ```
178
+
179
+ The public route for the `echo` service is:
180
+
181
+ ```bash
182
+ curl -X POST http://127.0.0.1:8000/api/v1/echo \
183
+ -H 'content-type: application/json' \
184
+ -d '{"message":"hello"}'
185
+ ```
186
+
187
+ The response uses the generated standard envelope:
188
+
189
+ ```json
190
+ {
191
+ "data": {
192
+ "message": "hello"
193
+ }
194
+ }
195
+ ```
196
+
197
+ OpenAPI is available at `/docs` and `/openapi.json`. Health status is available at
198
+ `/healthz`.
199
+
200
+ ## Multi-Step Services
201
+
202
+ A service can contain multiple functions. Steps run in ascending order, and the
203
+ output of each step becomes the input for the next step.
204
+
205
+ ```python
206
+ class ReadInput(BaseModel):
207
+ file_reference: str
208
+
209
+
210
+ class ReadOutput(BaseModel):
211
+ text: str
212
+
213
+
214
+ class SummarizeInput(BaseModel):
215
+ text: str
216
+
217
+
218
+ class SummarizeOutput(BaseModel):
219
+ summary: str
220
+
221
+
222
+ @function(service="document", step=1)
223
+ async def read_document(
224
+ payload: ReadInput, deps, context
225
+ ) -> ReadOutput:
226
+ return ReadOutput(text="Document contents")
227
+
228
+
229
+ @function(service="document", step=2)
230
+ async def summarize_document(
231
+ payload: SummarizeInput, deps, context
232
+ ) -> SummarizeOutput:
233
+ return SummarizeOutput(summary=payload.text)
234
+ ```
235
+
236
+ The builder validates that:
237
+
238
+ - steps are unique
239
+ - steps are contiguous, starting at `1`
240
+ - required fields needed by the next step are produced by the previous step
241
+ - generated route paths and operation IDs are unique
242
+
243
+ The public route remains one service route:
244
+
245
+ ```text
246
+ POST /api/v1/document
247
+ ```
248
+
249
+ ## Generated Project Layout
250
+
251
+ Running `api-builder` creates this shape:
252
+
253
+ ```text
254
+ app/
255
+ ├── config.py # environment-backed settings
256
+ ├── context.py # request execution context
257
+ ├── dependencies.py # named dependency container
258
+ ├── errors.py # standard exception mapping
259
+ ├── health.py # GET /healthz
260
+ ├── main.py # FastAPI application factory
261
+ ├── middleware.py # request middleware
262
+ ├── responses.py # response envelopes
263
+ ├── routers/
264
+ │ ├── public.py # service routes
265
+ │ └── dev.py # local function routes
266
+ ├── services/ # generated service orchestration
267
+ ├── generated/
268
+ │ ├── function_registry.py # discovered function registry
269
+ │ ├── service_registry.py # service registry
270
+ │ ├── manifest.py # runtime metadata
271
+ │ └── manifest.json # inspectable metadata
272
+ └── tests/ # generated health/OpenAPI tests
273
+ ```
274
+
275
+ Generated files contain a header identifying them as generated. Treat the decorated
276
+ functions under `functions/` as the source of truth and regenerate after changing
277
+ their metadata or type models.
278
+
279
+ ## Development Routes
280
+
281
+ When the environment is `local`, `development`, or `test`, API Builder exposes one
282
+ route per decorated function:
283
+
284
+ ```text
285
+ POST /__dev/functions/{service}/{function}
286
+ ```
287
+
288
+ These routes require the `X-Internal-Dev-Token` header. They are excluded from the
289
+ OpenAPI schema and application entirely in production environments.
290
+
291
+ Use them to exercise a single step while developing a service. The public service
292
+ route remains the normal integration boundary.
293
+
294
+ ## Errors
295
+
296
+ Business functions can raise `FunctionError`:
297
+
298
+ ```python
299
+ from api_builder_runtime import FunctionError
300
+
301
+
302
+ raise FunctionError("EMPTY_DOCUMENT", "The document contains no readable text.")
303
+ ```
304
+
305
+ Declare an error status on the decorator when a custom HTTP status is needed:
306
+
307
+ ```python
308
+ @function(
309
+ service="document",
310
+ step=2,
311
+ errors={"EMPTY_DOCUMENT": 422},
312
+ )
313
+ async def summarize_document(payload, deps, context):
314
+ ...
315
+ ```
316
+
317
+ The generated error handler returns a stable API error shape and avoids exposing
318
+ internal tracebacks or implementation details to clients.
319
+
320
+ ## Validation Rules
321
+
322
+ Build-time validation fails fast when the project contains an invalid function:
323
+
324
+ - missing `functions/` directory
325
+ - no decorated functions
326
+ - wrong function signature
327
+ - missing or invalid Pydantic input model
328
+ - missing or invalid Pydantic output model
329
+ - invalid service or dependency names
330
+ - duplicate or missing service steps
331
+ - incompatible adjacent step models
332
+ - duplicate routes or operation IDs
333
+
334
+ Use `api-builder --check` in CI to validate source functions without changing the
335
+ working tree.
336
+
337
+ ## Example
338
+
339
+ The repository includes a two-step PDF summary example at:
340
+
341
+ ```text
342
+ examples/basic/functions/pdf_summary.py
343
+ ```
344
+
345
+ Build it with:
346
+
347
+ ```bash
348
+ api-builder --project-root examples/basic --force
349
+ ```
350
+
351
+ ## Library API
352
+
353
+ The builder can also be called from Python:
354
+
355
+ ```python
356
+ from pathlib import Path
357
+ from api_builder import build
358
+
359
+
360
+ result = build(Path("."), force=True)
361
+ for route in result.routes:
362
+ print(route.method, route.path)
363
+ ```
364
+
365
+ The runtime decorator is intentionally small and has no application-global registry.
366
+ Discovery happens when the builder scans the current project.
367
+
368
+ ## Local Development
369
+
370
+ ```bash
371
+ python -m venv .venv
372
+ source .venv/bin/activate
373
+ pip install -e '.[dev]'
374
+ pytest
375
+ ruff check src tests examples/basic/functions
376
+ mypy src tests
377
+ python -m build
378
+ python -m twine check dist/*
379
+ ```
380
+
381
+ ## Scope
382
+
383
+ API Builder generates application structure and validates the contract between
384
+ decorated functions. It does not diagnose infrastructure failures, determine root
385
+ cause, repair runtime systems, or create external service clients automatically.
386
+ Dependencies are named in function metadata and represented in the generated app;
387
+ application owners decide how those dependencies are implemented.
388
+
389
+ ## License
390
+
391
+ MIT. See the project metadata for the current author and package version.
@@ -3,7 +3,7 @@ api_builder/__main__.py,sha256=bYt9eEaoRQWdejEHFD8REx9jxVEdZptECFsV7F49Ink,30
3
3
  api_builder/cli.py,sha256=4JKxdbKyiXyYclXE58m4iaAbdQZe45sak3_fwogG-O8,870
4
4
  api_builder/generator.py,sha256=44m3HW77MHGm-GZGuHQfmZboUmf6ZWFCyM8OFVJcl0g,28069
5
5
  api_builder_runtime/__init__.py,sha256=d7rCDFTjGATRfnm_wJl-Hhgu12wCunIJWQgzytkW4nM,1203
6
- function_api_builder-0.1.0.dist-info/METADATA,sha256=DkSnrw39qfvphxFEcHJggt7T5XAftxrMOtTrs-1KWI0,2109
7
- function_api_builder-0.1.0.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
8
- function_api_builder-0.1.0.dist-info/entry_points.txt,sha256=r80gTGMlJgvD0x46XQRjM9Bd-Dn09OMge23CkzAA5QU,53
9
- function_api_builder-0.1.0.dist-info/RECORD,,
6
+ function_api_builder-0.1.1.dist-info/METADATA,sha256=hXgRsppFhrp4kslu9kq9NizjS8PJfwpaokWtEIJVyrc,10507
7
+ function_api_builder-0.1.1.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
8
+ function_api_builder-0.1.1.dist-info/entry_points.txt,sha256=r80gTGMlJgvD0x46XQRjM9Bd-Dn09OMge23CkzAA5QU,53
9
+ function_api_builder-0.1.1.dist-info/RECORD,,
@@ -1,72 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: function-api-builder
3
- Version: 0.1.0
4
- Summary: Generate complete FastAPI applications from decorated Python business functions.
5
- Author: Sushruth Samson
6
- License-Expression: MIT
7
- Keywords: api-builder,api-generator,backend,code-generation,fastapi,pydantic
8
- Classifier: Development Status :: 3 - Alpha
9
- Classifier: Environment :: Console
10
- Classifier: Framework :: FastAPI
11
- Classifier: Intended Audience :: Developers
12
- Classifier: License :: OSI Approved :: MIT License
13
- Classifier: Operating System :: OS Independent
14
- Classifier: Programming Language :: Python :: 3
15
- Classifier: Programming Language :: Python :: 3.11
16
- Classifier: Programming Language :: Python :: 3.12
17
- Classifier: Programming Language :: Python :: 3.13
18
- Classifier: Topic :: Software Development :: Code Generators
19
- Requires-Python: >=3.11
20
- Requires-Dist: fastapi>=0.115
21
- Requires-Dist: pydantic>=2.7
22
- Requires-Dist: uvicorn>=0.30
23
- Provides-Extra: dev
24
- Requires-Dist: build>=1.2; extra == 'dev'
25
- Requires-Dist: httpx>=0.27; extra == 'dev'
26
- Requires-Dist: mypy>=1.13; extra == 'dev'
27
- Requires-Dist: pytest>=8; extra == 'dev'
28
- Requires-Dist: ruff>=0.8; extra == 'dev'
29
- Requires-Dist: twine>=6; extra == 'dev'
30
- Description-Content-Type: text/markdown
31
-
32
- # API Builder
33
-
34
- Generate a FastAPI application from decorated Python business functions.
35
-
36
- ## Install
37
-
38
- ```bash
39
- pip install function-api-builder
40
- ```
41
-
42
- ## Use
43
-
44
- Create `functions/example.py` in your project:
45
-
46
- ```python
47
- from pydantic import BaseModel
48
- from api_builder_runtime import function
49
-
50
-
51
- class Input(BaseModel):
52
- value: str
53
-
54
-
55
- class Output(BaseModel):
56
- result: str
57
-
58
-
59
- @function(service="example", step=1)
60
- def run(payload: Input, deps, context) -> Output:
61
- return Output(result=payload.value)
62
- ```
63
-
64
- Then run:
65
-
66
- ```bash
67
- api-builder
68
- ```
69
-
70
- The builder writes a complete `app/` FastAPI application with a public service route, optional local development routes, health checks, OpenAPI, and generated tests.
71
-
72
- The function signature must be `(payload, deps, context)`, with Pydantic models for the payload and return value. See `examples/basic/functions/pdf_summary.py` for a multi-step service.