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()