94 lines
2.8 KiB
Python
94 lines
2.8 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""通过OpenAPI导入创建目录并导入接口"""
|
||
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)
|
||
|
||
# 尝试导入OpenAPI(指定父目录,看是否能自动创建子目录)
|
||
print("\n[2/3] 导入OpenAPI到指定目录...")
|
||
|
||
# 方法1: 使用import-openapi端点,指定folderId
|
||
import_url = f"https://api.apifox.com/api/v1/projects/{PROJECT_ID}/import-openapi"
|
||
|
||
# 尝试不同的导入方式
|
||
import_methods = [
|
||
{
|
||
"name": "直接导入(指定父目录)",
|
||
"data": {
|
||
"openapi": json.dumps(openapi_data),
|
||
"folderId": int(PARENT_FOLDER_ID),
|
||
"mergeMode": "smart" # smart, overwrite, skip
|
||
}
|
||
},
|
||
{
|
||
"name": "导入(带目录路径)",
|
||
"data": {
|
||
"openapi": json.dumps(openapi_data),
|
||
"folderPath": "门店端-新版/Agent管理",
|
||
"mergeMode": "smart"
|
||
}
|
||
}
|
||
]
|
||
|
||
for method in import_methods:
|
||
print(f"\n 尝试: {method['name']}")
|
||
try:
|
||
resp = requests.post(
|
||
import_url,
|
||
headers=headers,
|
||
json=method['data'],
|
||
timeout=30
|
||
)
|
||
|
||
print(f" 状态码: {resp.status_code}")
|
||
|
||
if resp.status_code == 200:
|
||
try:
|
||
result = resp.json()
|
||
if result.get('success'):
|
||
imported = result.get('data', {}).get('imported', 0)
|
||
print(f" ✓ 导入成功,导入 {imported} 个接口")
|
||
print(f" 结果: {json.dumps(result, ensure_ascii=False, indent=2)[:300]}")
|
||
break
|
||
else:
|
||
print(f" ✗ 导入失败: {result.get('errorMessage', '未知错误')}")
|
||
except json.JSONDecodeError:
|
||
print(f" ✗ 非JSON响应: {resp.text[:200]}")
|
||
else:
|
||
print(f" ✗ HTTP {resp.status_code}: {resp.text[:200]}")
|
||
except Exception as e:
|
||
print(f" ✗ 异常: {str(e)}")
|
||
|
||
print("\n" + "=" * 80)
|
||
print("提示: 如果导入成功,接口会自动创建在指定目录下")
|
||
print("如果目录不存在,Apifox可能会自动创建,或者需要手动创建")
|
||
print("=" * 80)
|
||
|