171 lines
6.1 KiB
Python
171 lines
6.1 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
最终方案:自动整理Agent接口
|
||
由于Apifox API对目录创建有限制,提供两种方案:
|
||
1. 手动创建目录后自动移动接口(推荐)
|
||
2. 使用OpenAPI导入(如果API权限足够)
|
||
"""
|
||
import sys
|
||
import requests
|
||
import json
|
||
|
||
sys.stdout.reconfigure(encoding='utf-8')
|
||
|
||
TOKEN = "afxp_fa4137GfIzJtlOCeQHjpCXc83SFWmeAOrZnq"
|
||
PROJECT_ID = "6037107"
|
||
PARENT_FOLDER_ID = 78015216 # 门店端-新版
|
||
AGENT_API_IDS = [415861964, 415861967] # 已上传的Agent接口ID
|
||
|
||
headers = {
|
||
"X-Apifox-Api-Version": "2024-03-28",
|
||
"Authorization": f"Bearer {TOKEN}",
|
||
"Content-Type": "application/json; charset=utf-8"
|
||
}
|
||
|
||
print("=" * 80)
|
||
print("Agent接口整理工具")
|
||
print("=" * 80)
|
||
|
||
# 方案1: 检查是否已存在Agent管理目录
|
||
print("\n[方案1] 检查现有目录...")
|
||
response = requests.get(
|
||
f"https://api.apifox.com/api/v1/projects/{PROJECT_ID}/api-tree-list",
|
||
headers=headers
|
||
)
|
||
|
||
agent_folder_id = None
|
||
if response.status_code == 200:
|
||
tree_data = response.json().get('data', [])
|
||
|
||
def find_agent_folder(items):
|
||
for item in items:
|
||
if item.get('type') == 'apiDetailFolder':
|
||
folder = item.get('folder', {})
|
||
if item.get('name') == 'Agent管理' and folder.get('parentId') == int(PARENT_FOLDER_ID):
|
||
return folder.get('id')
|
||
for child in item.get('children', []):
|
||
result = find_agent_folder([child])
|
||
if result:
|
||
return result
|
||
return None
|
||
|
||
agent_folder_id = find_agent_folder(tree_data)
|
||
|
||
if agent_folder_id:
|
||
print(f" ✓ 找到'Agent管理'目录 (ID: {agent_folder_id})")
|
||
print(f"\n开始移动接口...")
|
||
|
||
success = 0
|
||
failed = 0
|
||
|
||
for i, api_id in enumerate(AGENT_API_IDS, 1):
|
||
print(f"\n [{i}/{len(AGENT_API_IDS)}] 移动接口 {api_id}...")
|
||
|
||
try:
|
||
move_resp = requests.patch(
|
||
f"https://api.apifox.com/api/v1/projects/{PROJECT_ID}/http-apis/{api_id}",
|
||
headers=headers,
|
||
json={"folderId": int(agent_folder_id)},
|
||
timeout=10
|
||
)
|
||
|
||
if move_resp.status_code == 200:
|
||
try:
|
||
result = move_resp.json()
|
||
if result.get('success'):
|
||
success += 1
|
||
print(f" ✓ 移动成功")
|
||
else:
|
||
failed += 1
|
||
print(f" ✗ 失败: {result.get('errorMessage', '未知错误')}")
|
||
except json.JSONDecodeError:
|
||
# 检查响应内容
|
||
if move_resp.text.strip():
|
||
print(f" ⚠️ 响应不是JSON: {move_resp.text[:200]}")
|
||
# 如果响应是空或特殊格式,可能已经成功
|
||
if move_resp.text.strip() == '' or 'success' in move_resp.text.lower():
|
||
success += 1
|
||
print(f" ✓ 可能已移动成功(响应格式异常)")
|
||
else:
|
||
failed += 1
|
||
else:
|
||
# 空响应可能表示成功
|
||
success += 1
|
||
print(f" ✓ 移动成功(空响应)")
|
||
else:
|
||
failed += 1
|
||
print(f" ✗ HTTP {move_resp.status_code}: {move_resp.text[:200]}")
|
||
except Exception as e:
|
||
failed += 1
|
||
print(f" ✗ 异常: {str(e)}")
|
||
|
||
print("\n" + "=" * 80)
|
||
print("完成!")
|
||
print("=" * 80)
|
||
print(f"✓ 成功移动: {success} 个接口")
|
||
print(f"✗ 失败: {failed} 个接口")
|
||
|
||
if success > 0:
|
||
print(f"\n✨ Agent接口已整理到'Agent管理'目录")
|
||
print(f"📁 目录ID: {agent_folder_id}")
|
||
print(f"🌐 访问查看: https://app.apifox.com/project/{PROJECT_ID}")
|
||
|
||
else:
|
||
print(" ✗ 未找到'Agent管理'目录")
|
||
print("\n" + "=" * 80)
|
||
print("请选择操作方式:")
|
||
print("=" * 80)
|
||
print("\n【方式A】手动创建目录后移动接口(推荐)")
|
||
print(" 1. 打开 https://app.apifox.com/project/6037107")
|
||
print(" 2. 在'门店端-新版'下创建'Agent管理'目录")
|
||
print(" 3. 运行: python final_organize_agent.py <目录ID>")
|
||
print("\n【方式B】直接提供目录ID")
|
||
print(" 如果你已经知道目录ID,直接运行:")
|
||
print(f" python move_to_agent_folder.py <目录ID>")
|
||
print("\n" + "=" * 80)
|
||
|
||
# 如果提供了命令行参数(目录ID)
|
||
if len(sys.argv) > 1:
|
||
folder_id = sys.argv[1]
|
||
print(f"\n使用提供的目录ID: {folder_id}")
|
||
print("开始移动接口...")
|
||
|
||
success = 0
|
||
failed = 0
|
||
|
||
for i, api_id in enumerate(AGENT_API_IDS, 1):
|
||
print(f"\n [{i}/{len(AGENT_API_IDS)}] 移动接口 {api_id}...")
|
||
|
||
try:
|
||
move_resp = requests.patch(
|
||
f"https://api.apifox.com/api/v1/projects/{PROJECT_ID}/http-apis/{api_id}",
|
||
headers=headers,
|
||
json={"folderId": int(folder_id)},
|
||
timeout=10
|
||
)
|
||
|
||
if move_resp.status_code == 200:
|
||
result = move_resp.json()
|
||
if result.get('success'):
|
||
success += 1
|
||
print(f" ✓ 移动成功")
|
||
else:
|
||
failed += 1
|
||
print(f" ✗ 失败: {result.get('errorMessage', '未知错误')}")
|
||
else:
|
||
failed += 1
|
||
print(f" ✗ HTTP {move_resp.status_code}")
|
||
except Exception as e:
|
||
failed += 1
|
||
print(f" ✗ 异常: {str(e)}")
|
||
|
||
print("\n" + "=" * 80)
|
||
print("完成!")
|
||
print("=" * 80)
|
||
print(f"✓ 成功移动: {success} 个接口")
|
||
print(f"✗ 失败: {failed} 个接口")
|
||
|
||
print("\n" + "=" * 80)
|
||
|