python格式化swagger⽂档接⼝
⽣成接⼝格式的⽅法:
# -*- coding:utf8 -*-
import json
from idcm import interfaces
class Interface():
def writefilename(self,data):
try:
if data!='':
with open('E:\interface\\'+data+'.py', 'w+', encoding='utf-8') as f:
f.write('# -*- coding:utf8 -*-' + '\n')
f.close()
global olddata
olddata=data
return data
else:
return olddata
except Exception as e:
print(e)
def writedata(self,mode,data='',num=1,moudle='',): #data是指写⼊⽂件的数据,num是指循环写⼊的次数,moudle指⽂件头部导⼊的模块
filename = Interface().writefilename('')
try:
with open('E:\interface\\' + filename + '.py', 'a+', encoding='utf-8') as f:
if mode == 'importmethod':
f.write('import ' + data + '\n')
if mode == 'fromimportmethod':
f.write('from ' + moudle + ' import ' + data + '\n')
if mode == 'classname':
f.write('class' + ' ' + data + '():')
if mode == 'defname':
f.write('def' + ' ' + data + '(self):')
if mode == 'url' or mode == 'method':
f.write(mode + '=' + '"' + data + '"')
if mode == 'keyword':
for i in range(0, num):
f.write(' ')
if mode == 'newline':
for i in range(0, num):
f.write('\n')
if mode == 'keywordnewlinefour':
for i in range(0, num):
f.write('\n')
for i in range(0, num+3):
f.write(' ')
if mode == 'keywordnewlineeight':
for i in range(0, num):
f.write('\n')
for i in range(0, num+7):
f.write(' ')
if mode == 'return': f.write('return ' + 'url,' + 'method,'+'parameter')
if mode == 'parameters':
parameters={}
for i in range(0, num):
if data[i]!='':
parameters[data[i]]='' #将list对象写成dict
else:
pass
parameters=json.dumps(parameters) #将字典序列化成json写⼊⽂件
place('"parameters[','parameters[').replace(']"',']') #把parameters⾥⾯的value值的双引号去掉,让value变成变量
f.write('parameters='+parameters)
f.close()
except Exception as e:
print(e)
if __name__=='__main__':
intf=interfaces.Requestss().getassetslist()
print(len(intf[3][0]),len(intf[3][1]))
Interface().writefilename('swagger')
Interface().writedata('importmethod','json')
Interface().writedata('fromimportmethod','interfaces',1,'idcm') Interface().writedata('newline', '', 3)
Interface().writedata('classname','Swaggers')
url='***'
for i in range(0,len(intf[0])):
Interface().writedata('keywordnewlinefour', '',1)
Interface().writedata('defname', intf[0][i])
Interface().writedata('keywordnewlineeight', '',1)
Interface().writedata('method', intf[1][i])
Interface().writedata('keywordnewlineeight', '',1)
Interface().writedata('url', url+intf[2][i])
Interface().writedata('keywordnewlineeight', '',1)
Interface().writedata('parameters', intf[3][i],len(intf[3][i])) Interface().writedata('keywordnewlineeight', '',1)
Interface().writedata('return')
获取swagger接⼝参数的⽅法:
# -*- coding:utf-8 -*-
import requests
import json
import time
class Requestss():
def getassetslist(self):
url = '***/swagger/v1/swagger.json'
dat = (url)
dat = dat.json()
path=dat['paths']
method = []
interfaces=[]
parameters = []
# num=0
# for key,value in path.items(): #获取dict的键,值
# num+=1
# interface.append(key)
# method.append(value)
for key in path:
keys=key.split('/') #如把/api/Advertising/GetBuyInAmount分割取接⼝名称GetBuyInAmount
interfaces.append(key)
keyss=keys[len(keys)-1] #取/api/Advertising/GetBuyInAmount以‘\’分割为列表的最后的⼀个数据为接⼝名称if '{' in keyss:
keyss=keys[len(keys)-2] #如果最后⼀个接⼝带有{},则往上取⼀个数据为接⼝名称
interface.append(keyss)
for value in path[key]: #取接⼝的⽅法,如post,get
method.append(value)
for k,v in path[key].items():#取各接⼝的请求的参数,取字典key,value的值
#print(k,v)
parameter = []
if v['parameters']!=[]:
for para in range(0, len(v['parameters'])):
parameter.append(v['parameters'][para]['name'])
else:
parameter.append('')
parameters.append(parameter)
print(len(parameters),parameters)
# for i in range(0,num):
# #print(path[keys[i]])
# for key in path[interface[i]]:
# pamas = path[interface[i]][key]['parameters']
# parameters=[]
# for p in range (0,len(pamas)):
python官方文档中文版# parameters.append(path[interface[i]][key]['parameters'][p]['name']) # #print(pama)
# #print(keys[i], key, parameters)
# else:
# #print(keys[i], key)
# pass
# method.append(key)
return interface,method,interfaces,parameters
if __name__=='__main__':
Requestss().getassetslist()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论