一、环境接口管理
POST 创建环境
POST /test_envs/
Body 请求参数
{
"name": "ali",
"global_variable": {
"key": "Value"
},
"debug_global_variable": {},
"db": [
{
"name": "db1",
"type": "mysql",
"config": {
"user": "future",
"password": "123456",
"host": "api.lemonban.com",
"port": 3306
}
},
{
"name": "db2",
"type": "mysql",
"config": {
"user": "future",
"password": "123456",
"host": "api.lemonban.com",
"port": 3306
}
}
],
"host": "http://www.hhxpython.com/",
"headers": {},
"global_func": "",
"project": 1
}
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
body | body | object | 否 | none |
» name | body | string | 是 | none |
» global_variable | body | object | 否 | none |
»» key | body | string | 是 | none |
» debug_global_variable | body | object | 否 | none |
» db | body | [object] | 否 | none |
»» name | body | string | 是 | none |
»» type | body | string | 是 | none |
»» config | body | object | 是 | none |
»»» user | body | string | 是 | none |
»»» password | body | string | 是 | none |
»»» host | body | string | 是 | none |
»»» port | body | integer | 是 | none |
» host | body | string | 是 | none |
» headers | body | object | 否 | none |
» global_func | body | string | 否 | none |
» project | body | integer | 是 | none |
返回示例
成功
{
"id": 2,
"name": "ali",
"global_variable": {
"key": "Value"
},
"debug_global_variable": {},
"db": [
{
"name": "db1",
"type": "mysql",
"config": {
"user": "future",
"password": "123456",
"host": "api.lemonban.com",
"port": 3306
}
},
{
"name": "db2",
"type": "mysql",
"config": {
"user": "future",
"password": "123456",
"host": "api.lemonban.com",
"port": 3306
}
}
],
"host": "http://www.hhxpython.com/",
"headers": {},
"global_func": "",
"project": 1
}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
201 | 成功 | Inline |
返回数据结构
GET 查看环境列表
GET /test_envs/
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
project | query | integer | 是 | none |
返回示例
200 Response
{}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
200 | 成功 | Inline |
返回数据结构
DELETE 删除环境
DELETE /test_envs/{id}/
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
id | path | string | 是 | none |
返回示例
204 Response
{}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
204 | 删除成功 | Inline |
返回数据结构
PUT 修改环境
PUT /test_envs/{id}/
Body 请求参数
{
"name": "ali",
"global_variable": {
"key": "Value"
},
"debug_global_variable": {},
"db": [
{
"name": "db1",
"type": "mysql",
"config": {
"user": "future",
"password": "123456",
"host": "api.lemonban.com",
"port": 3306
}
},
{
"name": "db2",
"type": "mysql",
"config": {
"user": "future",
"password": "123456",
"host": "api.lemonban.com",
"port": 3306
}
}
],
"host": "http://www.hhxpython.com/",
"headers": {},
"global_func": "",
"project": 1
}
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
id | path | string | 是 | none |
body | body | object | 否 | none |
» name | body | string | 是 | none |
» global_variable | body | object | 是 | none |
»» key | body | string | 是 | none |
» debug_global_variable | body | object | 否 | none |
» db | body | [object] | 否 | none |
»» name | body | string | 是 | none |
»» type | body | string | 是 | none |
»» config | body | object | 是 | none |
»»» user | body | string | 是 | none |
»»» password | body | string | 是 | none |
»»» host | body | string | 是 | none |
»»» port | body | integer | 是 | none |
» host | body | string | 是 | none |
» headers | body | object | 否 | none |
» global_func | body | string | 否 | none |
» project | body | integer | 是 | none |
返回示例
成功
{
"id": 2,
"name": "ali",
"global_variable": {
"key": "Value"
},
"debug_global_variable": {},
"db": [
{
"name": "db1",
"type": "mysql",
"config": {
"user": "future",
"password": "123456",
"host": "api.lemonban.com",
"port": 3306
}
},
{
"name": "db2",
"type": "mysql",
"config": {
"user": "future",
"password": "123456",
"host": "api.lemonban.com",
"port": 3306
}
}
],
"host": "http://www.hhxpython.com/",
"headers": {},
"global_func": "",
"project": 1
}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
200 | 成功 | Inline |
返回数据结构
GET 查看环境
GET /test_envs/{id}/
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
id | path | string | 是 | none |
返回示例
200 Response
{}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
200 | 成功 | Inline |
二、后端代码
2.1 模型
class TestEnv(models.Model):
"""测试环境表"""
name = models.CharField(max_length=150, help_text='环境名称', verbose_name='环境名称')
project = models.ForeignKey(Project, on_delete=models.CASCADE, help_text='项目id', verbose_name='项目id',
related_name='test_envs')
global_variable = models.JSONField(help_text='全局变量', verbose_name='全局变量', default=dict, null=True,
blank=True)
debug_global_variable = models.JSONField(help_text='debug模式全局变量', verbose_name='debug模式全局变量',
default=dict, null=True, blank=True)
db = models.JSONField(help_text='数据库配置', verbose_name='数据库配置', default=list, null=True, blank=True)
host = models.CharField(help_text='base_url地址', verbose_name='base_url地址', max_length=100, blank=True)
headers = models.JSONField(help_text='请求头', verbose_name='请求头', default=dict, null=True, blank=True)
global_func = models.TextField(help_text='全局工具函数', verbose_name='全局工具函数',
default=open('./utils/global_func.py', 'r', encoding='utf-8').read(), null=True,
blank=True)
def __str__(self):
return self.name
class Meta:
db_table = 'tb_test_env'
verbose_name = "测试环境表"
verbose_name_plural = verbose_name
2.2 序列化器
class EnvSerializer(serializers.ModelSerializer):
class Meta:
model = TestEnv
fields = '__all__'
2.3 视图
class EnvViewSet(ModelViewSet):
queryset = TestEnv.objects.all()
serializer_class = EnvSerializer
def get_queryset(self):
"""复写此方法,实现过滤,project,type参数"""
queryset = super().get_queryset()
project = self.request.query_params.get('project')
if project:
queryset = queryset.filter(project=project)
return queryset
2.4 路由
from rest_framework.routers import DefaultRouter
from .views import ProjectViewSet, InterfaceViewSet, EnvViewSet
route = DefaultRouter()
route.register('projects', ProjectViewSet)
route.register('interfaces', InterfaceViewSet)
route.register('test_envs', EnvViewSet)
urlpatterns = route.urls