Skip to main content

Sugar library for aiohttp handlers

Project description

aiohttp-extracts

This library allows you to extract variable number of request parameters to a handler's arguments. It uses type hints to determine where each value must be extracted from.

For example

With aiohttp-extracts:

from aiohttp import web
from aiohttp_extracts import with_extraction
from aiohttp_extracts import RequestAttr, MatchInfo, QueryAttr


routes = web.RouteTableDef()


@routes.get(r'/chats/{chat_id:(\d+)}/')
@with_extraction
async def handler(
    user: RequestAttr[int],           # by default it uses argument name
    chat: MatchInfo['chat_id', int],  # but you can specify name what you want
    offset: QueryAttr[int] = 0,       # and you can simply set a default value
    count: QueryAttr[int] = 100
) -> web.Response:
    ...

Without aiohttp-extracts:

from aiohttp import web
from aiohttp_extracts import with_extraction


routes = web.RouteTableDef()


@routes.get(r'/chats/{chat_id:(\d+)}/')
async def handler(request: web.Request):
    user = request.get('user')
    chat = request.match_info.get('chat_id')
    offset = request.query.get('offset', 0)
    count = request.query.get('count', 100)
) -> web.Response:
    ...

Installation

Installation process as simple as:

$ pip install aiohttp-extracts

Usage

First we need to set a middleware to app.

from aiohttp import web
from aioservertiming import server_timing_mware

app = web.Applicalion(
    middlewares = [
        server_timing_mware
    ]
)

Usual handler

from aiohttp import web
from aiohttp_extracts import with_extraction
from aiohttp_extracts import RequestAttr, MatchInfo, QueryAttr


@with_extraction
async def handler(
    user: RequestAttr[int],
    chat: MatchInfo['chat_id', int],
    offset: QueryAttr[int] = 0,
    count: QueryAttr[int] = 100
) -> web.Response:
    ...

Classview

With decorator

from aiohttp import web
from aiohttp_extracts import with_extraction
from aiohttp_extracts import RequestAttr, MatchInfo, QueryAttr


@extract_classview
class ChatView(web.View):

    async def get(
        user: RequestAttr[int],
        chat: MatchInfo['chat_id', int],
        offset: QueryAttr[int] = 0,
        count: QueryAttr[int] = 100
    ) -> web.Response:
        ...

With metaclass

from aiohttp import web
from aiohttp_extracts import ExtractionMeta
from aiohttp_extracts import RequestAttr, MatchInfo, QueryAttr


class ChatView(web.View, metaclass=ExtractionMeta):

    async def get(
        user: RequestAttr[int],
        chat: MatchInfo['chat_id', int],
        offset: QueryAttr[int] = 0,
        count: QueryAttr[int] = 100
    ) -> web.Response:
        ...

Types that can be used in handlers args

Type name What it replaces Additional info
aiohttp.web.Request request Usually request object
aiohttp_extracts.RequestAttr request.get(...) Any request attribute
aiohttp_extracts.MatchInfo request.match_info.get(...)
aiohttp_extracts.QueryAttr request.query.get(...)
aiohttp_extracts.Header request.headers.get(...)
aiohttp_extracts.Cookie request.cookies.get(...)
aiohttp_extracts.JSONBody await request.json()

Links

This library on PyPI

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp-extracts-0.0.1a1.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

aiohttp_extracts-0.0.1a1-py3-none-any.whl (13.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page