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.

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
from pydantic_client.clients.requests import RequestsClient


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


class R(RequestsClient):

    @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:
        ...


my_client = R("http://localhost/v1")
get_book: Book = my_client.get_book(1)

The Group

from pydantic_client import Group
from pydantic_client.clients.requests import RequestsClient

group = Group("/book")
person_group = Group("/person")


class GroupClient(RequestsClient):
    def __init__(self):
        super().__init__("http://localhost")

    @group.get("/{book_id}")
    def get(self, book_id: int) -> Book:  # type: ignore
        ...

    @person_group.get("/{person_id}")
    def get_person(self, person_id: int) -> Person:  # type: ignore
        ...


client = GroupClient()
book = client.get(1)
person = client.get_person(1)

change log

v0.1.14: add global or request level headers

# global level headers
my_client = R("http://localhost/v1", headers={"Authorization": "xxxxxxx"})

# request level headers, and its priority is higher than global. 

# header should be xxxxxxx
my_client.delete(1)
# header should be zzzzz
my_client.get(1, request_headers={"Authorization": "zzzzz"})
# header should be yyyyy
my_client.post(1, request_headers={"Authorization": "yyyyy"})

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-0.1.14.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

pydantic_client-0.1.14-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_client-0.1.14.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for pydantic_client-0.1.14.tar.gz
Algorithm Hash digest
SHA256 40d3a24cc3f9ee7ea351a487ae6a4fb7e2e3c6c8c3b3bcb83c1fb8d6a31cab52
MD5 f5ad0abcef503258e2ba3ab66deb47a8
BLAKE2b-256 5678379039e4c80de16cef337a5e84a4bc3209a9cc044c5135ce84d82b38ec7e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for pydantic_client-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 5dc027045de316310585203b1a47f8cc20d3cb0b89f592dd642d04b0f452177b
MD5 2cdbb3f28d707113c83710014c315fa4
BLAKE2b-256 327c68f1e55f223b9822e349da85e7de64077bd1ff156409abfd891da4da8b63

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