zt-apollo-cli 0.2.3__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zt-apollo-cli
3
- Version: 0.2.3
3
+ Version: 0.3.0
4
4
  Summary: Apollo configuration management CLI
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -32,19 +32,18 @@ pip install -e .
32
32
 
33
33
  ```bash
34
34
  uv build
35
- uv public --token <your_token>
35
+ uv publish --token <your_token>
36
36
  ```
37
37
 
38
38
  ## 快速开始
39
39
 
40
- ### 1) 登录并缓存 Cookie(已登录可跳过)
40
+ ### 1) 登录并缓存 Cookie
41
41
 
42
- 首次运行会打开浏览器,请完成登录。成功后会缓存 Cookie 到本地。
43
- 再次执行 `login` 时,如果检测到本地已有有效登录态会自动跳过;如需强制刷新可加 `--force`。
42
+ 首次或需要刷新登录态时执行,会打开浏览器完成登录并缓存 Cookie 到本地。
43
+ 其他命令遇到认证失效时会自动重新登录。
44
44
 
45
45
  ```bash
46
46
  zt-apollo-cli login
47
- zt-apollo-cli login --force
48
47
  ```
49
48
 
50
49
  ### 2) 查看部门列表
@@ -19,19 +19,18 @@ pip install -e .
19
19
 
20
20
  ```bash
21
21
  uv build
22
- uv public --token <your_token>
22
+ uv publish --token <your_token>
23
23
  ```
24
24
 
25
25
  ## 快速开始
26
26
 
27
- ### 1) 登录并缓存 Cookie(已登录可跳过)
27
+ ### 1) 登录并缓存 Cookie
28
28
 
29
- 首次运行会打开浏览器,请完成登录。成功后会缓存 Cookie 到本地。
30
- 再次执行 `login` 时,如果检测到本地已有有效登录态会自动跳过;如需强制刷新可加 `--force`。
29
+ 首次或需要刷新登录态时执行,会打开浏览器完成登录并缓存 Cookie 到本地。
30
+ 其他命令遇到认证失效时会自动重新登录。
31
31
 
32
32
  ```bash
33
33
  zt-apollo-cli login
34
- zt-apollo-cli login --force
35
34
  ```
36
35
 
37
36
  ### 2) 查看部门列表
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "zt-apollo-cli"
7
- version = "0.2.3"
7
+ version = "0.3.0"
8
8
  description = "Apollo configuration management CLI"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -1,7 +1,7 @@
1
1
  """Apollo API 请求封装"""
2
2
  import requests
3
3
 
4
- from .auth import get_auth_cookies, get_auth_headers
4
+ from .auth import get_auth_cookies, get_auth_headers, is_auth_error, login
5
5
  from .config import config
6
6
 
7
7
 
@@ -11,8 +11,8 @@ class ApolloApi:
11
11
  def __init__(self):
12
12
  self.base_url = config.base_url
13
13
 
14
- def _request(self, method: str, path: str, **kwargs):
15
- """发送 API 请求"""
14
+ def _request(self, method: str, path: str, _retried: bool = False, **kwargs):
15
+ """发送 API 请求;认证失效时自动重新登录并重试一次"""
16
16
  cookies = get_auth_cookies()
17
17
  headers = get_auth_headers(cookies)
18
18
  extra_headers = kwargs.pop("headers", None)
@@ -27,7 +27,17 @@ class ApolloApi:
27
27
  cookies=cookies,
28
28
  **kwargs
29
29
  )
30
-
30
+
31
+ if is_auth_error(response):
32
+ if _retried:
33
+ raise Exception(
34
+ f"Apollo API 认证失败(已尝试重新登录): "
35
+ f"{response.status_code} {response.text[:500]}"
36
+ )
37
+ print("检测到认证失效,正在自动重新登录...")
38
+ login()
39
+ return self._request(method, path, _retried=True, **kwargs)
40
+
31
41
  if response.status_code >= 400:
32
42
  raise Exception(f"Apollo API 错误: {response.status_code} {response.text}")
33
43
 
@@ -2,6 +2,7 @@
2
2
  import json
3
3
  import os
4
4
 
5
+ import requests
5
6
  from playwright.sync_api import sync_playwright
6
7
 
7
8
  from .config import config
@@ -9,6 +10,16 @@ from .config import config
9
10
 
10
11
  APOLLO_DOMAIN = ".eminxing.com"
11
12
  REQUIRED_COOKIES = ["SESSION"]
