nc-user-terminator 0.1.4__py3-none-any.whl → 0.1.6__py3-none-any.whl

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.

Potentially problematic release.


This version of nc-user-terminator might be problematic. Click here for more details.

nc_user_manager/client.py CHANGED
@@ -7,15 +7,15 @@ from exceptions import OAuthError
7
7
  from utils import request
8
8
 
9
9
 
10
- TENANT_HEADER = "X-Tenant-Code"
10
+ PRODUCT_HEADER = "X-Product-Code"
11
11
  DEFAULT_TTL = 3600
12
12
 
13
13
 
14
14
  class OAuthClient:
15
15
  def __init__(
16
16
  self,
17
- base_url: str,
18
- tenant_code: str,
17
+ base_url: Optional[str],
18
+ product_code: str,
19
19
  redirect_url: Optional[str] = None,
20
20
  single_session: bool = False,
21
21
  cache: Optional[BaseCache] = None,
@@ -24,12 +24,14 @@ class OAuthClient:
24
24
  OAuth 客户端
25
25
 
26
26
  :param base_url: 服务端基础地址 (例如 http://localhost:8000)
27
- :param tenant_code: 租户编码
27
+ :param product_code: 产品编码
28
28
  :param redirect_url: 可选,重定向地址
29
29
  :param single_session: 是否单会话登录
30
30
  """
31
+ if not base_url:
32
+ base_url = "http://172.27.92.74"
31
33
  self._base_url = base_url.rstrip("/")
32
- self._tenant_code = tenant_code
34
+ self._product_code = product_code
33
35
  self._redirect_url = redirect_url
34
36
  self._single_session = single_session
35
37
  self._cache = cache or MemoryCache(maxsize=10000, ttl=DEFAULT_TTL)
@@ -43,7 +45,7 @@ class OAuthClient:
43
45
  return await asyncio.to_thread(request, *args, **kwargs)
44
46
 
45
47
  def _headers(self, extra: Optional[Dict[str, str]] = None) -> Dict[str, str]:
46
- headers = {TENANT_HEADER: self._tenant_code}
48
+ headers = {PRODUCT_HEADER: self._product_code}
47
49
  if extra:
48
50
  headers.update(extra)
49
51
  return headers
@@ -209,7 +211,8 @@ class OAuthClient:
209
211
  headers = self._headers({"Authorization": f"Bearer {token}"})
210
212
  res_dict = request("GET", f"{self._base_url}/api/me", headers=headers)
211
213
 
212
- self._cache_user(token, res_dict)
214
+ if res_dict.get("success", True):
215
+ self._cache_user(token, res_dict)
213
216
  return UserResponse(res_dict)
214
217
 
215
218
  async def say_my_name_async(self, token: str) -> UserResponse:
@@ -224,8 +227,9 @@ class OAuthClient:
224
227
 
225
228
  headers = self._headers({"Authorization": f"Bearer {token}"})
226
229
  res_dict = await self._arequest("GET", f"{self._base_url}/api/me", headers=headers)
230
+ if res_dict.get("success", True):
231
+ await self._cache_user_async(token, res_dict)
227
232
 
228
- await self._cache_user_async(token, res_dict)
229
233
  return UserResponse(res_dict)
230
234
 
231
235
  # 刷新过期时间
@@ -264,3 +268,24 @@ class OAuthClient:
264
268
  await self._uncache_user_async(token, data)
265
269
  return resp
266
270
 
271
+ async def main():
272
+ def get_redis_url() -> str:
273
+ return f'redis://default:1q2w3e@localhost:6379/1'
274
+
275
+
276
+ import redis.asyncio as redis
277
+ # —— Redis 连接与策略(Bearer + Redis)——
278
+ _redis = redis.from_url(get_redis_url(), decode_responses=True)
279
+
280
+ redis_cache = AsyncRedisCache(_redis)
281
+
282
+ client = OAuthClient("http://auth.nc.com/", "bankgpt", "http://localhost:8000/auth/google/custom_callback", single_session=True, cache=redis_cache)
283
+ authorize = client.authorize("google")
284
+ print(authorize)
285
+
286
+
287
+
288
+
289
+ if __name__ == '__main__':
290
+ asyncio.run(main())
291
+
nc_user_manager/utils.py CHANGED
@@ -2,11 +2,26 @@ import json
2
2
  import urllib.parse
3
3
  import urllib.request
4
4
  import urllib.error
5
+ from typing import Optional, Dict, Any
5
6
 
6
- from exceptions import OAuthError
7
7
 
8
-
9
- def request(method: str, url: str, params=None, headers=None, json_body=None) -> dict:
8
+ def request(
9
+ method: str,
10
+ url: str,
11
+ params: Optional[Dict[str, Any]] = None,
12
+ headers: Optional[Dict[str, str]] = None,
13
+ json_body: Optional[dict] = None
14
+ ) -> dict:
15
+ """
16
+ 发送 HTTP 请求并返回统一结果,不抛异常
17
+ 返回格式:
18
+ {
19
+ "status": int, # HTTP 状态码
20
+ "success": bool, # 是否成功 (2xx)
21
+ "body": dict or str, # JSON解析后的body, 或原始body
22
+ "error": Optional[str] # 错误信息
23
+ }
24
+ """
10
25
  # 拼接 GET 参数
11
26
  if params:
12
27
  query = urllib.parse.urlencode(params)
@@ -20,20 +35,43 @@ def request(method: str, url: str, params=None, headers=None, json_body=None) ->
20
35
  headers["Content-Type"] = "application/json"
21
36
 
22
37
  req = urllib.request.Request(url, method=method.upper(), data=data)
23
-
24
38
  if headers:
25
39
  for k, v in headers.items():
26
40
  req.add_header(k, v)
27
41
 
42
+ result = {
43
+ "status": 0,
44
+ "success": False,
45
+ "message": None,
46
+ "error": None
47
+ }
48
+
28
49
  try:
29
50
  with urllib.request.urlopen(req) as resp:
30
51
  body = resp.read().decode()
52
+ if not body:
53
+ return {}
31
54
  try:
32
55
  return json.loads(body)
33
56
  except json.JSONDecodeError:
34
- raise OAuthError("Invalid JSON response", resp.getcode(), body)
57
+ result["status"] = 200
58
+ result["success"] = False
59
+ result["message"] = body
60
+ result["error"] = "Invalid JSON response"
61
+ return result
35
62
  except urllib.error.HTTPError as e:
36
63
  body = e.read().decode() if e.fp else None
37
- raise OAuthError("HTTP request failed", e.code, body)
64
+ result["status"] = e.code
65
+ try:
66
+ result["message"] = json.loads(body) if body else None
67
+ except json.JSONDecodeError:
68
+ result["message"] = body
69
+ result["error"] = "HTTPError"
70
+ result["success"] = False
38
71
  except urllib.error.URLError as e:
39
- raise OAuthError(f"Network error: {e.reason}")
72
+ result["status"] = 0
73
+ result["message"] = None
74
+ result["error"] = f"NetworkError: {e.reason}"
75
+ result["success"] = False
76
+
77
+ return result
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nc-user-terminator
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: OAuth client wrapper for multi-tenant projects
5
5
  Author: bw_song
6
6
  Author-email: m132777096902@gmail.com
@@ -8,5 +8,10 @@ Requires-Python: >=3.8
8
8
  Description-Content-Type: text/markdown
9
9
  Requires-Dist: cachetools==6.2.0
10
10
  Dynamic: author-email
11
- Dynamic: description-content-type
12
11
  Dynamic: requires-python
12
+
13
+ # 更新
14
+ + V0.1.5
15
+ - 新增本地缓存机制
16
+ + v0.1.6
17
+ - 默认连接地址
@@ -0,0 +1,10 @@
1
+ nc_user_manager/__init__.py,sha256=fF3FZD0XUW5YCfTbBeJs3RK-Mnm3IQ7lns6Eb_tMqPU,238
2
+ nc_user_manager/cache.py,sha256=u9ioXDwHmEJHRB4VKI4JU_pp-8Y5O4bLPm8-pwqiMVc,2273
3
+ nc_user_manager/client.py,sha256=3p47xEp_86Bp8ygeID9Pxr3E0AP6S5zNdAoyEXgkLL0,11035
4
+ nc_user_manager/exceptions.py,sha256=yUMDrh1HHZF36UUadQNHvJlDgEYSYoYOObs8Q11fjrE,351
5
+ nc_user_manager/models.py,sha256=mDK7zskIcaThxvUUTWVf6eMasw7YaA3hDGVVSd1ZJgo,1088
6
+ nc_user_manager/utils.py,sha256=gxFSaUq0oiymIlvHITu2L7EkU2ZYQmW2q_okmUxGBeU,2319
7
+ nc_user_terminator-0.1.6.dist-info/METADATA,sha256=sgSD2PFoJeXjFhy23g2zNxfCn1YpQvOcWgZIDniVIs8,412
8
+ nc_user_terminator-0.1.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
+ nc_user_terminator-0.1.6.dist-info/top_level.txt,sha256=kOAUtl6RYo-x3vMJL8It3KCJLoIFPvMUiAAyXjPQTYA,16
10
+ nc_user_terminator-0.1.6.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- nc_user_manager/__init__.py,sha256=fF3FZD0XUW5YCfTbBeJs3RK-Mnm3IQ7lns6Eb_tMqPU,238
2
- nc_user_manager/cache.py,sha256=u9ioXDwHmEJHRB4VKI4JU_pp-8Y5O4bLPm8-pwqiMVc,2273
3
- nc_user_manager/client.py,sha256=jCtHW65Hk3AV3VQaKj7QapCyLgKqDoHKuX3FKVtwZn4,10241
4
- nc_user_manager/exceptions.py,sha256=yUMDrh1HHZF36UUadQNHvJlDgEYSYoYOObs8Q11fjrE,351
5
- nc_user_manager/models.py,sha256=mDK7zskIcaThxvUUTWVf6eMasw7YaA3hDGVVSd1ZJgo,1088
6
- nc_user_manager/utils.py,sha256=0QmJ9s3mzuYQSlwkcS8BW5XCe23lcoKskEeuqfdZHck,1245
7
- nc_user_terminator-0.1.4.dist-info/METADATA,sha256=-fUx3iRGBFFT3AqVd69tATCupipDXL7dVzHTQ6R_oFw,361
8
- nc_user_terminator-0.1.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
- nc_user_terminator-0.1.4.dist-info/top_level.txt,sha256=kOAUtl6RYo-x3vMJL8It3KCJLoIFPvMUiAAyXjPQTYA,16
10
- nc_user_terminator-0.1.4.dist-info/RECORD,,