Skip to main content

Pait is a python api interface tool, which can also be called a python api interface type (type hint)

Project description

pait

Pait is a python api interface tool, which can also be called a python api interface type (type hint)

pait enables your python web framework to have type checking and parameter type conversion like fastapi (power by pydantic)

了解如何实现类型转换和检查功能

Installation

pip install pait

Usage

IDE Support

While pydantic will work well with any IDE out of the box.

Use Example

Currently only supports starlette and flask(more python web frameworks will be supported in the future)

starlette use example:

import uvicorn

from typing import Optional
from starlette.applications import Starlette
from starlette.routing import Route
from starlette.requests import Request
from starlette.responses import JSONResponse

from pait.field import Body, Query
from pait.web.starletter import params_verify
from pydantic import (
    BaseModel,
    conint,
    constr,
)


class PydanticModel(BaseModel):
    uid: conint(gt=10, lt=1000)
    user_name: constr(min_length=2, max_length=4)


class PydanticOtherModel(BaseModel):
    age: conint(gt=1, lt=100)


@params_verify()
async def demo_post(
        request: Request,
        model: PydanticModel = Body(),
        other_model: PydanticOtherModel = Body()
):
    """Test Method:Post request, Pydantic Model and request"""
    print(request)
    return_dict = model.dict()
    return_dict.update(other_model.dict())
    return JSONResponse({'result': model.dict()})


@params_verify()
async def demo_get2(
    model: PydanticModel = Query(),
    other_model: PydanticOtherModel = Query()
):
    """Test Method:Post request, Pydantic Model"""
    return_dict = model.dict()
    return_dict.update(other_model.dict())
    return JSONResponse({'result': return_dict})


@params_verify()
async def demo_get(
    uid: conint(gt=10, lt=1000) = Query(),
    user_name: constr(min_length=2, max_length=4) = Query(),
    email: Optional[str] = Query(default='example@xxx.com'),
    model: PydanticOtherModel = Query()
):
    """Text Pydantic Model and Field"""
    _dict = {
        'uid': uid,
        'user_name': user_name,
        'email': email,
        'age': model.age
    }
    return JSONResponse({'result': _dict})


app = Starlette(
    routes=[
        Route('/api', demo_get, methods=['GET']),
        Route('/api1', demo_post, methods=['POST']),
        Route('/api2', demo_get2, methods=['GET']),
    ]
)


uvicorn.run(app)

flask use example:

from typing import Optional

from flask import Flask

from pait.field import Body, Query
from pait.web.flask import params_verify
from pydantic import (
    BaseModel,
    conint,
    constr,
)


class PydanticModel(BaseModel):
    uid: conint(gt=10, lt=1000)
    user_name: constr(min_length=2, max_length=4)


class PydanticOtherModel(BaseModel):
    age: conint(gt=1, lt=100)


app = Flask(__name__)


@app.route("/api", methods=['POST'])
@params_verify()
def demo_post(
    model: PydanticModel = Body(),
    other_model: PydanticOtherModel = Body()
):
    return_dict = model.dict()
    return_dict.update(other_model.dict())
    return {'result': model.dict()}


@app.route("/api2", methods=['GET'])
@params_verify()
def demo_get2(
    model: PydanticModel = Query(),
    other_model: PydanticOtherModel = Query()
):
    return_dict = model.dict()
    return_dict.update(other_model.dict())
    return {'result': model.dict()}


@app.route("/api", methods=['GET'])
@params_verify()
def demo_get(
    uid: conint(gt=10, lt=1000) = Query(),
    user_name: constr(min_length=2, max_length=4) = Query(),
    email: Optional[str] = Query(default='example@xxx.com'),
    model: PydanticOtherModel = Query()
):
    """Text Pydantic Model and Field"""
    _dict = {
        'uid': uid,
        'user_name': user_name,
        'email': email,
        'age': model.age
    }
    return {'result': _dict}


app.run(port=8000)

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

Uploaded Source

Built Distribution

pait-0.1.1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pait-0.1.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.5.3

File hashes

Hashes for pait-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4aed89ef8f02a1ce5c73c8c46af01d342d8918922d35c7198968a75dfa310f34
MD5 b57c683f601965af84626adac0a937a4
BLAKE2b-256 9a1e8e77481304110db13dd067febe0c5467c7c2af6987852713a969dffe1988

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pait-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.5.3

File hashes

Hashes for pait-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fdc23ca9722c3506a537d7246a66c104b7f91981f192456d0dfa1ad04eafd603
MD5 77825da908fd97ead2dbe7e662f5d89f
BLAKE2b-256 2b809c008a6a2d9f2c010db3d33d9407af654dec3d325a409d7c0993c321db5f

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