API Client extension for validate and transform requests / responses using pydantic.
Project description
Python API Client Pydantic Extension
Installation
pip install api-client-pydantic
Usage
The following decorators have been provided to validate request data and converting json straight to pydantic class.
from apiclient_pydantic import serialize, serialize_all_methods
# serialize request and response data
@serialize(config: Optional[ConfigDict] = None, validate_return: bool = True, response: Optional[Type[BaseModel]] = None)
# wraps all local methods of a class with a decorator 'serialize'.
@serialize_all_methods(config: Optional[ConfigDict] = None)
Usage:
-
Define the schema for your api in pydantic classes.
from pydantic import BaseModel, Field class Account(BaseModel): account_number: int = Field(alias='accountNumber') sort_code: int = Field(alias='sortCode') date_opened: datetime = Field(alias='dateOpened')
-
Add the
@serialize
decorator to the api client method to transform the response directly into your defined schema.@serialize(response=List[Account]) def get_accounts(): ... # or @serialize def get_accounts() -> List[Account]: ...
-
Add the
@serialize
decorator to the api client method to translate the incoming kwargs into the required dict or instance for the endpoint:from apiclient_pydantic import ModelDumped @serialize def create_account(data: AccountHolder): # data will be AccountHolder instance ... create_account(data={'last_name' : 'Smith','first_name' : 'John'}) # data will be a AccountHolder(last_name="Smith", first_name="John") @serialize def create_account(data: ModelDumped[AccountHolder]): # data will be exactly a dict ... create_account(data={'last_name' : 'Smith','first_name' : 'John'}) # data will be a dict {"last_name": "Smith", "first_name": "John"}
-
For more convenient use, you can wrap all APIClient methods with
@serialize_all_methods
.from apiclient import APIClient from apiclient_pydantic import serialize_all_methods from typing import List from .models import Account, AccountHolder @serialize_all_methods class MyApiClient(APIClient): def decorated_func(self, data: Account) -> Account: ... def decorated_func_holder(self, data: AccountHolder) -> List[Account]: ...
Related projects
apiclient-pydantic-generator - Now deprecated.
This code generator creates a ApiClient app from an openapi 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
Built Distribution
Hashes for api_client_pydantic-3.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c13fcff86eb9254246ab3d6b713e4ad7351b2aa20ebcd7bc77ff907ea170847f |
|
MD5 | 4aaf2a717ff35840b99897994197f129 |
|
BLAKE2b-256 | d94123c04bbc2e60fbe78e2c2162e373efabad8cdf83ecbe039aa9bdaf7a1b64 |
Hashes for api_client_pydantic-3.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78be8944687518d927ec4e52d2bdde8103c7205e2aef38baa9dcec3c26fc800a |
|
MD5 | 5cbbf01158cca735907ba361f36ba106 |
|
BLAKE2b-256 | 8b2e88cee6ca6406da295f41e7b54e517b6e121a78a02bd2655f41c5f3f5ec7d |