Skip to content

Commit 8213e35

Browse files
committed
Fix base URL normalization and README examples
1 parent 46f2245 commit 8213e35

2 files changed

Lines changed: 16 additions & 25 deletions

File tree

README.md

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pip install uapi-sdk-python
1818
```python
1919
from uapi import UapiClient
2020

21-
client = UapiClient("https://uapis.cn/api/v1")
21+
client = UapiClient("https://uapis.cn", "YOUR_API_KEY")
2222
result = client.social.get_social_qq_userinfo(qq="10001")
2323
print(result)
2424
```
@@ -49,7 +49,7 @@ print(result)
4949
```python
5050
from uapi import UapiClient, UapiError
5151

52-
client = UapiClient("https://uapis.cn/api/v1")
52+
client = UapiClient("https://uapis.cn", "YOUR_API_KEY")
5353

5454
# 成功路径
5555
result = client.social.get_social_qq_userinfo(qq="10001")
@@ -88,7 +88,7 @@ except UapiError as err:
8888
from functools import lru_cache
8989
from uapi import UapiClient
9090

91-
client = UapiClient("https://uapis.cn/api/v1", token="<TOKEN>")
91+
client = UapiClient("https://uapis.cn", token="YOUR_API_KEY")
9292

9393
@lru_cache(maxsize=128)
9494
def cached_lookup(qq: str):
@@ -99,34 +99,19 @@ user = cached_lookup("10001")
9999

100100
也可以在 FastAPI / Django 项目里配合 Redis,将 SDK 的响应序列化后写入缓存,命中即直接返回。
101101

102-
### 注入自定义 httpx.Client
102+
### 调整超时与环境
103103

104104
```python
105-
import httpx
106-
from httpx import Auth
107105
from uapi import UapiClient
108106

109-
class StaticToken(Auth):
110-
def __init__(self, token: str):
111-
self.token = token
112-
def auth_flow(self, request):
113-
request.headers["Authorization"] = f"Bearer {self.token}"
114-
yield request
115-
116-
http_client = httpx.Client(
117-
timeout=5,
118-
transport=httpx.HTTPTransport(retries=3),
119-
event_hooks={"request": [lambda request: print("->", request.url)]},
120-
)
121-
122107
client = UapiClient(
123-
"https://uapis.cn/api/v1",
124-
client=http_client,
125-
auth=StaticToken("<TOKEN>"),
108+
"https://uapis.cn",
109+
token="YOUR_API_KEY",
110+
timeout=5.0,
126111
)
127112
```
128113

129-
通过自定义 `client` / `transport` / `auth`,可以无缝植入代理、重试策略或 APM 埋点
114+
如果你需要切换到别的环境,直接改 `base_url` 就可以;如果你只想缩短等待时间,传 `timeout` 就够了
130115

131116
## 错误模型概览
132117

uapi/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class _Config:
1212
token: Optional[str] = None
1313
timeout: float = 15.0
1414

15+
16+
def _normalize_base_url(base_url: str) -> str:
17+
normalized = base_url.rstrip("/")
18+
if normalized.endswith("/api/v1"):
19+
normalized = normalized[: -len("/api/v1")]
20+
return normalized
21+
1522
class _HTTP:
1623
def __init__(self, cfg: _Config):
1724
self._cfg = cfg
@@ -43,7 +50,7 @@ class UapiClient:
4350
"""
4451

4552
def __init__(self, base_url: str, token: str | None = None, timeout: float = 15.0):
46-
self._http = _HTTP(_Config(base_url, token, timeout))
53+
self._http = _HTTP(_Config(_normalize_base_url(base_url), token, timeout))
4754
# 动态挂载每个 Tag 的 API 门面
4855
_clipzy_zai_xian_jian_tie_ban = _ClipzyZaiXianJianTieBanApi(self._http)
4956
self.clipzy_zai_xian_jian_tie_ban = _clipzy_zai_xian_jian_tie_ban
@@ -2608,4 +2615,3 @@ def post_search_aggregate(self, **kwargs):
26082615

26092616
return self._http.request("POST", path, params=params, json=body if body else None)
26102617

2611-

0 commit comments

Comments
 (0)