Http client base pydantic, with requests or aiohttp
Project description
pydantic-client
Http client base pydantic, with requests, aiohttp and httpx. Only support the json response.
If you like this project, please give me a star.
How to install
only support
requests
:pip install pydantic-client
support
aiohttp
andrequests
:pip install "pydantic-client[aiohttp]"
support
httpx(async)
andrequests
: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.3: you can define 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()
)
v1.0.5: support file response type.
from pydantic_client.schema.file import File
from pydantic_client import post
@post("/download")
def download_file(self) -> File:
# you will get the bytes content of the file
...
Project details
Release history Release notifications | RSS feed
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.5.tar.gz
(10.7 kB
view details)
Built Distribution
File details
Details for the file pydantic_client-1.0.5.tar.gz
.
File metadata
- Download URL: pydantic_client-1.0.5.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bac00bf9cdcfb90c1b0e889422ad3d1b5064041ca9d5f9a5ac952d56ab3c2896 |
|
MD5 | f80c738086fa87b2364431acf6274041 |
|
BLAKE2b-256 | 8cda52fa99da118d92dbdd0a303187d5f70c88fe74818ee856333b60a621e8b8 |
File details
Details for the file pydantic_client-1.0.5-py3-none-any.whl
.
File metadata
- Download URL: pydantic_client-1.0.5-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30a56666402029c8b343108e1110799ab7c50bbe5f83379af8f06141fffd2f74 |
|
MD5 | 886a704224e44b5b561151c4dfb68405 |
|
BLAKE2b-256 | fa023cc5888364cadb35ec41bd2f399d90a5c21b272c80525bbf27f44b567dc9 |