Pait encapsulates a number of common methods.
Through these methods developers can quickly develop extension packages without considering compatibility with different web frameworks.
OpenAPI routing and grpc-gateway are developed based on these methods.
data is the carrier for each CoreModel.
Pait decorates the route function to generate a CoreModel and store it in pait.g.data to support for configuration, documentation, etc feature.
The CoreModel stores a lot of information about the route functions, but the route functions are missing key OpenAPI information such as url, method, etc. So you need to use load_app to get more data before using OpenAPI.
So before using OpenAPI you need to use load_app to fill in the data, it's very simple to use, but you need to call it after registering all the routes, as follows.
Pait provides an HTTP exception generator function for each web framework,
which generates HTTP standard exceptions for web frameworks by parameters such as HTTP status code, error content, Headers, etc.
They are used as follows.
At the same time HTTP exception response Model also supports custom creation, the following example of use:
frompait.modelimportresponse# Create a response model with a status code of 500 and content-type as htmlresponse.HttpStatusCodeBaseModel.clone(resp_model=response.HtmlResponseModel,status_code=500)# Create a response model with status code 500 and content-type set to textresponse.HttpStatusCodeBaseModel.clone(resp_model=response.TextResponseModel,status_code=500)
Pait unifies the route registration and response generation of different web frameworks through SimpleRoute.
Developers can easily create and register routes through SimpleRoute without considering compatibility.
Unified route response generation is provided by the UnifiedResponsePluginProtocol plugin.
The UnifiedResponsePluginProtocol plugin is added to the route function when the route function is registered.
The first highlighted code creates three route functions according to the SimpleRoute standard, which is as follows:
1.The route functions need to be decorated by pait, and the response_model_list attribute cannot be empty (the response models of the route functions in the code are JsonResponseModel, TextResponseModel, HtmlResponseModel, these are all required by SimpleRoute, if there is no response model, then SimpleRoute can't register the route function to the web framework.)
2.The return value of the route function changes from a response object to a Python base type, and the returned Python base type needs to be consistent with the response_data of the response model.
The second highlight code is the registration of routes via the add_simple_route and add_multi_simple_route methods,
where add_simple_route can only register a single route and add_multi_simple_route can register multiple routes.
Both add_simple_route and add_multi_simple_route receive app and SimpleRoute instances,
whereas SimpleRoute supports only three attributes, as follows:
A route function that conforms to the SimpleRoute standard
The URL of the route
HTTP method of the route
In addition, add_multi_simple_route supports two optional parameters, as follows:
Route prefix, for example, if the prefix is "/api" and the url of SimpleRoute is "/user", the registered route URL is "/api/user".
The title of the route group. For some frameworks, the route groups or blueprints they use need to be uniquely named, so the title should be different for different add_multi_simple_routes
After running the code, test that the route works properly via the curl command:
Pait provides a unified method for setting and getting attribute values for Web frameworks, which are set_app_attribute and get_app_attribute.
The set_app_attribute and get_app_attribute can be used to set and get Web framework attributes at any time, as follows:
As you can see by the result, the route function is able to get client and through client get the status_code of the url.
By setting property values for the web framework, can decouple the component from the framework and also make the component more flexible, but it is more recommended to use DI tools to realize decoupling, see Awesome Dependency Injection in Python.
3.How to use Pait with other web frameworks
Currently, Pait is still in the process of rapid iteration, so the main focus is on feature development.
If you want to use Pait in other frameworks that are not yet supported, or if you want to extend the functionality,
can refer to the two frameworks to make simple adaptations.