Skip to main content

Pait is a Python api tool. Pait enables your Python web framework to have type checking, parameter type conversion, interface document generation and can display your documents through Redoc or Swagger (power by inspect, pydantic)

Project description

Pait(π tool) - Python Modern API Tools, easier to use web frameworks/write API routing

Coverage

PyPI - Python Version PyPI

GitHub Workflow Status GitHub release (release name instead of tag name) Test

Support framework


Documentation: https://so1n.me/pait/

中文文档: https://so1n.me/pait-zh-doc/


Warning

There are changes between the current version and the 0.8 version of the API, For more information, please refer to 1.0.0version change

pait

Pait is an api tool that can be used in any python web framework, the features provided are as follows:

  • Parameter checksum automatic conversion (parameter check depends on Pydantic)
  • Parameter dependency verification
  • Automatically generate openapi files
  • Swagger, Redoc, Rapidoc, Elements, OpenAPI route
  • gRPC Gateway route
  • TestClient support, support response result verification
  • Support for plugin extensions, such as the Mock plugin, CheckResponse Plugin, Cache Response Plugin
  • Support Pydantic V1 and V2
  • WebSocket support
  • Auto API Test support

Note:

  • mypy check 100%

  • python version >= 3.7 (support postponed annotations)

Installation

pip install pait --pre

If want to use the gRPC gateway feature, need to add a 'grpc' dependency:

pip install pait[grpc] --pre

Simple Example

from typing import Type
import uvicorn  # type: ignore
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route

from pait.app.starlette import pait
from pait.field import Body
from pait.openapi.doc_route import add_doc_route
from pait.model.response import JsonResponseModel
from pydantic import BaseModel, Field


class DemoResponseModel(JsonResponseModel):
    """demo post api response model"""
    class ResponseModel(BaseModel):
        uid: int = Field()
        user_name: str = Field()

    description: str = "demo response"
    response_data: Type[BaseModel] = ResponseModel


@pait(response_model_list=[DemoResponseModel])
async def demo_post(
    uid: int = Body.i(description="user id", gt=10, lt=1000),
    user_name: str = Body.i(description="user name", min_length=2, max_length=4)
) -> JSONResponse:
    return JSONResponse({'uid': uid, 'user_name': user_name})


app = Starlette(routes=[Route('/api', demo_post, methods=['POST'])])
add_doc_route(app)
uvicorn.run(app)

See documentation for more features

Support Web framework

Framework Description
Flask All features supported
Sanic All features supported
Starlette All features supported
Tornado All features supported
Django Coming soon

If the web framework is not supported(which you are using).

Can be modified sync web framework according to pait.app.flask

Can be modified async web framework according to pait.app.starlette

Performance

For the parameter validation and transformation feature, Pait is mainly responsible for injecting the request data dependency into the Pydantic model, and then the Pydanitc verifies and transforms the data.

The time consumed by this process <=0.0003 (s), for benchmarks data and subsequent optimization, see #27

Full example

For more complete examples, please refer to example

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pait-1.0.0a11.tar.gz (81.9 kB view details)

Uploaded Source

Built Distribution

pait-1.0.0a11-py3-none-any.whl (124.5 kB view details)

Uploaded Python 3

File details

Details for the file pait-1.0.0a11.tar.gz.

File metadata

  • Download URL: pait-1.0.0a11.tar.gz
  • Upload date:
  • Size: 81.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.7.3 Linux/5.15.77-amd64-desktop

File hashes

Hashes for pait-1.0.0a11.tar.gz
Algorithm Hash digest
SHA256 c3d8eb7e270a8a2e63ec9b695d556d0e740d24b285f962550839985ef929481b
MD5 6b6cf2338c9962af0df32354df562ba0
BLAKE2b-256 8a7651a63a3cf7e5dc36d0e92db08e10c716a0f54545816252256e084ff84b18

See more details on using hashes here.

File details

Details for the file pait-1.0.0a11-py3-none-any.whl.

File metadata

  • Download URL: pait-1.0.0a11-py3-none-any.whl
  • Upload date:
  • Size: 124.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.7.3 Linux/5.15.77-amd64-desktop

File hashes

Hashes for pait-1.0.0a11-py3-none-any.whl
Algorithm Hash digest
SHA256 4dcb81214f9c1c23f9e4b63aa000288dcf1fad7b595fce0f0014364afe7b8163
MD5 c6cf23047f7d37f8643e2b1e00acc950
BLAKE2b-256 b6831cd9724fa0d3e511d4d0c52bd5e827361fa9b3637631b4f7d3c2120e22b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page