Mock response plugin

In the development process, the back-end developer will often define the API document and discuss the implementation of the API with the front-end developer through the API document and make modifications, there is no specific implementation of the API in this stage. In this stage, the front-end and back-end developers will be developed at the same time, then there may be a front-end developers need to debug the API in the development process, but due to the back-end developers have not yet completed the development of the front-end developers can not be debugging the situation.

To do this, the Mock plugin can be used to return the specified response data for route functions that do not implement the logic, as follows:

docs_source_code/plugin/mock_plugin/flask_with_mock_plugin_demo.py
from typing import List, Type

from flask import Flask
from pydantic import BaseModel, Field

from pait.app.flask import pait
from pait.app.flask.plugin import MockPlugin
from pait.field import Query
from pait.model.response import JsonResponseModel


class UserSuccessRespModel2(JsonResponseModel):
    class ResponseModel(BaseModel):
        class DataModel(BaseModel):
            uid: int = Field(description="user id", gt=10, lt=1000, example=666)
            user_name: str = Field(example="mock_name", description="user name", min_length=2, max_length=10)
            multi_user_name: List[str] = Field(
                example=["mock_name"], description="user name", min_length=1, max_length=10
            )
            age: int = Field(example=99, description="age", gt=1, lt=100)
            email: str = Field(example="example@so1n.me", description="user email")

        code: int = Field(0, description="api code")
        msg: str = Field("success", description="api status msg")
        data: DataModel

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


@pait(response_model_list=[UserSuccessRespModel2], plugin_list=[MockPlugin.build()])
def demo(
    uid: int = Query.i(description="user id", gt=10, lt=1000),
    user_name: str = Query.i(description="user name", min_length=2, max_length=4),
    email: str = Query.i(default="example@xxx.com", description="user email"),
) -> dict:
    return {}


app = Flask("demo")
app.add_url_rule("/api/demo", view_func=demo, methods=["GET"])


if __name__ == "__main__":
    app.run(port=8000)
docs_source_code/plugin/mock_plugin/starlette_with_mock_plugin_demo.py
from typing import List, Type

from pydantic import BaseModel, Field
from starlette.applications import Starlette
from starlette.routing import Route

from pait.app.starlette import pait
from pait.app.starlette.plugin import MockPlugin
from pait.field import Query
from pait.model.response import JsonResponseModel


class UserSuccessRespModel2(JsonResponseModel):
    class ResponseModel(BaseModel):
        class DataModel(BaseModel):
            uid: int = Field(description="user id", gt=10, lt=1000, example=666)
            user_name: str = Field(example="mock_name", description="user name", min_length=2, max_length=10)
            multi_user_name: List[str] = Field(
                example=["mock_name"], description="user name", min_length=1, max_length=10
            )
            age: int = Field(example=99, description="age", gt=1, lt=100)
            email: str = Field(example="example@so1n.me", description="user email")

        code: int = Field(0, description="api code")
        msg: str = Field("success", description="api status msg")
        data: DataModel

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


@pait(response_model_list=[UserSuccessRespModel2], plugin_list=[MockPlugin.build()])
async def demo(
    uid: int = Query.i(description="user id", gt=10, lt=1000),
    user_name: str = Query.i(description="user name", min_length=2, max_length=4),
    email: str = Query.i(default="example@xxx.com", description="user email"),
) -> dict:
    return {}


app = Starlette(routes=[Route("/api/demo", demo, methods=["GET"])])


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app)
docs_source_code/plugin/mock_plugin/sanic_with_mock_plugin_demo.py
from typing import List, Type

from pydantic import BaseModel, Field
from sanic import Sanic

from pait.app.sanic import pait
from pait.app.sanic.plugin import MockPlugin
from pait.field import Query
from pait.model.response import JsonResponseModel


class UserSuccessRespModel2(JsonResponseModel):
    class ResponseModel(BaseModel):
        class DataModel(BaseModel):
            uid: int = Field(description="user id", gt=10, lt=1000, example=666)
            user_name: str = Field(example="mock_name", description="user name", min_length=2, max_length=10)
            multi_user_name: List[str] = Field(
                example=["mock_name"], description="user name", min_length=1, max_length=10
            )
            age: int = Field(example=99, description="age", gt=1, lt=100)
            email: str = Field(example="example@so1n.me", description="user email")

        code: int = Field(0, description="api code")
        msg: str = Field("success", description="api status msg")
        data: DataModel

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


@pait(response_model_list=[UserSuccessRespModel2], plugin_list=[MockPlugin.build()])
async def demo(
    uid: int = Query.i(description="user id", gt=10, lt=1000),
    user_name: str = Query.i(description="user name", min_length=2, max_length=4),
    email: str = Query.i(default="example@xxx.com", description="user email"),
) -> dict:
    return {}


app = Sanic("demo")
app.add_route(demo, "/api/demo", methods=["GET"])


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app)
docs_source_code/plugin/mock_plugin/tornado_with_mock_plugin_demo.py
from typing import List, Type

from pydantic import BaseModel, Field
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler

from pait.app.tornado import pait
from pait.app.tornado.plugin import MockPlugin
from pait.field import Query
from pait.model.response import JsonResponseModel


class UserSuccessRespModel2(JsonResponseModel):
    class ResponseModel(BaseModel):
        class DataModel(BaseModel):
            uid: int = Field(description="user id", gt=10, lt=1000, example=666)
            user_name: str = Field(example="mock_name", description="user name", min_length=2, max_length=10)
            multi_user_name: List[str] = Field(
                example=["mock_name"], description="user name", min_length=1, max_length=10
            )
            age: int = Field(example=99, description="age", gt=1, lt=100)
            email: str = Field(example="example@so1n.me", description="user email")

        code: int = Field(0, description="api code")
        msg: str = Field("success", description="api status msg")
        data: DataModel

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


class DemoHandler(RequestHandler):
    @pait(response_model_list=[UserSuccessRespModel2], plugin_list=[MockPlugin.build()])
    async def get(
        uid: int = Query.i(description="user id", gt=10, lt=1000),
        user_name: str = Query.i(description="user name", min_length=2, max_length=4),
        email: str = Query.i(default="example@xxx.com", description="user email"),
    ) -> dict:
        return {}


app: Application = Application([(r"/api/demo", DemoHandler)])


if __name__ == "__main__":
    app.listen(8000)
    IOLoop.instance().start()

The code first implements a response object called UserSuccessRespModel2, which differs from the previous response object in that some of its fields have example attributes. Then it creates a demo route function without any code logic, which has only a few parameters and uses the Mock plugin and the UserSuccessRespModel2 response object via pait.

Run the code and execute the following command, the output shows that after using the Mock plugin, the route function is able to return data and the data is the same as the value of example in the UserSuccessRespModel2 response object:

  curl http://127.0.0.1:8000/api/demo
{"code":0,"data":{"age":99,"email":"example@so1n.me","multi_user_name":["mock_name"],"uid":666,"user_name":"mock_name"},"msg":"success"}

Note

  • 1.example also supports factory functions, which have a similar effect to default_factory. Available values are example=time.now, example=lambda :random.randint(100000, 900000) and so on.
  • 2.Mock plugin supports defining the name of the field to be adopted by the parameter example_column_name, which is example by default and can also be mock.