python实现swagger导出json格式不同版本的接口对比

解决的问题:将swagger上的接口保存成json文件,对比不同版本中json文件,将新增,删除,修改的接口输出到txt文件

import json

newFilePath = "./newApi211.json"
oldFilePath = "./oldApi241.json"

def readJsonFile(filePath):  #获取到仅是接口的json数据
    file = open(filePath, "rb")
    fileJson =json.load(file)
    apis = fileJson['paths']

    return apis

# 将接口字典中的key转成list
def keyList(dict):
    keyList = []
    for key in dict.keys():
        keyList.append(key)
    # return tempList,len(tempList)
    return keyList

# 找出变动的接口
def checkAPIs( ):
    newJson = readJsonFile(newFilePath)
    oldJson = readJsonFile(oldFilePath)

    newKeys = keyList(newJson)
    oldKeys = keyList(oldJson)

    resFile = open("./result.txt", 'w')

    sameKey = []  # 存放接口中共有的url
    # 1.找出新增的接口 2.收集相同接口的url
    for i in range(0,len(newKeys)):
        if newKeys[i] not in oldKeys:
            # print("新接口:{} ".format(newKeys[i]))
            resFile.write("001新增的接口:"+ newKeys[i] + "\n")
        else:
            #print("没有新增的接口!")
            sameKey.append(newKeys[i])

    # 2.找出去掉的接口
    for i in range(0,len(oldKeys)):
        if oldKeys[i] not in newKeys:
            # print("新接口:{} ".format(newKeys[i]))
            resFile.write("n002去掉的接口:" + newKeys[i] + "\n")
        else:
            pass

    #3. 找出改动接口中具体内层的变化
    for j in range(0,len(sameKey)):
        try:
            if newJson[sameKey[j]] != oldJson[sameKey[j]]:
                inNewJson = newJson[sameKey[j]]
                inOldJson = oldJson[sameKey[j]]
                inNewKeys = keyList(inNewJson)
                inOldKeys = keyList(inOldJson)
                inSameKey = []
                # 1.找出改动接口中新增的请求方法 2.收集未改动的请求方法
                for i in range(0, len(inNewKeys)):
                    if inNewKeys[i] not in inOldKeys:
                        # print("新接口:{} ".format(newKeys[i]))
                        resFile.write("003改动的接口:" + sameKey[j] + "--301新增了请求方法:" + inNewKeys[i] + "\n")
                    else:
                        inSameKey.append(inNewKeys[i])
                # 2.找出已经去掉的请求方法
                for i in range(0, len(inOldKeys)):
                    if inOldKeys[i] not in inNewKeys:
                        resFile.write("003改动的接口:" + sameKey[j] + "--302去掉了请求方法:" + inNewKeys[i] + "\n")
                    else:
                        pass
                # 3.找出改动的请求方法
                for i in range(0, len(inSameKey)):
                    if inNewJson[inSameKey[i]] != inOldJson[inSameKey[i]]:
                        resFile.write("003改动的接口:" + sameKey[j] + "--303改动了请求方法:" + inSameKey[i] + "\n")
        except KeyError as e:
            print(e)
        except Exception as e:
            print(e)
    resFile.close()

if __name__ == '__main__':
    checkAPIs()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值