137 lines
5.1 KiB
Python
137 lines
5.1 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""使用OpenAPI导入自动创建Agent管理目录并导入接口"""
|
||
import sys
|
||
import requests
|
||
import json
|
||
|
||
sys.stdout.reconfigure(encoding='utf-8')
|
||
|
||
TOKEN = "afxp_fa4137GfIzJtlOCeQHjpCXc83SFWmeAOrZnq"
|
||
PROJECT_ID = "6037107"
|
||
PARENT_FOLDER_ID = 78015216 # 门店端-新版
|
||
|
||
headers = {
|
||
"X-Apifox-Api-Version": "2024-03-28",
|
||
"Authorization": f"Bearer {TOKEN}",
|
||
"Content-Type": "application/json; charset=utf-8"
|
||
}
|
||
|
||
print("=" * 80)
|
||
print("使用OpenAPI导入自动创建Agent管理目录")
|
||
print("=" * 80)
|
||
|
||
# 读取OpenAPI文件
|
||
print("\n[1/3] 读取OpenAPI文件...")
|
||
try:
|
||
with open('agent_openapi.json', 'r', encoding='utf-8') as f:
|
||
openapi_data = json.load(f)
|
||
print(" ✓ OpenAPI文件读取成功")
|
||
except Exception as e:
|
||
print(f" ✗ 读取失败: {e}")
|
||
sys.exit(1)
|
||
|
||
# 转换为JSON字符串
|
||
openapi_string = json.dumps(openapi_data, ensure_ascii=False)
|
||
|
||
# 步骤2: 使用正确的导入API格式
|
||
print("\n[2/3] 导入OpenAPI(自动创建目录)...")
|
||
print(f" 目标父目录ID: {PARENT_FOLDER_ID}")
|
||
|
||
# 根据文档,端点格式:POST /v1/projects/{projectId}/import-openapi
|
||
# 可以添加locale查询参数
|
||
import_url = f"https://api.apifox.com/api/v1/projects/{PROJECT_ID}/import-openapi?locale=zh-CN"
|
||
|
||
# 根据文档,正确的格式是:
|
||
# input: 可以是字符串(OpenAPI JSON字符串)或对象(包含url)
|
||
# options: 包含targetEndpointFolderId等选项
|
||
payload = {
|
||
"input": openapi_string, # 直接传入JSON字符串
|
||
"options": {
|
||
"targetEndpointFolderId": PARENT_FOLDER_ID, # 指定父目录
|
||
"endpointOverwriteBehavior": "CREATE_NEW", # 创建新接口(避免覆盖已存在的)
|
||
"updateFolderOfChangedEndpoint": True, # 更新接口目录
|
||
"prependBasePath": False # 不添加基础路径
|
||
}
|
||
}
|
||
|
||
print(f"\n 发送导入请求...")
|
||
try:
|
||
response = requests.post(
|
||
import_url,
|
||
headers=headers,
|
||
json=payload,
|
||
timeout=60 # 导入可能需要较长时间
|
||
)
|
||
|
||
print(f" 状态码: {response.status_code}")
|
||
print(f" 响应头: {dict(response.headers)}")
|
||
print(f" 响应内容前500字符: {response.text[:500]}")
|
||
|
||
if response.status_code == 200:
|
||
# 检查响应是否是JSON
|
||
if response.text.strip().startswith('{') or response.text.strip().startswith('['):
|
||
result = response.json()
|
||
else:
|
||
print(f"\n⚠️ 响应不是JSON格式,可能是异步导入")
|
||
print(f" 完整响应: {response.text}")
|
||
print(f"\n提示: Apifox导入可能是异步的,请稍后在Web界面查看结果")
|
||
sys.exit(0)
|
||
|
||
if result.get('success'):
|
||
data = result.get('data', {})
|
||
counters = data.get('counters', {})
|
||
|
||
print("\n" + "=" * 80)
|
||
print("导入成功!")
|
||
print("=" * 80)
|
||
print(f"\n📊 导入统计:")
|
||
print(f" ✓ 新增接口: {counters.get('endpointCreated', 0)}")
|
||
print(f" ✓ 更新接口: {counters.get('endpointUpdated', 0)}")
|
||
print(f" ✓ 新增目录: {counters.get('endpointFolderCreated', 0)}")
|
||
print(f" ✓ 更新目录: {counters.get('endpointFolderUpdated', 0)}")
|
||
print(f" ✗ 失败接口: {counters.get('endpointFailed', 0)}")
|
||
print(f" ✗ 失败目录: {counters.get('endpointFolderFailed', 0)}")
|
||
|
||
# 检查是否有错误
|
||
errors = data.get('errors', [])
|
||
if errors:
|
||
print(f"\n⚠️ 错误信息:")
|
||
for error in errors:
|
||
print(f" - {error.get('message', '未知错误')} (代码: {error.get('code', 'N/A')})")
|
||
|
||
folder_created = counters.get('endpointFolderCreated', 0)
|
||
if folder_created > 0:
|
||
print(f"\n✨ 成功创建 {folder_created} 个目录!")
|
||
print(f"📁 'Agent管理'目录已自动创建在'门店端-新版'下")
|
||
|
||
endpoint_created = counters.get('endpointCreated', 0)
|
||
if endpoint_created > 0:
|
||
print(f"\n✨ 成功导入 {endpoint_created} 个接口到'Agent管理'目录")
|
||
|
||
print(f"\n🌐 访问查看: https://app.apifox.com/project/{PROJECT_ID}")
|
||
|
||
else:
|
||
print(f"\n✗ 导入失败: {result.get('errorMessage', '未知错误')}")
|
||
print(f" 完整响应: {json.dumps(result, ensure_ascii=False, indent=2)}")
|
||
else:
|
||
print(f"\n✗ HTTP {response.status_code}")
|
||
print(f" 响应内容: {response.text[:500]}")
|
||
|
||
except Exception as e:
|
||
print(f"\n✗ 异常: {str(e)}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
|
||
print("\n" + "=" * 80)
|
||
|
||
# 步骤3: 如果导入成功,删除之前上传的重复接口(可选)
|
||
print("\n[3/3] 检查是否需要清理重复接口...")
|
||
print(" 提示: 如果之前已上传过接口,现在可能会有重复")
|
||
print(" 建议: 在Apifox中手动删除旧接口,或使用脚本移动")
|
||
|
||
print("\n" + "=" * 80)
|
||
print("完成!")
|
||
print("=" * 80)
|
||
|