AIOAPI¶
AIOAPI
is a library for building APIs with AIOHTTP
framework and Python 3.7+ based on standard Python type hints.
Install¶
Just:
1 | $ pip install aioapi |
Requirements¶
AIOAPI
depends on AIOHTTP
framework and tries to extend the view layer in the right way, also AIOAPI
depends on pydantic
— a great data validation library.
At a glance¶
Look at simple application below and pay attention to the highlighted lines to see the power of AIOAPI
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | from http import HTTPStatus from uuid import UUID import aioapi as api from aioapi import Body, PathParam from aiohttp import web from pydantic import BaseModel class Database: async def get_user(self, *, user_id): ... async def create_user(self, *, user_id, name, age): ... class User(BaseModel): user_id: UUID name: string age: int = 42 async def get_user(app: web.Application, user_id: PathParam[UUID]): user = await app["db"].get_user(user_id=user_id) return web.json_response( {"user_id": user.user_id, "name": user.name, "age": user.age} ) async def create_user(app: web.Application, body: Body[User]) user = body.cleaned await app["db"].create_user( user_id=user.user_id, name=user.name, age=user.age ) return web.Response(status=HTTPStatus.CREATED) def main(): app = web.Application() app["db"] = Database() app.add_routes([ api.post("/users", create_user), api.get("/users/{user_id}", get_user), ]) web.run_app(app) if __name__ == "__main__": main() |
That simple example shows you how AIOAPI
can help you to simplify your daily routine with data serialization and validation in APIs. As you can see you need just to define the right types and AIOAPI
will do all other job for you.
Looks interesting for you? Go ahead and explore documentation, AIOAPI
can surprise you!