Skip to main content

API Client extension for validate and transform requests / responses using pydantic.

Project description

GitHub issues GitHub stars GitHub Release Date GitHub commits since latest release GitHub last commit GitHub license

PyPI PyPI PyPI - Downloads

Gitmoji Ruff

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:

  1. 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')
    
  2. 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]:
         ...
    
  3. 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"}
    
  4. 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.

apiclient-pydantic-generator

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

api_client_pydantic-3.0.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

api_client_pydantic-3.0.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file api_client_pydantic-3.0.1.tar.gz.

File metadata

  • Download URL: api_client_pydantic-3.0.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.8.18 Linux/6.5.0-1025-azure

File hashes

Hashes for api_client_pydantic-3.0.1.tar.gz
Algorithm Hash digest
SHA256 34d00964ac077dfed11269e7cca375105ef3a4d34da925b04457728ae8d93859
MD5 9968386c172ac55f9200d4ffec3da7af
BLAKE2b-256 879cec4b8bd98a2f3bc68ff1680acea91aeda51094c8909a74a8d0eafe92bb54

See more details on using hashes here.

File details

Details for the file api_client_pydantic-3.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for api_client_pydantic-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 330acb7345f756d372d765346098b567373310fd2e65966f2cfa8729998b0e76
MD5 c2f41f3748d53ead00931902187acb3a
BLAKE2b-256 af6c4f2b41b6528284cb30edf67d7278fe6c2dd600b62edf02e56d6b89552734

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page