Skip to content

Commit b67c227

Browse files
committed
chore: regen translate stream
1 parent 004fd32 commit b67c227

18 files changed

+974
-9
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ print(result)
2424
```
2525

2626
> [!TIP]
27-
> 请使用与运行脚本一致的解释器安装依赖:执行 `python -m pip install uapi-sdk-python` 后直接 `python myip.py`如果 VS Code / Pyright “Import uapi could not be resolved”,将解释器切换到你的虚拟环境(例如 `.venv/Scripts/python.exe`)即可恢复补全
27+
> 请使用与运行脚本相同的 Python 解释器安装依赖,例如执行 `python -m pip install uapi-sdk-python` 后再运行 `python main.py` VS Code / Pyright 中若提示 “Import uapi could not be resolved”,将解释器切换到当前虚拟环境即可恢复补全
2828
2929
## 特性
3030

@@ -51,13 +51,13 @@ from uapi import UapiClient
5151
client = UapiClient("https://uapis.cn/api/v1", token="<TOKEN>")
5252

5353
@lru_cache(maxsize=128)
54-
def cached_user(qq: str):
54+
def cached_lookup(qq: str):
5555
return client.social.get_social_qq_userinfo(qq=qq)
5656

57-
print(cached_user("10001"))
57+
user = cached_lookup("10001")
5858
```
5959

60-
也可以结合 Redis/Memcached:先读缓存,未命中再调用 SDK,并把结果序列化回写缓存
60+
也可以在 FastAPI / Django 项目里配合 Redis,将 SDK 的响应序列化后写入缓存,命中即直接返回
6161

6262
### 注入自定义 httpx.Client
6363

@@ -66,7 +66,7 @@ import httpx
6666
from httpx import Auth
6767
from uapi import UapiClient
6868

69-
class Bearer(Auth):
69+
class StaticToken(Auth):
7070
def __init__(self, token: str):
7171
self.token = token
7272
def auth_flow(self, request):
@@ -76,17 +76,17 @@ class Bearer(Auth):
7676
http_client = httpx.Client(
7777
timeout=5,
7878
transport=httpx.HTTPTransport(retries=3),
79-
event_hooks={"request": [lambda req: print("->", req.url)]},
79+
event_hooks={"request": [lambda request: print("->", request.url)]},
8080
)
8181

