Python counts the number of swagger interfaces to Excel documents

        Recently, all the interface documents of our company are placed in swagger. I have been trying to count the total number of interfaces, but I can’t find a good method. I can only transfer them to Excel through python, which is also convenient for jmeter to configure CSV files when running the interface in a loop. After various online The method experiment finally solved the problem, which is convenient for students who want to transfer Excel documents, and it is also convenient for statistics.

1. First save swagger as a document

 

Click the IP address in the upper left corner, and a json file will appear. Right-click and save as, it can be saved as a file in .json format, and the suffix of the saved and modified file will be in txt format.

 

2. Parse the json, analyze the content location of the interface address you want to get, and find the way to extract the data

Through analysis, we found that all interface information is placed in the key paths, and each interface address is a subkey, as shown in the following figure:

3. Use Python script to extract data and write to Excel 

I basically have thousands of module interfaces in the entire system, and there are quite a few. For the convenience of future use, in addition to the interface address, I also extracted the request method, interface classification, and interface description by the way, so that I can classify and sort out the desired interface in the Excel table in the future; The Python script is as follows:

 

import json
import xlwt

api_excel = xlwt.Workbook(encoding='utf-8')  # 创建一个文档
api_sheet = api_excel.add_sheet('listview接口')  # 添加一个sheet
json_file = open('D:\\jmxreport\\api-docs.txt', encoding='utf-8')  # 打开保存的swagger文本文档
api_data = json.load(json_file)  # 将文档json内容转换为Python对象
api = api_data['paths']  # 取swagger文件内容中的path,文件中path是键名
path_list = []  # 创建接口地址空列表
method_list = []  # 创建请求方式空列表
tags_list = []  # 创建接口分类空列表
summary_list = []  # 创建接口描述空列表
for path in api.keys():  # 循环取key
    values = api[path]  # 根据key获取值
    method = tuple(values.keys())[0]  # 获取请求方式,文件中请求方式是key
    path_list.append(path)  # 将path写入接口地址列表中
    method_list.append(method)  # 将method写入请求方式列表中
    if method == 'get':  # key为get时从get里面取分类和描述,key为post时从post里面取分类和描述
        tags = values['get']['tags'][0]  # 获取接口分类
        summary = values['get']['summary']  # 获取接口描述
        tags_list.append(tags)  # 将接口分类写入列表中
        summary_list.append(summary)  # 将接口描述写入列表中
    if method == 'post':
        tags = values['post']['tags'][0]
        summary = values['post']['summary']
        tags_list.append(tags)
        summary_list.append(summary)
for i in range(len(path_list)):  # 将接口path循环写入第一列
    api_sheet.write(i, 0, path_list[i])
for j in range(len(method_list)):  # 将请求方式循环写入第二列
    api_sheet.write(j, 1, method_list[j])
for m in range(len(tags_list)):  # 将接口分类循环写入第三列
    api_sheet.write(m, 2, tags_list[m])
for n in range(len(summary_list)):  # 将接口描述循环写入第四列
    api_sheet.write(n, 3, summary_list[n])
api_excel.save('D:\\jmxreport\\listview接口列表.xls')  # 保存文件

operation result:

---The End---

Good statistics or conversion methods will continue to be updated in the future, and everyone is welcome to communicate and help each other.

Please pay attention if it is helpful to you!

 

Guess you like

Origin blog.csdn.net/github_35856054/article/details/130216160
Recommended