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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file aiohttp-extracts-0.0.1a1.tar.gz.

File metadata

  • Download URL: aiohttp-extracts-0.0.1a1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for aiohttp-extracts-0.0.1a1.tar.gz
Algorithm Hash digest
SHA256 03ba8cada56059268531b5bccdc7c29fcb5724b2a2eacd13628396a51991d2d7
MD5 dda99412d4be8455fdf7aee76e9fec35
BLAKE2b-256 df575569c70be03acffbf124daeab558457c2610c08c13ef02e151bbaa2b06f9

See more details on using hashes here.

File details

Details for the file aiohttp_extracts-0.0.1a1-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_extracts-0.0.1a1-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for aiohttp_extracts-0.0.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 db35f60c5850319f6793b0f663a09ea2942e3c6792061d27097c9c8faee78696
MD5 df978b62ff6365d2fb809935c1de37d1
BLAKE2b-256 d93fa8676616717b1273b4c95d6120fda0221d1baf57210ee362172446ee2243

See more details on using hashes here.

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