api-client-pydantic


Nameapi-client-pydantic JSON
Version 3.0.0 PyPI version JSON
download
home_pagehttps://github.com/mom1/api-client-pydantic
SummaryAPI Client extension for validate and transform requests / responses using pydantic.
upload_time2024-01-05 10:16:05
maintainer
docs_urlNone
authorMaxST
requires_python>=3.8.4,<4
licenseMIT
keywords api-client api-client-extension
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![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": "",
    "docs_url": null,
    "requires_python": ">=3.8.4,<4",
    "maintainer_email": "",
    "keywords": "api-client,api-client-extension",
    "author": "MaxST",
    "author_email": "mstolpasov@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/d9/41/23c04bbc2e60fbe78e2c2162e373efabad8cdf83ecbe039aa9bdaf7a1b64/api_client_pydantic-3.0.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.0.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": "8b2e88cee6ca6406da295f41e7b54e517b6e121a78a02bd2655f41c5f3f5ec7d",
                "md5": "5cbbf01158cca735907ba361f36ba106",
                "sha256": "78be8944687518d927ec4e52d2bdde8103c7205e2aef38baa9dcec3c26fc800a"
            },
            "downloads": -1,
            "filename": "api_client_pydantic-3.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5cbbf01158cca735907ba361f36ba106",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8.4,<4",
            "size": 8942,
            "upload_time": "2024-01-05T10:16:04",
            "upload_time_iso_8601": "2024-01-05T10:16:04.149266Z",
            "url": "https://files.pythonhosted.org/packages/8b/2e/88cee6ca6406da295f41e7b54e517b6e121a78a02bd2655f41c5f3f5ec7d/api_client_pydantic-3.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d94123c04bbc2e60fbe78e2c2162e373efabad8cdf83ecbe039aa9bdaf7a1b64",
                "md5": "4aaf2a717ff35840b99897994197f129",
                "sha256": "c13fcff86eb9254246ab3d6b713e4ad7351b2aa20ebcd7bc77ff907ea170847f"
            },
            "downloads": -1,
            "filename": "api_client_pydantic-3.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4aaf2a717ff35840b99897994197f129",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8.4,<4",
            "size": 10272,
            "upload_time": "2024-01-05T10:16:05",
            "upload_time_iso_8601": "2024-01-05T10:16:05.967291Z",
            "url": "https://files.pythonhosted.org/packages/d9/41/23c04bbc2e60fbe78e2c2162e373efabad8cdf83ecbe039aa9bdaf7a1b64/api_client_pydantic-3.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-05 10:16:05",
    "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"
}
        
Elapsed time: 0.16155s