Skip to main content

aiohttp simple pydantic validator

Project description

aiohttp-validator

Downloads License Python Versions

aiohttp simple pydantic http request validator

Installation

pip install aiohttp-validator

A Simple Example

import datetime as dt
from uuid import UUID
from typing import Any, Dict, List

import pydantic
from aiohttp import web

import aiohttp_validator as validator

routes = web.RouteTableDef()


@routes.get('/posts')
@validator.validated()
async def get_posts(request: web.Request, tags: List[str], limit: pydantic.conint(gt=0, le=100), offset: int = 0):
    assert isinstance(tags, list)
    assert isinstance(limit, int)
    assert isinstance(offset, int)
    # your code here ...

    return web.Response(status=200)


class RequestHeaders(pydantic.BaseModel):
    requestId: int


class User(pydantic.BaseModel):
    name: str
    surname: str


class Post(pydantic.BaseModel):
    title: str
    text: str
    timestamp: float
    author: User
    tags: List[str] = pydantic.Field(default_factory=list)


@routes.post('/posts/{section}/{date}')
@validator.validated()
async def create_post(request: web.Request, body: Post, headers: RequestHeaders, section: str, date: dt.date):
    assert isinstance(body, Post)
    assert isinstance(headers, RequestHeaders)
    assert isinstance(date, dt.date)
    assert isinstance(section, str)
    # your code here ...

    return web.Response(status=201)


class AuthCookies(pydantic.BaseModel):
    tokenId: UUID


@routes.post('/users')
@validator.validated()
async def create_user(request: web.Request, body: Dict[str, Any], headers: RequestHeaders, cookies: AuthCookies):
    assert isinstance(body, dict)
    assert isinstance(headers, RequestHeaders)
    assert isinstance(cookies, AuthCookies)
    # your code here ...

    return web.Response(status=201)

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

web.run_app(app, port=8080)

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-validator-0.1.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

aiohttp_validator-0.1.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp-validator-0.1.2.tar.gz.

File metadata

  • Download URL: aiohttp-validator-0.1.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.2 Linux/5.11.0-1027-azure

File hashes

Hashes for aiohttp-validator-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9d3ba75bd7627e0b0030c239eb893933cf4deadae638333c027ad56bf8b8f0d9
MD5 ae532d9d65f731e75c8523be4948cd55
BLAKE2b-256 f0fb19ebcb73ae79a24c2beb4f5431b408618283b931e125afa63b8b1261a288

See more details on using hashes here.

File details

Details for the file aiohttp_validator-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_validator-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.10.2 Linux/5.11.0-1027-azure

File hashes

Hashes for aiohttp_validator-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 80b078d4ef20a9ced55acf388db9efd3f18a2eb2bc4b42e275c65cb47e9a2f6c
MD5 471116015f1a909d79be53abfb0bcff1
BLAKE2b-256 0a6481ed0b8095896c663ef2cbe61ac86e6fea4f4bf94528dfef3e77f019b8d9

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