@@ -18,7 +18,7 @@ pip install uapi-sdk-python
1818``` python
1919from uapi import UapiClient
2020
21- client = UapiClient(" https://uapis.cn/api/v1 " )
21+ client = UapiClient(" https://uapis.cn" , " YOUR_API_KEY " )
2222result = client.social.get_social_qq_userinfo(qq = " 10001" )
2323print (result)
2424```
@@ -49,7 +49,7 @@ print(result)
4949``` python
5050from uapi import UapiClient, UapiError
5151
52- client = UapiClient(" https://uapis.cn/api/v1 " )
52+ client = UapiClient(" https://uapis.cn" , " YOUR_API_KEY " )
5353
5454# 成功路径
5555result = client.social.get_social_qq_userinfo(qq = " 10001" )
@@ -88,7 +88,7 @@ except UapiError as err:
8888from functools import lru_cache
8989from 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 )
9494def 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
107105from 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-
122107client = 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
0 commit comments