@@ -1307,11 +1307,61 @@ async def configure_preview_channel(
13071307
13081308 setting_status = setting_response .status_code
13091309
1310+ # 调用 Google Cloud API 配置 preview 通道
1311+ # 根据文档,需要两个步骤:
1312+ # 1. 创建 Release Channel Setting (EXPERIMENTAL)
1313+ # 2. 创建 Setting Binding (绑定到目标项目)
1314+ from src .httpx_client import post_async , get_async
1315+ import uuid
1316+
1317+ # 生成唯一的 ID
1318+ setting_id = f"preview-setting-{ uuid .uuid4 ().hex [:8 ]} "
1319+ binding_id = f"preview-binding-{ uuid .uuid4 ().hex [:8 ]} "
1320+
1321+ base_url = f"https://cloudaicompanion.googleapis.com/v1/projects/{ project_id } /locations/global"
1322+ headers = {
1323+ "Authorization" : f"Bearer { access_token } " ,
1324+ "Content-Type" : "application/json"
1325+ }
1326+
1327+ log .info (f"开始配置 preview 通道: { filename } (project_id={ project_id } )" )
1328+
1329+ # 步骤 1: 创建 Release Channel Setting
1330+ setting_url = f"{ base_url } /releaseChannelSettings"
1331+ setting_response = await post_async (
1332+ url = setting_url ,
1333+ json = {"release_channel" : "EXPERIMENTAL" },
1334+ headers = headers ,
1335+ params = {"release_channel_setting_id" : setting_id },
1336+ timeout = 30.0
1337+ )
1338+
1339+ setting_status = setting_response .status_code
1340+
13101341 if setting_status == 200 or setting_status == 201 :
13111342 log .info (f"步骤 1/2: Release Channel Setting 创建成功 (setting_id={ setting_id } )" )
13121343 elif setting_status == 409 :
1313- # Setting 已存在,继续下一步
1314- log .info (f"步骤 1/2: Release Channel Setting 已存在" )
1344+ # Setting 已存在,需要 LIST 获取真实的 setting_id,否则 Step 2 的 URL 会用错误的 ID
1345+ log .info (f"步骤 1/2: Release Channel Setting 已存在,正在获取已有 setting_id..." )
1346+ list_response = await get_async (
1347+ url = setting_url ,
1348+ headers = headers ,
1349+ timeout = 30.0
1350+ )
1351+ if list_response .status_code == 200 :
1352+ try :
1353+ list_data = list_response .json ()
1354+ settings = list_data .get ("releaseChannelSettings" , [])
1355+ if settings :
1356+ existing_name = settings [0 ].get ("name" , "" )
1357+ setting_id = existing_name .split ("/" )[- 1 ]
1358+ log .info (f"步骤 1/2: 获取到已有 setting_id={ setting_id } " )
1359+ else :
1360+ log .warning (f"步骤 1/2: LIST 返回空列表,保持随机 setting_id" )
1361+ except Exception as e :
1362+ log .warning (f"步骤 1/2: 解析 LIST 响应失败: { e } ,保持随机 setting_id" )
1363+ else :
1364+ log .warning (f"步骤 1/2: LIST 请求失败 (status={ list_response .status_code } ),保持随机 setting_id" )
13151365 else :
13161366 # 步骤 1 失败
13171367 error_text = setting_response .text if hasattr (setting_response , 'text' ) else ""
0 commit comments