Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

api-client-pydantic

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

api-client-pydantic

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

  • 3.0.1
  • PyPI
  • Socket score

Maintainers
1

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]:
             ...
    

apiclient-pydantic-generator - Now deprecated.

This code generator creates a ApiClient app from an openapi file.

apiclient-pydantic-generator

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc