Skip to content

Commit 0affd82

Browse files
committed
feat: add load_api classmethod for loading API keys from file
- Add load_api classmethod to APIPolling class for loading API keys from file - Support filtering out comments (lines starting with #) and empty lines - Add default path to ~/.api for convenience - Include proper type annotations and Path handling
1 parent b517225 commit 0affd82

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/openai_api_polling/polling/api_polling.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
from __future__ import annotations
1111

12+
from os import PathLike
13+
from pathlib import Path
1214
from typing import List
1315

1416

@@ -65,6 +67,18 @@ def mask_api_key(api_key: str,
6567
suffix = api_key[-visible:]
6668
return prefix + (mask_char * mask_len) + suffix
6769

70+
@classmethod
71+
def load_api(cls,
72+
api_file: str | PathLike = Path.home() / ".api") -> APIPolling:
73+
api_file = Path(api_file).expanduser().absolute().as_posix()
74+
api_keys: List[str] = []
75+
with open(api_file, "r", encoding="utf-8") as f:
76+
for line in f:
77+
line = line.strip()
78+
if line and not line.startswith("#"): # 只添加非空行且不以#开头的行
79+
api_keys.append(line)
80+
return cls(api_keys)
81+
6882

6983
if __name__ == "__main__":
7084
api_list = [

0 commit comments

Comments
 (0)