Http client base pydantic, with requests or aiohttp
Project description
pydantic-client
Http client base pydantic, with requests, aiohttp and httpx.
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
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"})
v0.1.17: load config from toml to init client.
# config.toml or pyproject.toml
[tool.pydantic_client.config]
base_url = "http://localhost/v1"
headers.authorization = "xxxxxxx"
headers.user = "xxxxxxx"
client = R.load_config_from_toml("config.toml")
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-0.1.17.tar.gz
.
File metadata
- Download URL: pydantic_client-0.1.17.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f65996332f52cf5e0e052181d0c7fc30cae7d4eff78255cdb4733c005c8a13bb |
|
MD5 | 872fc1f2e5c2d9c26e9dee9f60f02128 |
|
BLAKE2b-256 | dc667ae29cf352edb3d4f56c6ade780d52e249cc070825a315cb8112b737f8f9 |
Provenance
File details
Details for the file pydantic_client-0.1.17-py3-none-any.whl
.
File metadata
- Download URL: pydantic_client-0.1.17-py3-none-any.whl
- Upload date:
- Size: 13.4 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 | e1faf60f0b2d19b90010ab3005a0442508a04d79d637dd4b653b1892f3845d5c |
|
MD5 | 21dbd464513adc7bcd1e9100f274ce7c |
|
BLAKE2b-256 | 48e5e9882ffc58d17abb934c7133570f312f4c3812e2e0c8d8ff994c0ba15dce |