![GitHub issues](https://img.shields.io/github/issues/mom1/api-client-pydantic.svg)
![GitHub stars](https://img.shields.io/github/stars/mom1/api-client-pydantic.svg)
![GitHub Release Date](https://img.shields.io/github/release-date/mom1/api-client-pydantic.svg)
![GitHub commits since latest release](https://img.shields.io/github/commits-since/mom1/api-client-pydantic/latest.svg)
![GitHub last commit](https://img.shields.io/github/last-commit/mom1/api-client-pydantic.svg)
[![GitHub license](https://img.shields.io/github/license/mom1/api-client-pydantic)](https://github.com/mom1/api-client-pydantic/blob/master/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/api-client-pydantic.svg)](https://pypi.python.org/pypi/api-client-pydantic)
[![PyPI](https://img.shields.io/pypi/pyversions/api-client-pydantic.svg)]()
![PyPI - Downloads](https://img.shields.io/pypi/dm/api-client-pydantic.svg?label=pip%20installs&logo=python)
<a href="https://gitmoji.dev"><img src="https://img.shields.io/badge/gitmoji-%20😜%20😍-FFDD67.svg" alt="Gitmoji"></a>
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
# Python API Client Pydantic Extension
## Installation
```bash
pip install api-client-pydantic
```
## Usage
The following decorators have been provided to validate request data and converting json straight to pydantic class.
```python
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.
```python
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.
```python
@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:
```python
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`.
```python
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](https://github.com/MikeWooster/api-client) app from an openapi file.
[apiclient-pydantic-generator](https://github.com/mom1/apiclient-pydantic-generator)
Raw data
{
"_id": null,
"home_page": "https://github.com/mom1/api-client-pydantic",
"name": "api-client-pydantic",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.9",
"maintainer_email": null,
"keywords": "api-client, api-client-extension",
"author": "MaxST",
"author_email": "mstolpasov@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/89/b7/b121dfe930ac00a1ff2c7119df7e01bfaa41c0a43285c31c19e26dacac15/api_client_pydantic-3.1.0.tar.gz",
"platform": null,
"description": "![GitHub issues](https://img.shields.io/github/issues/mom1/api-client-pydantic.svg)\n![GitHub stars](https://img.shields.io/github/stars/mom1/api-client-pydantic.svg)\n![GitHub Release Date](https://img.shields.io/github/release-date/mom1/api-client-pydantic.svg)\n![GitHub commits since latest release](https://img.shields.io/github/commits-since/mom1/api-client-pydantic/latest.svg)\n![GitHub last commit](https://img.shields.io/github/last-commit/mom1/api-client-pydantic.svg)\n[![GitHub license](https://img.shields.io/github/license/mom1/api-client-pydantic)](https://github.com/mom1/api-client-pydantic/blob/master/LICENSE)\n\n[![PyPI](https://img.shields.io/pypi/v/api-client-pydantic.svg)](https://pypi.python.org/pypi/api-client-pydantic)\n[![PyPI](https://img.shields.io/pypi/pyversions/api-client-pydantic.svg)]()\n![PyPI - Downloads](https://img.shields.io/pypi/dm/api-client-pydantic.svg?label=pip%20installs&logo=python)\n\n<a href=\"https://gitmoji.dev\"><img src=\"https://img.shields.io/badge/gitmoji-%20\ud83d\ude1c%20\ud83d\ude0d-FFDD67.svg\" alt=\"Gitmoji\"></a>\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n\n# Python API Client Pydantic Extension\n\n## Installation\n\n```bash\npip install api-client-pydantic\n```\n\n## Usage\n\nThe following decorators have been provided to validate request data and converting json straight to pydantic class.\n\n```python\nfrom apiclient_pydantic import serialize, serialize_all_methods\n\n\n# serialize request and response data\n@serialize(config: Optional[ConfigDict] = None, validate_return: bool = True, response: Optional[Type[BaseModel]] = None)\n\n# wraps all local methods of a class with a decorator 'serialize'.\n@serialize_all_methods(config: Optional[ConfigDict] = None)\n```\n\nUsage:\n\n1. Define the schema for your api in pydantic classes.\n ```python\n from pydantic import BaseModel, Field\n\n\n class Account(BaseModel):\n account_number: int = Field(alias='accountNumber')\n sort_code: int = Field(alias='sortCode')\n date_opened: datetime = Field(alias='dateOpened')\n ```\n\n2. Add the `@serialize` decorator to the api client method to transform the response\n directly into your defined schema.\n ```python\n @serialize(response=List[Account])\n def get_accounts():\n ...\n # or\n @serialize\n def get_accounts() -> List[Account]:\n ...\n ```\n3. Add the `@serialize` decorator to the api client method to translate the incoming kwargs\n into the required dict or instance for the endpoint:\n ```python\n from apiclient_pydantic import ModelDumped\n\n @serialize\n def create_account(data: AccountHolder):\n # data will be AccountHolder instance\n ...\n\n create_account(data={'last_name' : 'Smith','first_name' : 'John'})\n # data will be a AccountHolder(last_name=\"Smith\", first_name=\"John\")\n\n @serialize\n def create_account(data: ModelDumped[AccountHolder]):\n # data will be exactly a dict\n ...\n\n create_account(data={'last_name' : 'Smith','first_name' : 'John'})\n # data will be a dict {\"last_name\": \"Smith\", \"first_name\": \"John\"}\n ```\n4. For more convenient use, you can wrap all APIClient methods with `@serialize_all_methods`.\n ```python\n from apiclient import APIClient\n from apiclient_pydantic import serialize_all_methods\n from typing import List\n\n from .models import Account, AccountHolder\n\n\n @serialize_all_methods\n class MyApiClient(APIClient):\n def decorated_func(self, data: Account) -> Account:\n ...\n\n def decorated_func_holder(self, data: AccountHolder) -> List[Account]:\n ...\n ```\n\n## Related projects\n\n### apiclient-pydantic-generator - Now deprecated.\n\nThis code generator creates a [ApiClient](https://github.com/MikeWooster/api-client) app from an openapi file.\n\n[apiclient-pydantic-generator](https://github.com/mom1/apiclient-pydantic-generator)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "API Client extension for validate and transform requests / responses using pydantic.",
"version": "3.1.0",
"project_urls": {
"Homepage": "https://github.com/mom1/api-client-pydantic",
"Repository": "https://github.com/mom1/api-client-pydantic"
},
"split_keywords": [
"api-client",
" api-client-extension"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bb122f21e1e9bc0a6cfdb11bae5efe03946a0577ef1f5260ee0ced6d43a37e48",
"md5": "ba4cfebe13eb66b237e2c466993aea05",
"sha256": "7c7d56ec31a316b2408df76ba083c5e04f678a9105e839ccfae7d67c93efd6a7"
},
"downloads": -1,
"filename": "api_client_pydantic-3.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ba4cfebe13eb66b237e2c466993aea05",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.9",
"size": 8637,
"upload_time": "2024-12-15T22:32:42",
"upload_time_iso_8601": "2024-12-15T22:32:42.384993Z",
"url": "https://files.pythonhosted.org/packages/bb/12/2f21e1e9bc0a6cfdb11bae5efe03946a0577ef1f5260ee0ced6d43a37e48/api_client_pydantic-3.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "89b7b121dfe930ac00a1ff2c7119df7e01bfaa41c0a43285c31c19e26dacac15",
"md5": "67b804a3fa8774d084be908d69d4c7e4",
"sha256": "57f97f8127f77d76cf61bb89a55b9f625e85996b6dc4aa6ad2fce07db88e928a"
},
"downloads": -1,
"filename": "api_client_pydantic-3.1.0.tar.gz",
"has_sig": false,
"md5_digest": "67b804a3fa8774d084be908d69d4c7e4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.9",
"size": 9834,
"upload_time": "2024-12-15T22:32:44",
"upload_time_iso_8601": "2024-12-15T22:32:44.578208Z",
"url": "https://files.pythonhosted.org/packages/89/b7/b121dfe930ac00a1ff2c7119df7e01bfaa41c0a43285c31c19e26dacac15/api_client_pydantic-3.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-15 22:32:44",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mom1",
"github_project": "api-client-pydantic",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "api-client-pydantic"
}