Giter Site home page Giter Site logo

so1n / pait Goto Github PK

View Code? Open in Web Editor NEW
51.0 4.0 3.0 4.44 MB

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

License: Apache License 2.0

Python 99.93% Shell 0.07%
python type-hints pydantic flask openapi sanic tornado redoc swagger starlette

pait's Introduction

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

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

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.