django-8-环境接口

一、环境接口管理

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

Created

成功

Inline

返回数据结构

GET 查看环境列表

GET /test_envs/

请求参数

名称

位置

类型

必选

说明

project

query

integer

none

返回示例

200 Response

{}

返回结果

状态码

状态码含义

说明

数据模型

200

OK

成功

Inline

返回数据结构

DELETE 删除环境

DELETE /test_envs/{id}/

请求参数

名称

位置

类型

必选

说明

id

path

string

none

返回示例

204 Response

{}

返回结果

状态码

状态码含义

说明

数据模型

204

No Content

删除成功

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

OK

成功

Inline

返回数据结构

GET 查看环境

GET /test_envs/{id}/

请求参数

名称

位置

类型

必选

说明

id

path

string

none

返回示例

200 Response

{}

返回结果

状态码

状态码含义

说明

数据模型

200

OK

成功

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值