Coder Social home page Coder Social logo

aiohug

version pipeline status coverage report

Goals:

  • Unpack aiohttp (>=3.1) request to arguments with annotations
  • Validate handlers arguments
  • Generate swagger specification

Posts:

Examples

Arguments from path and query

from aiohttp import web
from aiohug import RouteTableDef
from marshmallow import fields

routes = RouteTableDef()


@routes.get("/hello/{name}/")
async def hello(name: fields.String(), greeting: fields.String() = "Hello"):
    return {"msg": f"{greeting}, {name}"}


app = web.Application()
app.add_routes(routes)


if __name__ == "__main__":
    web.run_app(app)

There is no request object in handler signature anymore - only required arguments.

Body with schema

from aiohttp import web
from aiohug import RouteTableDef

routes = RouteTableDef()

class PayloadSchema(Schema):
    count = fields.Int()

@routes.get("/")
async def with_body(body: PayloadSchema()):
    return body

app = create_app()
app.add_routes(routes)

client = await test_client(app)
resp = await client.get("/", json={"count": "5", "another": 7})

assert await resp.json() == {"count": 5}

Another shortcuts

@routes.post("/ping/")
async def ping():
  return 201, "pong"

Swagger

Use aiohug_swagger package.

Decorators

Because of the way aiohttp routing works all decorators to resource handlers must be applied BEFORE aiohug's routing decorator, i.e.

def some_decorator(func):

 @wraps(func)
 def wrapper(request, *args, **kwargs):
     # Some logic for decorator
     return func(*args, **kwargs)

 return wrapper


 @routes.get("/ping/")
 @some_decorator
 async def hello():
     return "pong"

Moreover, make sure to decorate wrapper functions with wraps decorator from functools module - otherwise aiohug won't be able to access original handler's arguments and annotations.

Why aiohug?

It's just hug API implementation for aiohttp

TODO:

  • don’t pass default arguments

aiohug's Projects

aiohug icon aiohug

aiohug provide convenient API for handler registration in aiohttp. Swagger (OpenAPI Specification) from the box.

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.