1
- from typing import Any , Dict , List , Union , cast
1
+ from typing import Any , Dict , List , Union , cast , overload
2
2
from typing_extensions import NotRequired , TypedDict
3
3
from .request import Request , RequestConfig
4
4
from .async_request import AsyncRequest
5
5
from typing import List , Union
6
6
from ._config import ClientConfig
7
-
7
+ from . helpers import build_path
8
8
9
9
class TranslateImageParams (TypedDict ):
10
10
target_language : str
11
11
"""
12
12
Target langauge to translate to.
13
13
"""
14
- url : str
14
+ url : NotRequired [ str ]
15
15
"""
16
16
The URL of the image to translate.
17
17
"""
@@ -82,7 +82,7 @@ def __init__(
82
82
disable_request_logging = disable_request_logging ,
83
83
)
84
84
85
- def translate_text (
85
+ def text (
86
86
self , params : TranslateParams
87
87
) -> Union [TranslateResponse , TranslateListResponse ]:
88
88
resp = Request (
@@ -92,25 +92,41 @@ def translate_text(
92
92
verb = "post" ,
93
93
).perform ()
94
94
return resp
95
+
96
+ @overload
97
+ def image (self , params : TranslateImageParams ) -> TranslateImageResponse : ...
98
+ @overload
99
+ def image (self , file : bytes , options : TranslateImageParams = None ) -> TranslateImageParams : ...
100
+
101
+ def image (
102
+ self ,
103
+ blob : Union [TranslateImageParams , bytes ],
104
+ options : TranslateImageParams = None ,
105
+ ) -> TranslateImageResponse :
106
+ if isinstance (blob , dict ): # If params is provided as a dict, we assume it's the first argument
107
+ resp = Request (
108
+ config = self .config ,
109
+ path = "/ai/translate/image" ,
110
+ params = cast (Dict [Any , Any ], blob ),
111
+ verb = "post" ,
112
+ ).perform_with_content ()
113
+ return resp
114
+
115
+ options = options or {}
116
+ path = build_path (base_path = "/ai/translate/image" , params = options )
117
+ content_type = options .get ("content_type" , "application/octet-stream" )
118
+ headers = {"Content-Type" : content_type }
95
119
96
- def translate_image (
97
- self , params : TranslateImageParams
98
- ) -> TranslateImageResponse :
99
120
resp = Request (
100
121
config = self .config ,
101
- path = "/ai/translate/image" ,
102
- params = cast (Dict [Any , Any ], params ),
122
+ path = path ,
123
+ params = options ,
124
+ data = blob ,
125
+ headers = headers ,
103
126
verb = "post" ,
104
- ).perform ()
127
+ ).perform_with_content ()
105
128
return resp
106
129
107
- def translate (
108
- self , params : Union [TranslateParams , TranslateImageParams ]
109
- ) -> Union [TranslateResponse , TranslateListResponse , TranslateImageResponse ]:
110
- if "url" in params or "file_store_key" in params :
111
- return self .translate_image (params )
112
- return self .translate_text (params )
113
-
114
130
115
131
class AsyncTranslate (ClientConfig ):
116
132
config : RequestConfig
@@ -128,7 +144,7 @@ def __init__(
128
144
disable_request_logging = disable_request_logging ,
129
145
)
130
146
131
- async def translate_text (
147
+ async def text (
132
148
self , params : TranslateParams
133
149
) -> Union [TranslateResponse , TranslateListResponse ]:
134
150
resp = await AsyncRequest (
@@ -138,21 +154,37 @@ async def translate_text(
138
154
verb = "post" ,
139
155
).perform ()
140
156
return resp
141
-
142
- async def translate_image (
143
- self , params : TranslateImageParams
157
+
158
+ @overload
159
+ async def image (self , params : TranslateImageParams ) -> TranslateImageResponse : ...
160
+ @overload
161
+ async def image (self , file : bytes , options : TranslateImageParams = None ) -> TranslateImageParams : ...
162
+
163
+ async def image (
164
+ self ,
165
+ blob : Union [TranslateImageParams , bytes ],
166
+ options : TranslateImageParams = None ,
144
167
) -> TranslateImageResponse :
168
+ if isinstance (blob , dict ):
169
+ resp = await AsyncRequest (
170
+ config = self .config ,
171
+ path = "/ai/translate/image" ,
172
+ params = cast (Dict [Any , Any ], blob ),
173
+ verb = "post" ,
174
+ ).perform_with_content ()
175
+ return resp
176
+
177
+ options = options or {}
178
+ path = build_path (base_path = "/ai/translate/image" , params = options )
179
+ content_type = options .get ("content_type" , "application/octet-stream" )
180
+ headers = {"Content-Type" : content_type }
181
+
145
182
resp = await AsyncRequest (
146
183
config = self .config ,
147
- path = "/ai/translate/image" ,
148
- params = cast (Dict [Any , Any ], params ),
184
+ path = path ,
185
+ params = options ,
186
+ data = blob ,
187
+ headers = headers ,
149
188
verb = "post" ,
150
- ).perform ()
151
- return resp
152
-
153
- async def translate (
154
- self , params : Union [TranslateParams , TranslateImageParams ]
155
- ) -> Union [TranslateResponse , TranslateListResponse , TranslateImageResponse ]:
156
- if "url" in params or "file_store_key" in params :
157
- return await self .translate_image (params )
158
- return await self .translate_text (params )
189
+ ).perform_with_content ()
190
+ return resp
0 commit comments