Package: simio
SimioSmall, simple and async python rest api web framework based on aiohttp.
Supports auto swagger generation and background workers. All you need to do is configure config!You can see example of application with simiohere.Start with simio:Install simiopip install simioStart projectmkdir my-project && cd my-project
simio create-app
>>> Your project name: myprojTutorial:Simio is very simple! Here is some examples:Run your applicationAll you need to run your application is:Get configconfig=get_config()Create app builderfromsimio.appimportAppBuilderbuilder=AppBuilder(config)Build and run appapp=builder.build_app(config)app.run()HandlerHandlers in simio is just functions
Just addrouter.handlerdecorator to your handlerData from request you can mark withRtype hint. It's path, query and body.If you need clients or any variables in your, use type hint such as:Depends,Provide,VarThis arguments will be auto injected on app startupIf you need custom request validator, you can changerequest_validator_clswith your own class that hasAbstractRequestValidatorinterface.You can also change server response on request validation failure with argumenton_exception_responseimporttrafaretastfromsimioimportrouter,R,webfromsimio_diimportDependsfrompymongoimportMongoClientRequestSchema=t.Dict({t.Key("some_number"):t.ToInt(gte=0)})@router.post("/v1/hello/{user_id}/")asyncdefsimple_post_handler(example:R[RequestSchema],user_id:R[int],client:Depends[MongoClient]):client.db.coll.insert_one({"status":"alive"})returnweb.json_response({"id":user_id,"some_number":example["some_number"],})@router.get("/v1/hello/{user_id}/")asyncdefget(user_id:R[int]):returnweb.json_response(f"Your id is{user_id}!")SwaggerJust run your app and open:0.0.0.0:8080/api/docYou can find raw json file in your project directorySwagger generation can be disabled in configWorkers and CronsAny async function that hasappargument can be worker or cronYou can access logger, clients and everything else from appFor client access you can use same syntax for dependency injectionAt this moment supported only async workers/crons, but you can easily create your own job executors
Just create class with AbstractExecutorfromsimio.jobimportasync_worker,async_cron@async_worker.register()asyncdefping_worker(app:web.Application):app.logger.info('Work')awaitasyncio.sleep(sleep_time)@async_cron.register(cron="*/1 * * * *")asyncdefcron_job(app:web.Application,client:Depends[MongoClient]):awaitclient.db.collection.insert({"status":"alive"})ClientsAs we said earlier, simio uses dependency injection, so everything you need to use them is just add type hint
For client configuration use app configdefget_config():return{AppConfig:{AppConfig.name:"simio_app",},MongoClient:{"host":"mongodb://localhost:27017"},}And that's all!TestingAs you see, everything in simio is just a functions, so you can write easy unit tests for them@router.post('/v1/{user_id}/handle')asyncdefhandler(user_id:R[int],client:Depends[MongoClient]):awaitclient.db.coll.insert_one({"user":user_id})returnweb.json_response({"status":"ok"})asyncdeftest_handler():result=awaithandler(1,MagicMock())assert...!! This is 0.x version, so be ready for major updates in minor version !!