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, request_headers: dict = None) -> Book:
        ...


client = pydantic_client_manager.get()
# will get the book with book_id=1
book: Book = client.get_book(1)

# request_headers will overwrite the headers in client_config
client.change_book(1, request_headers={"Authorization": "Bearer abcdefg"})

And see the 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.aiohttp,
    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.4.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_client-1.0.4.tar.gz
  • Upload date:
  • Size: 10.4 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.4.tar.gz
Algorithm Hash digest
SHA256 3421c936006e746e088be018637c2368d3506a175a24f563388df251578c86e8
MD5 b9a416e7cc68a6a715ab064043bef30d
BLAKE2b-256 d7e176c5a92d098b68b52530a87f0b84a329eff5dd21504142493ef2c6ce6351

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pydantic_client-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4fffc0e291aedbef222ee5c7a11e1f319275835707d89c695f31723fdff0d618
MD5 89aaeac5504b48432f992de1ae0021e6
BLAKE2b-256 3b6c2a8147179c9c507cf40e4685bfeb538c741dbe2cf599990218392dc452f2

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