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, serialize_response
# serialize incoming kwargs
@serialize_request(schema: Optional[Type[BaseModel]] = None, extra_kwargs: dict = None)
# serialize response in pydantic class
@serialize_response(schema: Optional[Type[BaseModel]] = None)
# serialize request and response data
@serialize(schema_request: Optional[Type[BaseModel]] = None, schema_response: Optional[Type[BaseModel]] = None, **base_kwargs)
# 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
@serialize_response
decorator to the api client method to transform the response directly into your defined schema.@serialize_response(List[Account]) def get_accounts(): ... # or @serialize_response() def get_accounts() -> List[Account]: ...
- Add the
@serialize_request
decorator to the api client method to translate the incoming kwargs into the required dict for the endpoint:@serialize_request(AccountHolder) def create_account(data: dict): ... # or @serialize_request() 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@serialize_response
and@serialize_request
.- 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
Close
Hashes for api-client-pydantic-1.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 353c7c2959f3b267be7819d2779face3aed9925d94d2fdb7771dbe5de8d487e4 |
|
MD5 | 0273ab3a42bb9c8e5a2d992f56e18801 |
|
BLAKE2b-256 | c11f36102f5aa13927f7ebacf80968621fa02a042201750bb2726f0c7cd2b75d |
Close
Hashes for api_client_pydantic-1.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8abbfdb9969f30e6703b975aaa0802aadbc0f9013a8c577d09a9f7d442e06e86 |
|
MD5 | 1ae93c6ab0500adb95b6c06694286873 |
|
BLAKE2b-256 | fb685ae7f4f74f888bf49f9876225f16ad8783b00bf7ae9a643f937f46a583a0 |