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 params_serializer, response_serializer, serialize, serialize_all_methods
# serialize incoming kwargs
@params_serializer(by_alias: bool = True, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = True)
# serialize response in pydantic class
@response_serializer(response: Optional[Type[BaseModel]] = None)
# serialize request and response data
@serialize(response: Optional[Type[BaseModel]] = None, by_alias: bool = True, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = True)
# wraps all local methods of a class with a specified decorator. default 'serialize'
@serialize_all_methods(decorator=serialize)
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
@response_serializer
decorator to the api client method to transform the response directly into your defined schema.@response_serializer(List[Account]) def get_accounts(): ... # or @response_serializer() def get_accounts() -> List[Account]: ...
-
Add the
@params_serializer
decorator to the api client method to translate the incoming kwargs into the required dict for the endpoint:@params_serializer(AccountHolder) def create_account(data: dict): ... # or @params_serializer() def create_account(data: AccountHolder): # data will be exactly a dict ... create_account(last_name='Smith', first_name='John') # data will be a dict {"last_name": "Smith", "first_name": "John"}
-
@serialize
- It is a combination of the two decorators@response_serializer
and@params_serializer
. -
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
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-2.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e361edc42cd8f98f2e210d9d092d8ab54e02169e0734475687027479ba4c957 |
|
MD5 | db4984e9cfb0e46179d3e57da91b3d21 |
|
BLAKE2b-256 | 91227e538f1b6cc1f4bd58e038b6b0f29d7f041a6fc814e24c13513ed9e02c7e |
Hashes for api_client_pydantic-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab4e3ee59ffaa394ea7efa66e687236275fc9870dcd3a6bf5a9b0a4908d8e60d |
|
MD5 | 345b660bddbbaaf34dd8bb7109055e20 |
|
BLAKE2b-256 | 701bce57f18776ed38cdeea59134afe37ae59895d2bbfa7ad03bd821324f08f4 |