A simple decorator to convert Python functions into FastAPI endpoints
Project description
API AutoGen
Overview
API AutoGen is a Python package that provides a simple way to convert functions into FastAPI endpoints using a decorator.
Installation
To install the package, run:
pip install git+https://github.com/lechplace/api_autogen.git
Example Usage
1. Creating API Endpoints with the @api_func Decorator
Define your functions and use the @api_func decorator to automatically expose them as API endpoints.
Example:
from api_autogen import api_func, app
from pydantic import BaseModel
@api_func("math_funcs")
def add(a: int, b: int) -> int:
return a + b
class MultiplyInput(BaseModel):
a: int
b: int
@api_func("math_funcs")
def multiply(data: MultiplyInput) -> int:
return data.a * data.b
2. Where the Decorator is Used
- The
@api_func("math_funcs")decorator is applied to functions (addandmultiply). - It registers these functions as API endpoints under the path
/math_funcs/addand/math_funcs/multiply.
3. Where the FastAPI App is Defined
- The FastAPI application instance
appis provided inapi_autogenand should be used in the main application script.
4. Running the API Server
Start the FastAPI application using Uvicorn:
uvicorn api_autogen:app --reload
- The server will start at
http://127.0.0.1:8000. - Navigate to
http://127.0.0.1:8000/docsto explore the auto-generated API documentation.
Project Structure
api_autogen/
│── __init__.py # Exposes api_func and app
│── decorator.py # Implements the decorator logic
setup.py # Package setup file
README.md # Documentation
Summary
- Use
@api_func(package_name)to convert functions into API endpoints. - The FastAPI app is provided in
api_autogen. - Run the server with
uvicorn api_autogen:app --reload. - API documentation is available at
/docs.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
api_autogen-0.1.0.tar.gz
(2.8 kB
view details)
File details
Details for the file api_autogen-0.1.0.tar.gz.
File metadata
- Download URL: api_autogen-0.1.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
032dbc544a23b7922c391ab54ca5100d3acd51cca5958c8c7f196a444696876c
|
|
| MD5 |
31eeb1dbc010df3e22a350bc9fc92a5a
|
|
| BLAKE2b-256 |
e7e3046da0bfb2cbd48cee4e6b5902d31004ec55530bd7c9b59076cb6701f56b
|