#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Date:2022/4/6 14:15
# Title:
import requests
def get_project_id(token) -> None or int:
'''
获取项目信息
:param token: 项目token
:return:
'''
data = requests.get(f"自己部署的Yapi服务地址/api/project/get?token={token}")
if data.status_code == 200:
data = data.json()
if data['errcode'] == 0:
data = data['data']
return data['_id']
return None
def add_api_class(token, name, project_id, desc) -> None or int:
'''
新增接口分类
:param token: 项目token
:param name: 接口分类名称
:param project_id: 项目id
:param desc: 分类描述
:return:
'''
data = {
'token': token,
'name': name,
'project_id': project_id,
'desc': desc
}
data = requests.post(f"自己部署的Yapi服务地址/api/interface/add_cat", data=data)
if data.status_code == 200:
data = data.json()
if data['errcode'] == 0:
data = data['data']
return data['_id']
return None
def add_api_info(token,title, path, project_id, catid, method='GET'):
'''添加接口信息'''
url = "自己部署的Yapi服务地址/api/interface/add"
data = {
"method": method,
"catid": catid,
"title": title,
"path": path,
"project_id": project_id,
"token":token
}
data = requests.post(url,data=data)
print(data.json())
if data.status_code == 200:
data = data.json()
if data['errcode'] == 0:
print(data)
def yapi_project_apis_init(token,api_class,apis):
'''自动生成yapi接口信息'''
if project_id := get_project_id(token):
for api_c in api_class:
add_api_class(token,api_c['name'], project_id, api_c['desc'])
if __name__=="__main__":
token = "xxx自己申请的token"
project_id = get_project_id(token)
catid=add_api_class(token,'hello', project_id, '测试!!')
add_api_info(token,'测试11', '/test', project_id, catid, method='GET')
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.