Skip to main content

No project description provided

Project description

pydantic_aiohttp - Symbiosis of Pydantic and Aiohttp

PyPI version shields.io PyPI pyversions PyPI license

This repository provides simple HTTP Client based on aiohttp with integration of pydantic

Examples

Basic example

import asyncio

import pydantic

from pydantic_aiohttp import Client


class HelloWorldResponse(pydantic.BaseModel):
    hello: str


async def main():
    example_client = Client("https://api.example.com")

    response = await example_client.get("/hello", response_model=HelloWorldResponse)
    print(response.hello)

    # After all your work is done you should close client (this method closes aiohttp session instance)
    await example_client.close()


if __name__ == '__main__':
    asyncio.run(main())

Handling errors parsed as pydantic models

import asyncio

import pydantic

import pydantic_aiohttp
from pydantic_aiohttp import Client


class FastAPIValidationError(pydantic.BaseModel):
    loc: list[str]
    msg: str
    type: str


class FastAPIUnprocessableEntityError(pydantic.BaseModel):
    detail: list[FastAPIValidationError]


class User(pydantic.BaseModel):
    id: str
    email: str
    first_name: str
    last_name: str
    is_admin: bool


async def main():
    client = Client(
        "https://fastapi.example.com",
        error_response_models={
            pydantic_aiohttp.status.HTTP_422_UNPROCESSABLE_ENTITY: FastAPIUnprocessableEntityError
        }
    )

    try:
        # Imagine, that "email" field is required for this route
        await client.post(
            "/users",
            body={
                "first_name": "John",
                "last_name": "Doe"
            },
            response_model=User
        )
    except pydantic_aiohttp.HTTPUnprocessableEntity as e:
        # response field of exception now contain parsed pydantic model entity 
        print(e.response.detail[0].json(indent=4))
        # >>>
        # {
        #     "loc": [
        #         "body",
        #         "email"
        #     ],
        #     "msg": "field required",
        #     "type": "value_error.missing"
        # }

    await client.close()


if __name__ == '__main__':
    asyncio.run(main())

LICENSE

This project is licensed under the terms of the MIT license.

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

pydantic_aiohttp-0.1.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

pydantic_aiohttp-0.1.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_aiohttp-0.1.1.tar.gz.

File metadata

  • Download URL: pydantic_aiohttp-0.1.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.6 Linux/5.13.0-1022-azure

File hashes

Hashes for pydantic_aiohttp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a37c5b015d9d469bb8242e38323bedfda3e1791f9752666579c3ea9cccf7120b
MD5 4dabe47a0bc8d1b9e79da1ca8abb9d7c
BLAKE2b-256 35e1acff18701c3f3c6ea00e33b659e47b7ae56f60209f7267363e45393df456

See more details on using hashes here.

Provenance

File details

Details for the file pydantic_aiohttp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pydantic_aiohttp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.6 Linux/5.13.0-1022-azure

File hashes

Hashes for pydantic_aiohttp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2e0ae7d162efce3ead0175e301623168601fe7cb261d3f6a2213ca86bf88609a
MD5 b3d96b20f7fb094062f8fdb8f1b8de85
BLAKE2b-256 6729b6b82031bad3441e70df312b0c81ed53d23cdc3a358047f4782c53bb0e0c

See more details on using hashes here.

Provenance

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