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

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:

  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_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]:
        ...
    
  3. 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"}
    
  4. @serialize - It is a combination of the two decorators @serialize_response and@serialize_request.
  5. 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]:
             ...
    

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-1.1.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

api_client_pydantic-1.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file api-client-pydantic-1.1.0.tar.gz.

File metadata

  • Download URL: api-client-pydantic-1.1.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.11 Linux/5.8.0-1039-azure

File hashes

Hashes for api-client-pydantic-1.1.0.tar.gz
Algorithm Hash digest
SHA256 17a2b08b0bfc746e232df84c5faaaf50239db9569feb19f31d6861deab7edc39
MD5 d3ba9223c0db42693d29f178012289d6
BLAKE2b-256 29215e700484134ba3442e293e0d09b7d37923a977c774dada0c041aedde405d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: api_client_pydantic-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.7 CPython/3.8.11 Linux/5.8.0-1039-azure

File hashes

Hashes for api_client_pydantic-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d96b9b0ebcb147a7e874ef637b75292fd867c1467a39fb84232b4105d06382be
MD5 bba1390820e83cf07307c7475d338a7e
BLAKE2b-256 616a8a1a17e19698893957d7b7488f958d73c0dc804da8f32b30785d8022087b

See more details on using hashes here.

Release history Release notifications | RSS feed

3.1.0

2 files

3.0.2

2 files

3.0.1

2 files

3.0.0

2 files

2.2.1

2 files

2.2.0

2 files

2.1.0

2 files

2.0.1

2 files

2.0.0

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.1

2 files

This release

1.1.0 This release

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 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