8282
client = UapiClient(
8383
"https://uapis.cn/api/v1",
8484
client=http_client,
85-
auth=Bearer("<TOKEN>"),
85+
auth=StaticToken("<TOKEN>"),
8686
)
8787
```
8888

89-
这样可以把企业代理、日志、APM、重试策略统统放进同一个 `httpx.Client`,SDK 会原样复用
89+
通过自定义 `client` / `transport` / `auth`,可以无缝植入代理、重试策略或 APM 埋点
9090

9191
## 错误模型概览
9292

internal/.openapi-generator/FILES

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ docs/PostTextMd5Request.md
264264
docs/PostTextMd5Verify200Response.md
265265
docs/PostTextMd5Verify400Response.md
266266
docs/PostTextMd5VerifyRequest.md
267+
docs/PostTranslateStream400Response.md
268+
docs/PostTranslateStream500Response.md
269+
docs/PostTranslateStreamRequest.md
267270
docs/PostTranslateText200Response.md
268271
docs/PostTranslateText400Response.md
269272
docs/PostTranslateText500Response.md
@@ -284,6 +287,9 @@ setup.cfg
284287
setup.py
285288
test-requirements.txt
286289
test/__init__.py
290+
test/test_post_translate_stream400_response.py
291+
test/test_post_translate_stream500_response.py
292+
test/test_post_translate_stream_request.py
287293
tox.ini
288294
uapi/__init__.py
289295
uapi/api/__init__.py
@@ -559,6 +565,9 @@ uapi/models/post_text_md5_request.py
559565
uapi/models/post_text_md5_verify200_response.py
560566
uapi/models/post_text_md5_verify400_response.py
561567
uapi/models/post_text_md5_verify_request.py
568+
uapi/models/post_translate_stream400_response.py
569+
uapi/models/post_translate_stream500_response.py
570+
uapi/models/post_translate_stream_request.py
562571
uapi/models/post_translate_text200_response.py
563572
uapi/models/post_translate_text400_response.py
564573
uapi/models/post_translate_text500_response.py

internal/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Class | Method | HTTP request | Description
156156
*TextApi* | [**post_text_md5_verify**](docs/TextApi.md#post_text_md5_verify) | **POST** /text/md5/verify | 校验MD5哈希值
157157
*TranslateApi* | [**get_ai_translate_languages**](docs/TranslateApi.md#get_ai_translate_languages) | **GET** /ai/translate/languages | 获取AI翻译支持的语言和配置
158158
*TranslateApi* | [**post_ai_translate**](docs/TranslateApi.md#post_ai_translate) | **POST** /ai/translate | AI智能翻译
159+
*TranslateApi* | [**post_translate_stream**](docs/TranslateApi.md#post_translate_stream) | **POST** /translate/stream | 流式翻译(中英互译)
159160
*TranslateApi* | [**post_translate_text**](docs/TranslateApi.md#post_translate_text) | **POST** /translate/text | 多语言文本翻译
160161
*WebParseApi* | [**get_web_tomarkdown_async_status**](docs/WebParseApi.md#get_web_tomarkdown_async_status) | **GET** /web/tomarkdown/async/{task_id} | 查询网页转换任务状态和结果
161162
*WebParseApi* | [**get_webparse_extractimages**](docs/WebParseApi.md#get_webparse_extractimages) | **GET** /webparse/extractimages | 提取网页中的所有图片
@@ -417,6 +418,9 @@ Class | Method | HTTP request | Description
417418
- [PostTextMd5Verify200Response](docs/PostTextMd5Verify200Response.md)
418419
- [PostTextMd5Verify400Response](docs/PostTextMd5Verify400Response.md)
419420
- [PostTextMd5VerifyRequest](docs/PostTextMd5VerifyRequest.md)
421+
- [PostTranslateStream400Response](docs/PostTranslateStream400Response.md)
422+
- [PostTranslateStream500Response](docs/PostTranslateStream500Response.md)
423+
- [PostTranslateStreamRequest](docs/PostTranslateStreamRequest.md)
420424
- [PostTranslateText200Response](docs/PostTranslateText200Response.md)
421425
- [PostTranslateText400Response](docs/PostTranslateText400Response.md)
422426
- [PostTranslateText500Response](docs/PostTranslateText500Response.md)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PostTranslateStream400Response
2+
3+
4+
## Properties
5+
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
**error** | **str** | 错误描述 | [optional]
9+
**code** | **str** | 错误码 | [optional]
10+
11+
## Example
12+
13+
```python
14+
from uapi.models.post_translate_stream400_response import PostTranslateStream400Response
15+
16+
# TODO update the JSON string below
17+
json = "{}"
18+
# create an instance of PostTranslateStream400Response from a JSON string
19+
post_translate_stream400_response_instance = PostTranslateStream400Response.from_json(json)
20+
# print the JSON string representation of the object
21+
print(PostTranslateStream400Response.to_json())
22+
23+
# convert the object into a dict
24+
post_translate_stream400_response_dict = post_translate_stream400_response_instance.to_dict()
25+
# create an instance of PostTranslateStream400Response from a dict
26+
post_translate_stream400_response_from_dict = PostTranslateStream400Response.from_dict(post_translate_stream400_response_dict)
27+
```
28+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
29+
30+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PostTranslateStream500Response
2+
3+
4+
## Properties
5+
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
**error** | **str** | 错误描述 | [optional]
9+
**code** | **str** | | [optional]
10+
11+
## Example
12+
13+
```python
14+
from uapi.models.post_translate_stream500_response import PostTranslateStream500Response
15+
16+
# TODO update the JSON string below
17+
json = "{}"
18+
# create an instance of PostTranslateStream500Response from a JSON string
19+
post_translate_stream500_response_instance = PostTranslateStream500Response.from_json(json)
20+
# print the JSON string representation of the object
21+
print(PostTranslateStream500Response.to_json())
22+
23+
# convert the object into a dict
24+
post_translate_stream500_response_dict = post_translate_stream500_response_instance.to_dict()
25+
# create an instance of PostTranslateStream500Response from a dict
26+
post_translate_stream500_response_from_dict = PostTranslateStream500Response.from_dict(post_translate_stream500_response_dict)
27+
```
28+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
29+
30+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# PostTranslateStreamRequest
2+
3+
4+
## Properties
5+
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
**query** | **str** | 待翻译的文本内容 |
9+
**to_lang** | **str** | 目标语言,支持:中文、英文 |
10+
**from_lang** | **str** | 源语言,支持:中文、英文、auto(自动检测)。默认为auto | [optional] [default to 'auto']
11+
**tone** | **str** | 语气参数,可选 | [optional]
12+
13+
## Example
14+
15+
```python
16+
from uapi.models.post_translate_stream_request import PostTranslateStreamRequest
17+
18+
# TODO update the JSON string below
19+
json = "{}"
20+
# create an instance of PostTranslateStreamRequest from a JSON string
21+
post_translate_stream_request_instance = PostTranslateStreamRequest.from_json(json)
22+
# print the JSON string representation of the object
23+
print(PostTranslateStreamRequest.to_json())
24+
25+
# convert the object into a dict
26+
post_translate_stream_request_dict = post_translate_stream_request_instance.to_dict()
27+
# create an instance of PostTranslateStreamRequest from a dict
28+
post_translate_stream_request_from_dict = PostTranslateStreamRequest.from_dict(post_translate_stream_request_dict)
29+
```
30+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
31+
32+

internal/docs/TranslateApi.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Method | HTTP request | Description
66
------------- | ------------- | -------------
77
[**get_ai_translate_languages**](TranslateApi.md#get_ai_translate_languages) | **GET** /ai/translate/languages | 获取AI翻译支持的语言和配置
88
[**post_ai_translate**](TranslateApi.md#post_ai_translate) | **POST** /ai/translate | AI智能翻译
9+
[**post_translate_stream**](TranslateApi.md#post_translate_stream) | **POST** /translate/stream | 流式翻译(中英互译)
910
[**post_translate_text**](TranslateApi.md#post_translate_text) | **POST** /translate/text | 多语言文本翻译
1011

1112

@@ -162,6 +163,91 @@ No authorization required
162163

163164
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
164165

166+
# **post_translate_stream**
167+
> str post_translate_stream(post_translate_stream_request)
168+
169+
流式翻译(中英互译)
170+
171+
想让翻译结果像打字机一样逐字显示出来?这个流式翻译接口能实现这种效果。
172+
173+
## 功能概述
174+
不同于传统翻译API一次性返回完整结果,这个接口会实时地、一个字一个字地把翻译内容推给你(就像ChatGPT回复消息那样),非常适合用在聊天应用、直播字幕等需要即时反馈的场景。
175+
176+
## 它能做什么
177+
- **中英互译**:支持中文和英文之间的双向翻译
178+
- **自动识别**:不确定源语言?设置为 `auto` 让我们自动检测
179+
- **逐字返回**:翻译结果会像打字机一样逐字流式返回,用户体验更流畅
180+
- **音频朗读**:部分翻译结果会附带音频链接,方便朗读
181+
182+
## 支持的语言
183+
目前专注于中英互译,支持以下选项:
184+
- `中文`(简体/繁体)
185+
- `英文`
186+
- `auto`(自动检测)
187+
188+
### Example
189+
190+
191+
```python
192+
import uapi
193+
from uapi.models.post_translate_stream_request import PostTranslateStreamRequest
194+
from uapi.rest import ApiException
195+
from pprint import pprint
196+
197+
# Defining the host is optional and defaults to https://uapis.cn/api/v1
198+
# See configuration.py for a list of all supported configuration parameters.
199+
configuration = uapi.Configuration(
200+
host = "https://uapis.cn/api/v1"
201+
)
202+
203+
204+
# Enter a context with an instance of the API client
205+
with uapi.ApiClient(configuration) as api_client:
206+
# Create an instance of the API class
207+
api_instance = uapi.TranslateApi(api_client)
208+
post_translate_stream_request = uapi.PostTranslateStreamRequest() # PostTranslateStreamRequest | 包含翻译参数的JSON对象
209+
210+
try:
211+
# 流式翻译(中英互译)
212+
api_response = api_instance.post_translate_stream(post_translate_stream_request)
213+
print("The response of TranslateApi->post_translate_stream:\n")
214+
pprint(api_response)
215+
except Exception as e:
216+
print("Exception when calling TranslateApi->post_translate_stream: %s\n" % e)
217+
```
218+
219+
220+
221+
### Parameters
222+
223+
224+
Name | Type | Description | Notes
225+
------------- | ------------- | ------------- | -------------
226+
**post_translate_stream_request** | [**PostTranslateStreamRequest**](PostTranslateStreamRequest.md)| 包含翻译参数的JSON对象 |
227+
228+
### Return type
229+
230+
**str**
231+
232+
### Authorization
233+
234+
No authorization required
235+
236+
### HTTP request headers
237+
238+
- **Content-Type**: application/json
239+
- **Accept**: text/event-stream, application/json
240+
241+
### HTTP response details
242+
243+
| Status code | Description | Response headers |
244+
|-------------|-------------|------------------|
245+
**200** | SSE流式响应。Content-Type为text/event-stream | - |
246+
**400** | 请求参数错误 | - |
247+
**500** | 翻译服务错误 | - |
248+
249+
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
250+
165251
# **post_translate_text**
166252
> PostTranslateText200Response post_translate_text(to_lang, post_translate_text_request)
167253
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# coding: utf-8
2+
3+
"""
4+
UAPI
5+
6+
UAPI 官方接口文档
7+
8+
The version of the OpenAPI document: 1.0.0
9+
Generated by OpenAPI Generator (https://openapi-generator.tech)
10+
11+
Do not edit the class manually.
12+
""" # noqa: E501
13+
14+
15+
import unittest
16+
17+
from uapi.models.post_translate_stream400_response import PostTranslateStream400Response
18+
19+
class TestPostTranslateStream400Response(unittest.TestCase):
20+
"""PostTranslateStream400Response unit test stubs"""
21+
22+
def setUp(self):
23+
pass
24+
25+
def tearDown(self):
26+
pass
27+
28+
def make_instance(self, include_optional) -> PostTranslateStream400Response:
29+
"""Test PostTranslateStream400Response
30+
include_optional is a boolean, when False only required
31+
params are included, when True both required and
32+
optional params are included """
33+
# uncomment below to create an instance of `PostTranslateStream400Response`
34+
"""
35+
model = PostTranslateStream400Response()
36+
if include_optional:
37+
return PostTranslateStream400Response(
38+
error = 'Missing required parameter',
39+
code = 'MISSING_QUERY'
40+
)
41+
else:
42+
return PostTranslateStream400Response(
43+
)
44+
"""
45+
46+
def testPostTranslateStream400Response(self):
47+
"""Test PostTranslateStream400Response"""
48+
# inst_req_only = self.make_instance(include_optional=False)
49+
# inst_req_and_optional = self.make_instance(include_optional=True)
50+
51+
if __name__ == '__main__':
52+
unittest.main()

0 commit comments

Comments
 (0)