Skip to main content

Http client base pydantic, with requests or aiohttp

Project description

pydantic-client

codecov Upload Python Package

Http client base pydantic, with requests, aiohttp and httpx. Only support the json response.

How to install

only support requests:

pip install pydantic-client

support aiohttp and requests:

pip install "pydantic-client[aiohttp]"

support httpx(async) and requests:

pip install "pydantic-client[httpx]"

support all:

pip install "pydantic-client[all]"

How to use

from pydantic import BaseModel

from pydantic_client import delete, get, post, put, pydantic_client_manager
from pydantic_client import ClientConfig


class Book(BaseModel):
    name: str
    age: int


@pydantic_client_manager.register(
    ClientConfig(
        base_url="https://example.com",
        headers={"Authorization": "Bearer abcdefg"},
        timeout=10
    )
)
class WebClient:

    @get("/books/{book_id}?query={query}")
    def get_book(self, book_id: int, query: str) -> Book:
        ...

    @post("/books", form_body=True)
    def create_book_form(self, book: Book) -> Book:
        """ will post the form with book"""
        ...

    @put("/books/{book_id}")
    def change_book(self, book_id: int, book: Book) -> Book:
        """will put the json body"""
        ...

    @delete("/books/{book_id}")
    def change_book(self, book_id: int) -> Book:
        ...


client = pydantic_client_manager.get()
book: Book = client.get_book(1)

And see the examples to get more examples.

change log

v1.0.0: refactor all the code, to be simple. remove the group client.

v1.0.1: simple to use.

v1.0.2: use enum to define the client type.

v1.0.3: you can define the your own client session in client_config

import aiohttp
from pydantic_client import ClientConfig, ClientType

client_config = ClientConfig(
    client_type=ClientType.requests,
    base_url="https://example.com",
    headers={"Authorization": "Bearer abcdefg"},
    timeout=10,
    session=lambda: aiohttp.ClientSession()
)

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_client-1.0.3.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

pydantic_client-1.0.3-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_client-1.0.3.tar.gz.

File metadata

  • Download URL: pydantic_client-1.0.3.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for pydantic_client-1.0.3.tar.gz
Algorithm Hash digest
SHA256 e4949181f25eb8ad1f51fed39c9bed0a47e4b369b595f1fe44c41ce256cd3b6e
MD5 f15b7eea7963347786505cabe5caa681
BLAKE2b-256 bcb872533d3deff49626412415b28c2fc58a94e72ccb6571ea9a23151af741a7

See more details on using hashes here.

Provenance

File details

Details for the file pydantic_client-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_client-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 34e89ca1b55e7afd48ced8ec1720ac4303e4175c26565638a426adac9d18327e
MD5 6d59ab004aee4781b10d11596dee10e5
BLAKE2b-256 5e6020e4bb9a9bbb2a90fe599c023aeaa8726a93c4397ee030fac2d0c99e01e0

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