# -*- coding: utf-8 -*- import sys import requests import json sys.stdout.reconfigure(encoding='utf-8') TOKEN = "afxp_fa4137GfIzJtlOCeQHjpCXc83SFWmeAOrZnq" PROJECT_ID = "6037107" BASE_URL = "https://api.apifox.com/api/v1" STORE_FOLDER_ID = "78015216" # 门店端-新版 headers = { "X-Apifox-Api-Version": "2024-03-28", "Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json; charset=utf-8" } # 流量采购接口ID列表 TRAFFIC_API_IDS = [ "415976880", # 获取可购买的流量池包列表 "415976882", # 获取流量池包详情 "415977273", # 购买流量 "415976885", # 获取已购买的流量列表 "415976886", # 获取购买记录列表 "415976889", # 获取购买记录详情 "415976892" # 获取流量采购统计 ] def get_folder_tree(): """获取项目目录树""" try: response = requests.get( f"{BASE_URL}/projects/{PROJECT_ID}/api-tree-list", headers=headers, timeout=30 ) if response.status_code == 200: result = response.json() if result.get('success'): return result.get('data', []) return [] except Exception as e: print(f"获取目录树失败: {e}") return [] def create_folder(parent_id, name): """创建目录""" try: data = { "name": name, "parentId": parent_id, "type": "http" } response = requests.post( f"{BASE_URL}/projects/{PROJECT_ID}/folders", headers=headers, json=data, timeout=30 ) if response.status_code == 200: result = response.json() if result.get('success'): return result.get('data', {}).get('id'), None else: return None, result.get('errorMessage', 'Unknown error') else: return None, f"HTTP {response.status_code}: {response.text[:200]}" except Exception as e: return None, str(e) def find_folder_by_name(tree, name, parent_id=None): """在目录树中查找指定名称的目录""" for item in tree: if item.get('type') == 'folder': item_id = item.get('id') item_name = item.get('name', '') item_parent = item.get('parentId') # 检查是否匹配 if item_name == name: if parent_id is None or item_parent == parent_id: return item_id # 递归查找子目录 children = item.get('children', []) if children: found = find_folder_by_name(children, name, parent_id) if found: return found return None def move_api(api_id, folder_id): """移动API到指定目录""" try: # 先获取API详情 response = requests.get( f"{BASE_URL}/projects/{PROJECT_ID}/http-apis/{api_id}", headers=headers, timeout=30 ) if response.status_code != 200: return False, f"获取API失败: HTTP {response.status_code}" api_data = response.json().get('data') if not api_data: return False, "API不存在" # 更新folderId api_data['folderId'] = folder_id # 更新API update_response = requests.put( f"{BASE_URL}/projects/{PROJECT_ID}/http-apis/{api_id}", headers=headers, json=api_data, timeout=30 ) if update_response.status_code == 200: result = update_response.json() if result.get('success'): return True, None else: return False, result.get('errorMessage', 'Unknown error') else: return False, f"HTTP {update_response.status_code}: {update_response.text[:200]}" except Exception as e: return False, str(e) print("=" * 60) print("创建流量采购管理目录并移动接口...") print("=" * 60) # 1. 获取目录树 print("\n【1/3】获取目录树...") tree = get_folder_tree() if not tree: print("❌ 无法获取目录树") sys.exit(1) print("✅ 目录树获取成功") # 2. 检查目录是否已存在 print("\n【2/3】检查目录是否已存在...") existing_folder_id = find_folder_by_name(tree, "流量采购管理", STORE_FOLDER_ID) if existing_folder_id: print(f"✅ 目录已存在 (ID: {existing_folder_id})") folder_id = existing_folder_id else: print("📁 目录不存在,开始创建...") folder_id, error = create_folder(STORE_FOLDER_ID, "流量采购管理") if folder_id: print(f"✅ 目录创建成功 (ID: {folder_id})") else: print(f"❌ 目录创建失败: {error}") print("\n⚠️ 如果API创建失败,请手动在Apifox Web界面创建目录") print(f" 目录名称: 流量采购管理") print(f" 父目录: 门店端-新版 (ID: {STORE_FOLDER_ID})") sys.exit(1) # 3. 移动接口 print(f"\n【3/3】移动接口到目录 (ID: {folder_id})...") success_count = 0 fail_count = 0 for i, api_id in enumerate(TRAFFIC_API_IDS, 1): print(f" [{i}/{len(TRAFFIC_API_IDS)}] 移动接口 (ID: {api_id})...", end=" ") success, error = move_api(api_id, folder_id) if success: print("✅") success_count += 1 else: print(f"❌ {error}") fail_count += 1 # 输出结果 print("\n" + "=" * 60) print("✅ 完成!") print(f"\n📊 统计:") print(f" - 目录ID: {folder_id}") print(f" - 成功移动: {success_count}/{len(TRAFFIC_API_IDS)}") print(f" - 失败: {fail_count}") if fail_count > 0: print(f"\n⚠️ 有 {fail_count} 个接口移动失败") print(" 如果API移动失败,请手动在Apifox Web界面移动接口") print(f" 目标目录: 流量采购管理 (ID: {folder_id})") print(f"\n🔗 访问链接:") print(f" https://app.apifox.com/project/{PROJECT_ID}") print(f" 流量采购管理目录: https://app.apifox.com/project/{PROJECT_ID}/apis/folder/{folder_id}") print("=" * 60)