Package: fastipy
FastipyWhat is it and what is it forFastipyis a fast and easy-to-use open source Python library for developing RESTful APIs.Powered byuvicornInstallationpipinstallfastipyExamplesExample for GET Route with Query Params and debug modefromfastipyimportFastipy,Request,Reply# Debug mode is False by defaultapp=Fastipy()# Routes can be async or sync functions, but reply send functions are async# The handler returns the default HTTP status code 200@app.get('/')defhome(req:Request,reply:Reply):# Get query params ageage=req.query['age']# Example: Recovery all persons from database with this age and print the htmlprint("<h1>Listing all persons</h1><ul><li>A Person</li></ul>")Example for GET Route with Params, CORS and multiple methodsfromfastipyimportFastipy,Request,Replyapp=Fastipy().cors()@app.get('/user/:id')@app.post('/user/:id')asyncdefgetUser(req:Request,reply:Reply):# get users from databaseforiinusers:ifi["id"]==req.params["id"]:awaitreply.json(i).send()returnawaitreply.send_code(404)Example for POST Route with BodyfromfastipyimportFastipy,Request,Replyapp=Fastipy()@app.post('/user')asyncdefcreateUser(req:Request,reply:Reply):user=req.body.json# Save user in databaseawaitreply.text("Created").code(201).send()Example for PUT Route with BodyfromfastipyimportFastipy,Request,Replyapp=Fastipy()@app.put('/user')asyncdefcreateUser(req:Request,reply:Reply):user=req.body.json# Update user in databaseawaitreply.html('<h1>Created</h1>').code(201).send()See more examples inexamplesfolderCreating plugins# chat.pyfromfastipyimportFastipyInstance,Reply# Plugins can be asynchronous or synchronized functions# Plugins have the main instance as a parameter, which means they can use all of Fastipy's functionsdefchatRoutes(app:FastipyInstance,options:dict):@app.get('/')asyncdefindex(req:Request,reply:Reply):awaitreply.send_code(200)@app.get('/chat')asyncdeftest(req:Request,reply:Reply):awaitreply.send_code(200)# message.pyfromfastipyimportFastipyInstance,ReplyasyncdefmessageRoutes(app:FastipyInstance,options:dict):@message.get('/')asyncdefindex(req:Request,reply:Reply):awaitreply.send_code(200)@message.get('/message')asyncdeftest(req:Request,reply:Reply):awaitreply.send_code(200)app.name('custom plugin name')# main.pyfromfastipyimportFastipyfrommessageimportmessageRoutesfromchatimportchatRoutesapp=Fastipy().cors()app.register(messageRoutes,{'prefix':'/message'})app.register(chatRoutes,{'prefix':'/chat'})HooksfromfastipyimportFastipy,Request,Replyapp=Fastipy()# The preHandler hook is called before the request handler@app.hook('preHandler')defpreHandler(req:Request,reply:Reply):print('onRequest hook')# The onRequest hook is called when the request is handled@app.hook('onRequest')defonRequest(req:Request,reply:Reply):print('onRequest hook')# The onResponse hook is called when the reply sends a response@app.hook('onResponse')defonResponse(req:Request,reply:Reply):print('onResponse hook')# The onError hook is called when an error occurs@app.hook('onError')defonError(req:Request,reply:Reply,error):print('onError hook')# A hook will only be linked to a route if its declaration precedes the route# The order of hooks of the same type is important@app.get('/')asyncdefindex(req:Request,reply:Reply):awaitreply.send_code(200)RunningRun Fastipy application in development is easyimportuvicornif__name__=="__main__":# main:app indicates the FILE:VARIABLE# The file is the main file where Fastipy() is instantiated# The variable is the name of the variable that contains the instance of Fastipy()# You can find more configurations here https://www.uvicorn.org/config=uvicorn.Config('main:app',log_level='debug',port=3000)server=uvicorn.Server(config)server.run()Application DeployFor production deployment, please refer to thisuvicorn guide.Change LogVersion 1.5.0AddedRequest method PATCHMore security in CORSJSON Database moduleAdd support to hooks (onRequest, onResponse, onError)Add support to pluginsAdd decorators to routes (decorator, decorate_request, decorate_reply)Add support to middlewaresAdd support for lifespan events (startup, shutdown)New preHandler hookAdd stream file supportAutomatic OPTIONS method response for routesCustom global error handlerChangedBetter params and query params recovery in routesImproved route loggingRefactor structure of projectRefactor Routes classRoutes handler can be a sync functionBetter route search algorithm and structureIt is now possible to add specific hooks and middlewares to a routeImplementation of uvicorn HTTP web serverBetter error handlerContributorsHow to ContributingOpen pull request ðŸ˜Ž