13
+ _LOGIN_PATH_KEYWORDS = ("login", "sso", "auth", "signin", "cas", "oauth")
14
+ _LOGIN_PAGE_HINTS = (
15
+ 'id="username"',
16
+ 'name="username"',
17
+ 'type="password"',
18
+ "login_in",
19
+ "btn-login",
20
+ "请登录",
21
+ "未登录",
22
+ )
12
23
 
13
24
  def save_cookies(cookies: dict):
14
25
  """保存 cookie 到缓存文件"""
@@ -23,6 +34,25 @@ def load_cookies() -> dict:
23
34
  return json.load(f)
24
35
  return {}
25
36
 
37
+ def is_auth_error(response: requests.Response) -> bool:
38
+ """判断 HTTP 响应是否为认证失效(需重新登录)"""
39
+ if response.status_code == 401:
40
+ return True
41
+
42
+ if response.status_code in (301, 302, 303, 307, 308):
43
+ location = response.headers.get("Location", "").lower()
44
+ if any(keyword in location for keyword in _LOGIN_PATH_KEYWORDS):
45
+ return True
46
+
47
+ content_type = response.headers.get("Content-Type", "").lower()
48
+ if "text/html" in content_type:
49
+ body = response.text
50
+ if any(hint in body for hint in _LOGIN_PAGE_HINTS):
51
+ return True
52
+
53
+ return False
54
+
55
+
26
56
  def get_auth_headers(cookies: dict) -> dict:
27
57
  """从 cookie 构建认证请求头"""
28
58
  session_cookie = "; ".join(f"{k}={v}" for k, v in cookies.items())
@@ -124,17 +154,12 @@ def get_cookies_from_browser(headless: bool = True) -> dict:
124
154
 
125
155
  return cookie_dict
126
156
 
127
- def login(headless: bool = True, force: bool = False) -> dict:
128
- """登录并缓存 cookie;已有有效登录态时默认跳过"""
129
- cached = load_cookies()
130
- if not force and all(cached.get(k) for k in REQUIRED_COOKIES):
131
- print("检测到已登录,跳过重新登录。如需刷新请使用 --force。")
132
- return cached
133
-
157
+ def login(headless: bool = True) -> dict:
158
+ """登录并缓存 cookie"""
134
159
  if headless:
135
- print("正在启动浏览器(无头模式),请重新登录...")
160
+ print("正在启动浏览器(无头模式),请登录...")
136
161
  else:
137
- print("正在打开浏览器,请重新登录...")
162
+ print("正在打开浏览器,请登录...")
138
163
  cookies = get_cookies_from_browser(headless=headless)
139
164
  save_cookies(cookies)
140
165
  print("登录成功,Apollo cookie 已缓存!")
@@ -110,14 +110,9 @@ def cli(ctx, output_format, base_url):
110
110
  is_flag=True,
111
111
  help="显示浏览器窗口,用于无环境变量时的手动登录",
112
112
  )
113
- @click.option(
114
- "--force",
115
- is_flag=True,
116
- help="强制重新登录并刷新本地 cookie",
117
- )
118
- def cmd_login(headed, force):
119
- """登录 - 已登录则跳过,传 --force 可强制重新登录"""
120
- login(headless=not headed, force=force)
113
+ def cmd_login(headed):
114
+ """登录并缓存 Cookie"""
115
+ login(headless=not headed)
121
116
 
122
117
 
123
118
  @cli.group()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zt-apollo-cli
3
- Version: 0.2.3
3
+ Version: 0.3.0
4
4
  Summary: Apollo configuration management CLI
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -32,19 +32,18 @@ pip install -e .
32
32
 
33
33
  ```bash
34
34
  uv build
35
- uv public --token <your_token>
35
+ uv publish --token <your_token>
36
36
  ```
37
37
 
38
38
  ## 快速开始
39
39
 
40
- ### 1) 登录并缓存 Cookie(已登录可跳过)
40
+ ### 1) 登录并缓存 Cookie
41
41
 
42
- 首次运行会打开浏览器,请完成登录。成功后会缓存 Cookie 到本地。
43
- 再次执行 `login` 时,如果检测到本地已有有效登录态会自动跳过;如需强制刷新可加 `--force`。
42
+ 首次或需要刷新登录态时执行,会打开浏览器完成登录并缓存 Cookie 到本地。
43
+ 其他命令遇到认证失效时会自动重新登录。
44
44
 
45
45
  ```bash
46
46
  zt-apollo-cli login
47
- zt-apollo-cli login --force
48
47
  ```
49
48
 
50
49
  ### 2) 查看部门列表
File without changes