Coder Social home page Coder Social logo

gr1n / aioapi Goto Github PK

View Code? Open in Web Editor NEW
13.0 5.0 2.0 865 KB

Yet another way to build APIs using AIOHTTP framework

Home Page: https://gr1n.github.io/aioapi/

License: MIT License

Python 96.30% Makefile 3.70%
aiohttp aiohttp-server typing annotations api apis

aioapi's Introduction

aioapi

Build Status codecov PyPI PyPI - Downloads GitHub

Yet another way to build APIs using AIOHTTP framework.

Follow documentation to know what you can do with AIOAPI.

Installation

$ pip install aioapi

Usage & Examples

Below you can find a simple, but powerful example of AIOAPI library usage:

import aioapi as api
from aioapi import Body, PathParam
from aioapi.middlewares import validation_error_middleware
from aiohttp import web
from pydantic import BaseModel


class User(BaseModel):
    name: str
    age: int = 42


async def hello_body(user_id: PathParam[int], body: Body[User]):
    user = body.cleaned
    return web.json_response(
        {"id": user_id.cleaned, "name": user.name, "age": user.age}
    )


def main():
    app = web.Application()

    app.add_routes([api.post("/hello/{user_id}", hello_body)])
    app.middlewares.append(validation_error_middleware)

    web.run_app(app)


if __name__ == "__main__":
    main()

And there are also more examples of usage at examples/ directory.

To run them use command below:

$ make example

Contributing

To work on the AIOAPI codebase, you'll want to clone the project locally and install the required dependencies via poetry:

$ git clone [email protected]:Gr1N/aioapi.git
$ make install

To run tests and linters use command below:

$ make lint && make test

If you want to run only tests or linters you can explicitly specify what you want to run, e.g.:

$ make lint-black

Milestones

If you're interesting in project's future you can find milestones and plans at projects page.

License

AIOAPI is licensed under the MIT license. See the license file for details.

aioapi's People

Contributors

gr1n avatar pyup-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

mlaradji

aioapi's Issues

Path parameters

In aiohttp resources may have variables, for example:

@routes.get('/{name}')
async def variable_handler(request):
    return web.Response(text=f"Hello, {request.match_info['name']}")

...or:

web.get(r'/{name}', variable_handler)

And we need to implement a possibility to validate such cases.

It can be implemented in a way:

class View(TypedView):
    async def get(self, name: str):
        pass

Also, we need to think about how can we optionally declare metadata for future integration with OpenAPI.

Support CBV

Need to add support for class-based views.

At a glance:

class HelloView(web.View):
    async def get(self, request: web.Request, name: PathParam[str]):
        ...

    async def post(self, body: Body[HelloBodyRequest]):
        ...

Request body

Need to have possibility to validate request body.

At a glance:

class Request(BaseModel):
    pass

async def handle(body: tpd.Body[Request]):
    pass

Think about defaults and should we allow to have the ability to specify multiple parameters like fastapi did or not.

Initial Update

The bot created this issue to inform you that pyup.io has been set up on this repo.
Once you have closed it, the bot will open pull requests for updates as soon as they are available.

Header parameters

Need to have possibility to validate headers.

At a glance:

async def handle(locale: Header[str]):
    pass

Also, we can specify defaults:

async def handle(locale: Header[str] = "ru"):
    pass

Request object

Need to have possibility to pass request object.

At a glance:

async def handle(request: web.Request):
    pass

Query parameters

Need to have possibility to validate query parameters.

At a glance:

async def handle(name: tpd.QueryParam[str]):
    pass

Also, we can specify defaults:

async def handle(name: tpd.QueryParam[str] = "batman"):
    pass

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.