179 lines
6.0 KiB
Python
179 lines
6.0 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""自动上传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("自动上传Agent功能模块接口到Apifox")
|
||
print("=" * 80)
|
||
|
||
# 获取目录列表
|
||
print("\n检查Apifox目录结构...")
|
||
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', [])
|
||
|
||
# 递归查找Agent管理目录
|
||
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})")
|
||
else:
|
||
print(f"✗ 未找到'Agent管理'目录,将使用父目录'门店端-新版' (ID: {PARENT_FOLDER_ID})")
|
||
print(f"\n提示: 接口上传后,你可以在Apifox中手动创建'Agent管理'目录,然后将接口移动过去")
|
||
agent_folder_id = PARENT_FOLDER_ID
|
||
|
||
# Agent接口列表
|
||
apis = [
|
||
{
|
||
"name": "获取Agent模块列表",
|
||
"method": "GET",
|
||
"path": "/v2/store/agent/modules",
|
||
"folderId": int(agent_folder_id),
|
||
"description": "获取所有可用的Agent功能模块及其状态\n\n**功能模块**:\n- autoLike: 自动点赞\n- momentsSync: 朋友圈同步\n- autoCustomerDev: 自动开发客户\n- groupMessageDeliver: 群消息群发\n- autoGroup: 自动建群",
|
||
"tags": ["Agent管理"],
|
||
"parameters": {
|
||
"query": [
|
||
{
|
||
"name": "deviceId",
|
||
"type": "string",
|
||
"description": "设备ID(通过认证获取)",
|
||
"required": False
|
||
}
|
||
]
|
||
}
|
||
},
|
||
{
|
||
"name": "更新Agent模块状态",
|
||
"method": "PUT",
|
||
"path": "/v2/store/agent/modules/{moduleCode}/status",
|
||
"folderId": int(agent_folder_id),
|
||
"description": "启用或禁用指定的Agent功能模块\n\n**支持的模块代码**:\n- autoLike: 自动点赞\n- momentsSync: 朋友圈同步\n- autoCustomerDev: 自动开发客户\n- groupMessageDeliver: 群消息群发\n- autoGroup: 自动建群\n\n**数据存储**: 使用 ck_device_taskconf 表",
|
||
"tags": ["Agent管理"],
|
||
"parameters": {
|
||
"path": [
|
||
{
|
||
"name": "moduleCode",
|
||
"type": "string",
|
||
"description": "模块代码",
|
||
"required": True
|
||
}
|
||
]
|
||
},
|
||
"requestBody": {
|
||
"type": "application/json",
|
||
"jsonSchema": {
|
||
"type": "object",
|
||
"required": ["status"],
|
||
"properties": {
|
||
"status": {
|
||
"type": "integer",
|
||
"description": "状态:0-禁用,1-启用"
|
||
},
|
||
"deviceId": {
|
||
"type": "string",
|
||
"description": "设备ID"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
]
|
||
|
||
print(f"\n准备上传 {len(apis)} 个Agent接口...")
|
||
print("-" * 80)
|
||
|
||
success_count = 0
|
||
fail_count = 0
|
||
uploaded_apis = []
|
||
|
||
for i, api in enumerate(apis, 1):
|
||
print(f"\n[{i}/{len(apis)}] 上传: {api['name']}")
|
||
print(f" {api['method']} {api['path']}")
|
||
|
||
try:
|
||
response = requests.post(
|
||
f"https://api.apifox.com/api/v1/projects/{PROJECT_ID}/http-apis",
|
||
headers=headers,
|
||
json=api,
|
||
timeout=30
|
||
)
|
||
|
||
if response.status_code == 200:
|
||
result = response.json()
|
||
if result.get('success'):
|
||
api_id = result.get('data', {}).get('id')
|
||
success_count += 1
|
||
print(f" ✓ 成功 (API ID: {api_id})")
|
||
uploaded_apis.append({
|
||
'name': api['name'],
|
||
'method': api['method'],
|
||
'path': api['path'],
|
||
'id': api_id
|
||
})
|
||
else:
|
||
fail_count += 1
|
||
print(f" ✗ 失败: {result.get('errorMessage', '未知错误')}")
|
||
else:
|
||
fail_count += 1
|
||
print(f" ✗ HTTP {response.status_code}")
|
||
print(f" {response.text[:200]}")
|
||
except Exception as e:
|
||
fail_count += 1
|
||
print(f" ✗ 异常: {str(e)}")
|
||
|
||
print("\n" + "=" * 80)
|
||
print("上传完成!")
|
||
print("=" * 80)
|
||
print(f"✓ 成功: {success_count} 个")
|
||
print(f"✗ 失败: {fail_count} 个")
|
||
|
||
if uploaded_apis:
|
||
print("\n已上传的接口:")
|
||
for api in uploaded_apis:
|
||
print(f" - {api['method']:4s} {api['path']:40s} (ID: {api['id']})")
|
||
|
||
print(f"\n访问 Apifox 查看: https://app.apifox.com/project/{PROJECT_ID}")
|
||
|
||
if agent_folder_id == int(PARENT_FOLDER_ID):
|
||
print("\n" + "=" * 80)
|
||
print("后续步骤:")
|
||
print("1. 在Apifox中手动创建'Agent管理'目录(在'门店端-新版'下)")
|
||
print("2. 使用以下命令将接口移动到'Agent管理'目录:")
|
||
for api in uploaded_apis:
|
||
print(f" python apifox_manager.py move {api['id']} <Agent管理目录ID>")
|
||
print("=" * 80)
|
||
|