django-7-项目借口管理

一、项目接口设计

POST 接口创建

POST /interfaces/

Body 请求参数

{
  "name": "登录",
  "url": "/users/login/",
  "method": "post",
  "type": "1",
  "project": 1
}

请求参数

名称

位置

类型

必选

说明

body

body

object

none

» name

body

string

none

» url

body

string

none

» method

body

string

none

» type

body

string

none

» project

body

integer

none

返回示例

成功

{
  "id": 1,
  "steps": [],
  "name": "登录",
  "url": "/users/login/",
  "method": "post",
  "type": "1",
  "project": 1
}

返回结果

状态码

状态码含义

说明

数据模型

201

Created

成功

Inline

返回数据结构

状态码 201

名称

类型

必选

约束

中文名

说明

» id

integer

true

none

none

» steps

[string]

true

none

none

» name

string

true

none

none

» url

string

true

none

none

» method

string

true

none

none

» type

string

true

none

none

» project

integer

true

none

none

GET 查看接口列表

GET /interfaces/

请求参数

名称

位置

类型

必选

说明

project

query

string

none

type

query

string

none

返回示例

成功

[
  {
    "id": 1,
    "steps": [],
    "name": "登录",
    "url": "/users/login/",
    "method": "post",
    "type": "1",
    "project": 1
  }
]

返回结果

状态码

状态码含义

说明

数据模型

200

OK

成功

Inline

返回数据结构

DELETE 删除接口

DELETE /interfaces/{id}/

请求参数

名称

位置

类型

必选

说明

id

path

string

none

返回示例

204 Response

{}

返回结果

状态码

状态码含义

说明

数据模型

204

No Content

删除成功

Inline

返回数据结构

PUT 修改接口

PUT /interfaces/{id}/

Body 请求参数

{
  "name": "登录",
  "url": "/users/login/",
  "method": "post",
  "type": "1",
  "project": 1
}

请求参数

名称

位置

类型

必选

中文名

说明

id

path

string

none

body

body

object

none

» name

body

string

none

» url

body

string

none

» method

body

string

none

» type

body

string

接口类型

none

» project

body

integer

none

返回示例

成功

{
  "id": 1,
  "steps": [],
  "name": "登录",
  "url": "/users/login/",
  "method": "post",
  "type": "1",
  "project": 1
}

返回结果

状态码

状态码含义

说明

数据模型

200

OK

成功

Inline

返回数据结构

GET 查看接口

GET /interfaces/{id}/

请求参数

名称

位置

类型

必选

中文名

说明

id

path

string

none

返回示例

成功

{
  "id": 1,
  "steps": [],
  "name": "登录",
  "url": "/users/login/",
  "method": "post",
  "type": "1",
  "project": 1
}

返回结果

状态码

状态码含义

说明

数据模型

200

OK

成功

Inline

二、后端代码

2.1 模型

class Interface(models.Model):
    """接口表"""
    CHOICES = [
        ('1', '项目接口'),
        ('2', '外部接口')
    ]
    project = models.ForeignKey(Project, on_delete=models.CASCADE, help_text='项目id', verbose_name='项目id',
                                related_name='interfaces')
    name = models.CharField(max_length=50, help_text='接口名称', verbose_name='接口名')
    url = models.CharField(max_length=200, help_text='接口路径', verbose_name='接口路径')
    method = models.CharField(max_length=50, help_text='请求方法', verbose_name='请求方法')
    type = models.CharField(verbose_name='接口类型', help_text='接口类型', max_length=40, choices=CHOICES, default='1')

    # 扩展,接口说明,接口参数...

    def __str__(self):
        return self.url

    class Meta:
        db_table = 'tb_interface'
        verbose_name = '接口表'
        verbose_name_plural = verbose_name

2.2 序列化器

class InterfaceSerializer(serializers.ModelSerializer):

    class Meta:
        model = Interface
        fields = '__all__'

    def validate(self, attrs):
        url = attrs.get('url')
        type_ = attrs.get('type')
        if type_ == '2':
            if not url.startswith('http'):
                raise serializers.ValidationError('外部接口的url需要完整的url,必须以http://或https://开头')

        return attrs

2.3 视图

class InterfaceViewSet(ModelViewSet):
    queryset = Interface.objects.all()
    serializer_class = InterfaceSerializer

    def get_queryset(self):
        """复写此方法,实现过滤,project,type参数"""
        queryset = super().get_queryset()

        project = self.request.query_params.get('project')
        type_ = self.request.query_params.get('type')
        if project:
            queryset = queryset.filter(project=project)
        if type_:
            queryset = queryset.filter(type=type_)

        return queryset

2.4 路由

from rest_framework.routers import DefaultRouter

from . import views

router = DefaultRouter()
router.register('projects', views.ProjectViewSet)
router.register('interfaces', views.InterfaceViewSet)
router.register('test_envs', views.EnvViewSet)

urlpatterns = router.urls

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值