@@ -28,7 +28,7 @@ This documentation explains how to develop this project.
28
28
## Prerequisites
29
29
30
30
* Python 3.7+
31
- * APISIX 2.7.0
31
+ * APISIX 2.7.0+
32
32
33
33
## Debug
34
34
@@ -56,56 +56,69 @@ the `.py` files in this directory autoload
56
56
#### Plugin Format
57
57
58
58
``` python
59
- from apisix.runner.plugin.base import Base
59
+ from typing import Any
60
60
from apisix.runner.http.request import Request
61
61
from apisix.runner.http.response import Response
62
+ from apisix.runner.plugin.core import PluginBase
62
63
63
64
64
- class Test (Base ):
65
- def __init__ (self ):
66
- super (Test, self ).__init__ (self .__class__ .__name__ )
65
+ class Test (PluginBase ):
67
66
68
- def filter (self , request : Request, response : Response):
67
+ def name (self ) -> str :
68
+ """
69
+ The name of the plugin registered in the runner
70
+ :return:
71
+ """
72
+ return " test"
73
+
74
+ def config (self , conf : Any) -> Any:
75
+ """
76
+ Parse plugin configuration
77
+ :param conf:
78
+ :return:
79
+ """
80
+ return conf
81
+
82
+ def filter (self , conf : Any, request : Request, response : Response):
69
83
"""
70
84
The plugin executes the main function
85
+ :param conf:
86
+ plugin configuration after parsing
71
87
:param request:
72
88
request parameters and information
73
89
:param response:
74
90
response parameters and information
75
91
:return:
76
92
"""
77
- # Get plugin configuration information through `self.config`
78
- # print(self.config)
79
-
80
- # Setting the request object will continue to forward the request
81
-
82
- # Rewrite request headers
83
- request.headers[" X-Resp-A6-Runner" ] = " Python"
84
93
85
- # Rewrite request args
86
- request.args[ " a6_runner " ] = " Python "
94
+ # print plugin configuration
95
+ print (conf)
87
96
88
- # Rewrite request path
89
- request.path = " /a6/python/runner"
97
+ # Fetch request nginx variable `host`
98
+ host = request.get_var(" host" )
99
+ print (host)
90
100
91
- # Setting the response object will terminate the request and respond to the data
101
+ # Fetch request body
102
+ body = request.get_body()
103
+ print (body)
92
104
93
105
# Set response headers
94
- response.headers[ " X-Resp-A6-Runner" ] = " Python"
106
+ response.set_header( " X-Resp-A6-Runner" , " Python" )
95
107
96
108
# Set response body
97
- response.body = " Hello, Python Runner of APISIX"
109
+ response.set_body( " Hello, Python Runner of APISIX" )
98
110
99
111
# Set response status code
100
- response.status_code = 201
112
+ response.set_status_code( 201 )
101
113
```
102
114
103
- - The plugin must inherit the ` Base ` class
104
- - The plugin must implement the ` filter ` function
105
- - ` filter ` function parameters can only contain ` Request ` and ` Response ` classes as parameters
106
- - Request parameter can get and set request information
107
- - Response parameter can set response information
108
- - ` self.config ` can get plug-in configuration information
115
+ - Plugins must inherit the ` PluginBase ` class and implement all functions.
116
+ - ` name ` function: used to set the registered plugin name.
117
+ - ` config ` function: used to parse plugin configuration.
118
+ - ` filter ` function: used to filter requests.
119
+ - ` conf ` parameter: plugin configuration after parsing.
120
+ - ` request ` parameter: Request object, which can be used to get and set request information.
121
+ - ` response ` parameter: Response object, which can be used to set response information.
109
122
110
123
## Test
111
124
0 commit comments