swagger文档导入接口到httprunnermanager框架

#!/usr/bin/python
import requests
import json
import pymysql

def importdemo():
    #配置数据库连接
    db = pymysql.connect("你的服务地址", "你的用户名", "你的密码", "你的数据库")
    cursor = db.cursor()
    sql = "INSERT INTO testcaseinfo(create_time,update_time,type,name,belong_project,include,author,request,belong_module_id) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s')"

    #获取文档内容
    page = requests.get("http://你的域名/activityCenter/v2/api-docs")
    pagevalue  = page.content.decode("utf-8")
    dict1 = json.loads(pagevalue)

    print("dist1的类型:"+str(type(dict1)))
    #获取出paths中的
    keys = list(dict1['paths'].keys())
    print(keys)

    #所有的url键和值
    path = dict1.get('paths')
    print("path的类型:"+str(type(path)))
    baseurl = "http://你的域名"
    message = "message"
    #取出每个url和对应的值
    for key in keys:
        param = []
        data = {}
        request = {}
        test = {}
        interface = {}
        url = ''
        summary = ''#接口名称
        body = path[str(key)]
        if(list(body.keys())[0]=="post"):
            summary = body['post']['summary']
            parametersbody = body['post']['parameters']
            for v in parametersbody:
                value =v['name']
                param.append(value)
                data[value] = ''
                print("------------")
                print(str(v['name']))
                print(type(v['name']))
                # print(str(param))
                print("------------")
        url = baseurl + "/" +message + key
        if(list(body.keys())[0]=="post"):
            method = "POST"
        else:
            method = "GET"
        #封装request
        request['url'] = url
        request['method'] = method
        request['data'] = data
        request['headers'] = ''
        #封装test
        test['request'] = request
        #封装最外层
        interface['test'] = test
        interface['name'] = summary
        #将字典转化成字符串
        interfacestr = str(interface)
        #将每个分号前加上转义符
        cc = pymysql.escape_string(interfacestr)
        cursor.execute(sql % ('2019-04-24 11:00:00', '2019-04-24 11:00:00', '1', str(summary), '兼职', '[]', 'chenchi', cc , '1'))
        db.commit()
        print("------------")
        print(str(param))
        print(type(param))
        print("data:"+str(data))
        print("url:"+ url)
        print("requset:"+str(request))
        print("test:"+str(test))
        print("interface:"+str(interface))
        print("interface:" + str(type(interface)))
        print("------------")


if __name__ == '__main__':
    importdemo()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值