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/


pait

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

  • Integrate into the Type Hints ecosystem to provide a safe and efficient API interface coding method.
  • Automatic verification and type conversion of request parameters (depends on Pydantic and inspect, currently supports Pydantic V1 and V2 versions).
  • Automatically generate openapi files and support UI components such as Swagger,Redoc,RapiDoc and Elements.
  • TestClient support, response result verification of test cases。
  • Plugin expansion, such as parameter relationship dependency verification, Mock response, etc.。
  • gRPC GateWay (After version 1.0, this feature has been migrated to grpc-gateway)
  • Automated API testing
  • WebSocket support
  • SSE support

Note:

  • mypy check 100%

  • python version >= 3.8 (support postponed annotations)

Installation

pip install pait

Simple Example

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 pydantic import BaseModel, Field


class ResponseModel(BaseModel):
    """demo response"""
    uid: int = Field()
    user_name: str = Field()


@pait(response_model_list=[ResponseModel])
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

The main operating principle of Pait is to convert the function signature of the route function into Pydantic Model through the reflection mechanism when the program is started, and then verify and convert the request parameters through Pydantic Model when the request hits the route.

These two stages are all automatically handled internally by Pait. The first stage only slightly increases the startup time of the program, while the second stage increases the response time of the routing, but it only consumes 0.00005(s) more than manual processing. The specific benchmark data and subsequent optimization are described in #27.

Example

For more complete examples, please refer to example

Project details


Release history Release notifications | RSS feed

This version

1.1.2

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

Uploaded Source

Built Distribution

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

pait-1.1.2-py3-none-any.whl (157.0 kB view details)

Uploaded Python 3

File details

Details for the file pait-1.1.2.tar.gz.

File metadata

  • Download URL: pait-1.1.2.tar.gz
  • Upload date:
  • Size: 106.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.13 Linux/6.6.127-amd64-desktop-hwe

File hashes

Hashes for pait-1.1.2.tar.gz
Algorithm Hash digest
SHA256 c53d080d313a789c646a16dfa08ba3d4d6b645bea7bb6ef187d2cebb41a2e76e
MD5 573521bb992ad5eec666cc31780cb76a
BLAKE2b-256 ec404af7573e58aacc8501f1137d0ef3e159da92da9698103374343aa20cd6de

See more details on using hashes here.

File details

Details for the file pait-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: pait-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 157.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.13 Linux/6.6.127-amd64-desktop-hwe

File hashes

Hashes for pait-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c14d5cc8bc3e386677c17ebc47ac6e88202145692a7577d9495c2614618c29a5
MD5 cff599f67b734dcb811e211f2ace8ac2
BLAKE2b-256 c0c479acf078c990bf3f66bdd78e4321117e767a5feff5b474db942f410e4a38

See more details on using hashes here.

Supported by

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