Skip to main content

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

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

Release files for aiohug 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiohug 0.7.1
File Size Uploaded
aiohug-0.7.1.tar.gz 7.4 kB Details

Release files / aiohug-0.7.1.tar.gz

Download URL aiohug-0.7.1.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fab1fb74d20aaf25219acf7781ad8e1d527c6f684ced4788b05393b3f8b35d26
BLAKE2b-256 checksum
How to use checksums
024ab3ddcf70b8cac31978b65f9a25af88c6a3335997893100c848967c883e57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release history Release notifications | RSS feed

This release

0.7.1 This release

1 release file

0.7

1 release file

0.6.4

1 release file

0.6.1

1 release file

0.5

1 release file

0.4.2

1 release file

0.4.0

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page