Skip to content

Commit c9a0c08

Browse files
committed
🔧 (ci): 统一添加默认请求头到所有 HTTP 请求
为所有 GitHub Actions 工作流中的 HTTP 请求添加统一的默认请求头 `x-cf-user: ankio`, 提升请求的一致性和可维护性。通过集中定义 `DEFAULT_HEADERS` 避免重复代码, 并确保每个外部调用都携带必要的身份标识信息。
1 parent d7b2de8 commit c9a0c08

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

.github/workflows/main.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
flavors = ['release'] # 项目只有一个标准构建版本
1919

20+
# 所有 HTTP 请求的统一请求头
21+
DEFAULT_HEADERS = {"x-cf-user": "ankio"}
22+
2023
def get_latest_tag_with_prefix(prefix):
2124
print(f"获取最新的 tag: {prefix}")
2225

@@ -258,6 +261,7 @@ def upload_single_attempt(filename, filename_new, channel, timeout=300):
258261
url2 = "https://cloud.ankio.net/api/fs/put"
259262
filename_new = quote('/自动记账/自动记账/版本更新/' + channel + "/" + filename_new, 'utf-8')
260263
headers = {
264+
**DEFAULT_HEADERS,
261265
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
262266
'Chrome/58.0.3029.110 Safari/537.3',
263267
'Authorization': os.getenv("ALIST_TOKEN"),
@@ -331,6 +335,7 @@ def publish_to_github(repo, tag_name, release_name, release_body, file_path, pre
331335
# 创建 release
332336
create_release_url = f"https://api.github.com/repos/{repo}/releases"
333337
headers = {
338+
**DEFAULT_HEADERS,
334339
"Authorization": f"token {token}",
335340
"Accept": "application/vnd.github.v3+json"
336341
}
@@ -360,6 +365,7 @@ def publish_to_github(repo, tag_name, release_name, release_body, file_path, pre
360365
upload_response = requests.post(
361366
upload_url + f"?name={file_name}",
362367
headers={
368+
**DEFAULT_HEADERS,
363369
"Authorization": f"token {token}",
364370
"Accept": "application/vnd.github.v3+json",
365371
"Content-Type": "application/octet-stream" # 明确指定上传内容类型
@@ -448,7 +454,8 @@ def send_apk_with_changelog(workspace, title):
448454
"chat_id": "@ezbook_archives",
449455
"caption": "" # 空的说明文本
450456
},
451-
files={"document": (new_name, apk_file)}
457+
files={"document": (new_name, apk_file)},
458+
headers=DEFAULT_HEADERS
452459
)
453460
response.raise_for_status()
454461
file_id = response.json()['result']['document']['file_id']
@@ -482,7 +489,8 @@ def send_apk_with_changelog(workspace, title):
482489
json={
483490
"chat_id": channel_id,
484491
"media": media
485-
}
492+
},
493+
headers=DEFAULT_HEADERS
486494
)
487495
response.raise_for_status()
488496
message_sent = True
@@ -502,7 +510,8 @@ def send_apk_with_changelog(workspace, title):
502510
"text": truncate_content(content),
503511
"parse_mode": "MarkdownV2",
504512
"disable_web_page_preview": True
505-
}
513+
},
514+
headers=DEFAULT_HEADERS
506515
)
507516
response.raise_for_status()
508517
print("成功发送纯文本更新通知(降级方案)")
@@ -538,7 +547,7 @@ def send_qq_bot_notification(tag, log_data, repo, commit_count):
538547
resp = requests.post(
539548
bot_url,
540549
data=data,
541-
headers={"Content-Type": "application/x-www-form-urlencoded"},
550+
headers={**DEFAULT_HEADERS, "Content-Type": "application/x-www-form-urlencoded"},
542551
timeout=30
543552
)
544553
if not resp.ok:

0 commit comments

Comments
 (0)