Python Django implements restful API

Recently, I am writing a test platform, and I need to implement an api of a node server. I am using django, and I am going to use the djangorestframework plugin to implement it.

  • need

    Implement an interface, when calling, through the passed parameters, directly run the automated test of the corresponding project

  • environment

    Python3.6 ,PyCharm,W7

  • Project structure
    write picture description here

  • Function realization

    • Process All
      Process flow
      we have to do is to implement the above process

    • 安装
      pip install djangorestframework
      pip install markdown
      pip install django-filter # Filtering support

    • configure

      INSTALLED_APPS = (
          ...
          'rest_framework',
      )
    • Write code (this code does not involve database operations, just simply write an api)

      ①: Open AutoApi/Api/views.py and write the following code

    from django.http import JsonResponse, HttpResponseNotAllowed, HttpResponse
    from django.views.decorators.csrf import csrf_exempt
    from rest_framework.parsers import JSONParser
    from rest_framework import status
    
    @csrf_exempt
    def run_job(request):
        # 判断请求头是否为json
        if request.content_type != 'application/json':  
            # 如果不是的话,返回405
            return HttpResponse('only support json data', status=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
        # 判断是否为post 请求
        if request.method == 'POST':
            try:
                # 解析请求的json格式入参
                data = JSONParser().parse(request)
            except Exception as why:
                print(why.args)
            else:
                content = {'msg': 'SUCCESS'}
                print(data)
                # 返回自定义请求内容content,200状态码
                return JsonResponse(data=content, status=status.HTTP_200_OK)
        # 如果不是post 请求返回不支持的请求方法
        return HttpResponseNotAllowed(permitted_methods=['POST'])

    ②: Open AutoApi/Api/urls.py and write the following code

    from django.conf.urls import url
    from Api import views
    
    urlpatterns = [
        url(r'^runJob/$',views.run_job),
    ]

    ③: Open AutoApi/AutoApi/urls.py and modify the following code

    ALLOWED_HOSTS = '*' # 修改为* 代码允许任意host
    
    
    from django.conf.urls import url,include
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^',include('Api.urls')),# 新增
    ]

    ④: Start the service

     python manage.py runserver 0.0.0.0:8080
    

    write picture description here

    ⑤: We request to try it out

    write picture description here


The above is a simple implementation of an api. In fact, the development interface is so simple and not so mysterious.

接下来把post 的数据env ,project,cases 解析出来传给对应的自动化测试入口函数,就可以实现通过接口请求,启动自动化测试的目的。

follow-up

  • Implement interface call automation test project
  • implement asynchronous interface
  • Implement timed tasks
    .
    .
    .

The above is a summary of some recent work, come on!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325464996&siteId=291194637