Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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

Release files for aiohttp-extracts 0.0.1a1

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

Source distribution (sdist)

Source distribution for aiohttp-extracts 0.0.1a1
File Size Uploaded
aiohttp-extracts-0.0.1a1.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiohttp-extracts 0.0.1a1
File Interpreter ABI Platform
aiohttp_extracts-0.0.1a1-py3-none-any.whl Python 3 none any Details

Total release size: 16.9 kB

Release files / aiohttp-extracts-0.0.1a1.tar.gz

Download URL aiohttp-extracts-0.0.1a1.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
03ba8cada56059268531b5bccdc7c29fcb5724b2a2eacd13628396a51991d2d7
BLAKE2b-256 checksum
How to use checksums
df575569c70be03acffbf124daeab558457c2610c08c13ef02e151bbaa2b06f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.5

Release files / aiohttp_extracts-0.0.1a1-py3-none-any.whl

Download URL aiohttp_extracts-0.0.1a1-py3-none-any.whl
Size 13.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db35f60c5850319f6793b0f663a09ea2942e3c6792061d27097c9c8faee78696
BLAKE2b-256 checksum
How to use checksums
d93fa8676616717b1273b4c95d6120fda0221d1baf57210ee362172446ee2243
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

0.0.1a1 This release

2 release files

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