Skip to content

Commit fa4f375

Browse files
Update Wrapper:Request to handle binaries and json types gracefully to return types directly to user.
1 parent 3121917 commit fa4f375

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

jigsawstack/request.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ def perform(self) -> Union[T, None]:
5050
"""
5151
resp = self.make_request(url=f"{self.api_url}{self.path}")
5252

53-
# For binary responses or empty responses
53+
#for binary responses
5454
if resp.status_code == 200:
5555
content_type = resp.headers.get("content-type", "")
56-
if not resp.text or any(t in content_type for t in ["audio/", "image/", "application/octet-stream"]):
57-
return cast(T, resp)
56+
if not resp.text or any(t in content_type for t in ["audio/", "image/", "application/octet-stream", "image/png"]):
57+
return cast(T, resp.content)
5858

59-
# Handle errors and JSON responses
59+
#for json resposes.
6060
if resp.status_code != 200:
6161
try:
6262
error = resp.json()
@@ -104,6 +104,12 @@ def perform_file(self) -> Union[T, None]:
104104
message=error.get("message"),
105105
err=error.get("error"),
106106
)
107+
108+
#for binary responses
109+
if resp.status_code == 200:
110+
content_type = resp.headers.get("content-type", "")
111+
if "application/json" not in content_type:
112+
resp = cast(T, resp.content)
107113
return resp
108114

109115
def perform_with_content(self) -> T:

jigsawstack/translate.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
from ._config import ClientConfig
77

88

9+
class TranslateImageParams(TypedDict):
10+
target_language: str
11+
"""
12+
Target langauge to translate to.
13+
"""
14+
url: str
15+
"""
16+
The URL of the image to translate.
17+
"""
18+
file_store_key: NotRequired[str]
19+
"""
20+
The file store key of the image to translate.
21+
"""
22+
923
class TranslateParams(TypedDict):
1024
target_language: str
1125
"""
@@ -20,7 +34,6 @@ class TranslateParams(TypedDict):
2034
The text to translate.
2135
"""
2236

23-
2437
class TranslateResponse(TypedDict):
2538
success: bool
2639
"""
@@ -31,6 +44,15 @@ class TranslateResponse(TypedDict):
3144
The translated text.
3245
"""
3346

47+
class TranslateImageResponse(TypedDict):
48+
success: bool
49+
"""
50+
Indicates whether the translation was successful.
51+
"""
52+
image: bytes
53+
"""
54+
The image data that was translated.
55+
"""
3456

3557
class TranslateListResponse(TypedDict):
3658
success: bool
@@ -61,15 +83,18 @@ def __init__(
6183
)
6284

6385
def translate(
64-
self, params: TranslateParams
65-
) -> Union[TranslateResponse, TranslateListResponse]:
66-
path = "/ai/translate"
86+
self, params: Union[TranslateParams, TranslateImageParams]
87+
) -> Union[TranslateResponse, TranslateListResponse, TranslateImageResponse]:
88+
if "url" in params or "file_store_key" in params:
89+
path = "/ai/translate/image"
90+
else:
91+
path = "/ai/translate"
6792
resp = Request(
6893
config=self.config,
6994
path=path,
7095
params=cast(Dict[Any, Any], params),
7196
verb="post",
72-
).perform_with_content()
97+
).perform()
7398
return resp
7499

75100

@@ -91,13 +116,16 @@ def __init__(
91116
)
92117

93118
async def translate(
94-
self, params: TranslateParams
95-
) -> Union[TranslateResponse, TranslateListResponse]:
96-
path = "/ai/translate"
119+
self, params: Union[TranslateParams, TranslateImageParams]
120+
) -> Union[TranslateResponse, TranslateListResponse, TranslateImageParams]:
121+
if "url" in params or "file_store_key" in params:
122+
path = "/ai/translate/image"
123+
else:
124+
path = "/ai/translate"
97125
resp = await AsyncRequest(
98126
config=self.config,
99127
path=path,
100128
params=cast(Dict[Any, Any], params),
101129
verb="post",
102-
).perform_with_content()
130+
).perform()
103131
return resp

0 commit comments

Comments
 (0)