Fastipy
What is it and what is it for
Fastipy is a fast and easy-to-use open source Python library for developing RESTful APIs.
Powered by uvicorn
Installation
pip install fastipy
Examples
Example for GET Route with Query Params
from fastipy import Fastipy, Request, Reply
app = Fastipy()
@app.get("/")
def home(req: Request, _):
age = req.query["age"]
print("<h1>Retrieving all persons</h1><ul><li>A Person</li></ul>")
Example for GET Route with Params, CORS and multiple methods
from fastipy import Fastipy, Request, Reply
app = Fastipy().cors()
@app.get("/user/:id")
@app.post("/user/:id")
async def getUser(req: Request, reply: Reply):
for i in users:
if i["id"] == req.params["id"]:
return await reply.send(i)
await reply.send_code(404)
Example for POST Route with Body
from fastipy import Fastipy, Request, Reply
app = Fastipy()
@app.post("/user")
async def createUser(req: Request, reply: Reply):
user = req.body.json
await reply.code(201).send("Created")
Example for PUT Route with Body
from fastipy import Fastipy, Request, Reply
app = Fastipy()
@app.put("/user")
async def createUser(req: Request, reply: Reply):
user = req.body.json
await reply.type("text/html").code(201).send("<h1>Created</h1>")
Example for GET Route with file stream
from fastipy import Fastipy, Request, Reply
app = Fastipy()
@app.get("/stream")
async def streamFile(_, reply: Reply):
def generator():
with open("file.txt") as f:
for line in f:
yield line
await reply.send(generator())
Adding custom serializer to Reply send
from fastipy import Fastipy, Request, Reply
app = Fastipy()
app.add_serializer(
validation=lambda data: isinstance(data, str),
serializer=lambda data: ("application/json", json.dumps({"error": data})),
)
@app.get("/")
async def customSerializer(_, reply: Reply):
await reply.code(404).send("Field not found")
Running
Running Fastipy application in development is easy
import uvicorn
if __name__ == "__main__":
uvicorn.run("main:app", log_level="debug", port=8000, reload=True, loop="asyncio")
See more examples in examples folder
Creating plugins
from fastipy import FastipyInstance, Reply
def chatRoutes(app: FastipyInstance, options: dict):
@app.get("/")
async def index(_, reply: Reply):
await reply.send_code(200)
@app.get("/chat")
async def test(_, reply: Reply):
await reply.send_code(200)
from fastipy import FastipyInstance, Reply
async def messageRoutes(app: FastipyInstance, options: dict):
@message.get("/")
async def index(_, reply: Reply):
await reply.send_code(200)
@message.get("/message")
async def test(_, reply: Reply):
await reply.send_code(200)
app.name("custom plugin name")
from fastipy import Fastipy
from message import messageRoutes
from chat import chatRoutes
app = Fastipy().cors()
app.register(messageRoutes, {"prefix": "/message"})
app.register(chatRoutes, {"prefix": "/chat"})
Hooks
from fastipy import Fastipy, Request, Reply
app = Fastipy()
@app.hook("preHandler")
def preHandler(req: Request, reply: Reply):
print("onRequest hook")
@app.hook("onRequest")
def onRequest(req: Request, reply: Reply):
print("onRequest hook")
@app.hook("onResponse")
def onResponse(req: Request, reply: Reply):
print("onResponse hook")
@app.hook("onError")
def onError(error: Exception, req: Request, reply: Reply):
print(f"onError hook exception: {error}")
@app.get("/")
async def index(_, reply: Reply):
await reply.send_code(200)
End to End tests
from fastipy import TestClient
from main import app
client = TestClient(app)
response = client.post("/")
assert response.status_code == 200
assert response.text == "Hello World"
Application Deploy
For production deployment, please refer to this uvicorn guide.
Change Log
Version 1.5.3
Todo
Added
Changed
Contributors
How to Contributing
Open pull request