Skip to main content

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

Metadata

Release files for api-client-pydantic 3.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for api-client-pydantic 3.1.0
File Size Uploaded
api_client_pydantic-3.1.0.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for api-client-pydantic 3.1.0
File Interpreter ABI Platform
api_client_pydantic-3.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.5 kB

Release files / api_client_pydantic-3.1.0.tar.gz

Download URL api_client_pydantic-3.1.0.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
57f97f8127f77d76cf61bb89a55b9f625e85996b6dc4aa6ad2fce07db88e928a
BLAKE2b-256 checksum
How to use checksums
89b7b121dfe930ac00a1ff2c7119df7e01bfaa41c0a43285c31c19e26dacac15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.8.18 Linux/6.5.0-1025-azure

Release files / api_client_pydantic-3.1.0-py3-none-any.whl

Download URL api_client_pydantic-3.1.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c7d56ec31a316b2408df76ba083c5e04f678a9105e839ccfae7d67c93efd6a7
BLAKE2b-256 checksum
How to use checksums
bb122f21e1e9bc0a6cfdb11bae5efe03946a0577ef1f5260ee0ced6d43a37e48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.8.18 Linux/6.5.0-1025-azure

Release history Release notifications | RSS feed

This release

3.1.0 This release

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page