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.
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, PydanticClient
from pydantic_client.clients import RequestsClient, AIOHttpClient, HttpxClient
from pydantic_client import ClientConfig, PydanticClientFactory
class Book(BaseModel):
name: str
age: int
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:
...
"""
toml config example:
[tools.pydantic-client.config]
base_url = "http://localhost:5000" (have to set)
headers.authorization = "Bearer xxxxxx" (optional)
http2 = true (optional)
timeout = 10 (optional)
"""
client = PydanticClient.from_toml("your_toml_path.toml") \
.bind_client(RequestsClient) \
.bind_protocol(WebClient) \
.build()
# or
client: WebClient = PydanticClient(
ClientConfig(
base_url="https://example.com",
headers={"Authorization": "Bearer abcdefg"},
timeout=10
)
).bind_client(RequestsClient) \
.bind_protocol(WebClient) \
.build()
get_book: Book = client.get_book(1)
# use the factory
"""
toml file example:
[[tools.pydantic_client.factory]]
name = "book_client
base_url = "https://example.com/api/v1"
timeout = 1
[[tools.pydantic_client.factory]]
name = "author_client
base_url = "https://example.com/api/v2"
timeout = 1
[[tools.pydantic_client.factory]]
name = "address_client
base_url = "https://example.com/api/v3"
timeout = 1
"""
class BookProtocol:
@get("/books/{book_id}?query={query}")
def get_book(self, book_id: int, query: str) -> Book:
...
class AuthorProtocol:
@get("/books/{book_id}?query={query}")
def get_book(self, book_id: int, query: str) -> Book:
...
class AddressProtocol:
@get("/books/{book_id}?query={query}")
def get_book(self, book_id: int, query: str) -> Book:
...
factory = PydanticClientFactory.from_toml("pydantic_client.toml") \
.register_client("book_client", RequestsClient, BookProtocol) \
.register_client("author_client", HttpxClient, AuthorProtocol) \
.register_client("address_client", AIOHttpClient, AddressProtocol) \
.build()
book: Book = factory.get_client(BookProtocol).get_book(1, "name")
author: Book = factory.get_client(AuthorProtocol).get_book(1, "name")
change log
v1.0.0: refactor all the code, to be simple. remove the group client.
factory = PydanticClientFactory.from_toml("pydantic_client.toml")
.register_client("book_client", RequestsClient, BookProtocol)
.register_client("author_client", HttpxClient, AuthorProtocol)
.register_client("address_client", AIOHttpClient, AddressProtocol)
.build()
book: Book = factory.get_client(BookProtocol).get_books(1)
# change log
### v1.0.0: refactor all the code, to be simple. remove the group client.
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
Built Distribution
File details
Details for the file pydantic_client-1.0.0.tar.gz
.
File metadata
- Download URL: pydantic_client-1.0.0.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21e3bfd0002cce4b068f7c27f7962f32ddde2cd77b1443a63b56b066f2c8468c |
|
MD5 | fe3d36f5d77d6bd8f5eef9d46f0cf0b6 |
|
BLAKE2b-256 | f31a761afa6e7784b2130f41b97ea14d356151a8874352d85969712670c5a352 |
Provenance
File details
Details for the file pydantic_client-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: pydantic_client-1.0.0-py3-none-any.whl
- Upload date:
- Size: 14.5 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 | 58bee83c63695ee4d2c73a5749284646f4bea8ce12fbd58f2254408749b9d842 |
|
MD5 | 9ba4a088fa1295c5165bd2e3ec181ab6 |
|
BLAKE2b-256 | 4fb9aac1cd680c704d9f634724529ee6f0a781954cb2e67b5a8fd5c676b1c08a |