Skip to content

ponytailer/pydantic-client

Repository files navigation

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.

About

Http client base pydantic, with requests, aiohttp and httpx

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages