Skip to content

Commit 762ce6a

Browse files
fix: drop unnecessary data param, as every request is not multipartform.
1 parent b5ec3b3 commit 762ce6a

File tree

9 files changed

+49
-92
lines changed

9 files changed

+49
-92
lines changed

jigsawstack/async_request.py

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ async def make_request(
243243
headers = self.__get_headers()
244244
params = self.params
245245
verb = self.verb
246-
data = self.data
247246
files = self.files
248247

249248
_params = None
@@ -252,64 +251,27 @@ async def make_request(
252251
_form_data = None
253252

254253
if verb.lower() in ["get", "delete"]:
255-
# convert params for URL encoding if needed
256254
_params = self.__convert_params(params)
257255
elif files:
258-
# for multipart requests - matches request.py behavior
259256
_form_data = aiohttp.FormData()
260-
261-
# add file(s) to form data
262-
for field_name, file_data in files.items():
263-
if isinstance(file_data, bytes):
264-
# just pass the blob without filename
265-
_form_data.add_field(
266-
field_name, BytesIO(file_data), content_type="application/octet-stream"
267-
)
268-
elif isinstance(file_data, tuple):
269-
# if tuple format (filename, data, content_type)
270-
filename, content, content_type = file_data
271-
_form_data.add_field(
272-
field_name, content, filename=filename, content_type=content_type
273-
)
274-
275-
# add params as 'body' field in multipart form (JSON stringified)
257+
_form_data.add_field("file", BytesIO(files["file"]), filename="upload")
276258
if params and isinstance(params, dict):
277-
_form_data.add_field("body", json.dumps(params), content_type="application/json")
278-
elif data:
279-
# for binary data without multipart
280-
_data = data
281-
# pass params as query parameters for binary uploads
282-
if params and isinstance(params, dict):
283-
_params = self.__convert_params(params)
284-
else:
285-
# for JSON requests
259+
_form_data.add_field(
260+
"body", json.dumps(params), content_type="application/json"
261+
)
262+
263+
headers.pop("Content-Type", None)
264+
else: # pure JSON request
286265
_json = params
287266

288-
# m,ake the request based on the data type
289-
if _form_data:
290-
return await session.request(
291-
verb,
292-
url,
293-
params=_params,
294-
data=_form_data,
295-
headers=headers,
296-
)
297-
elif _json is not None:
298-
return await session.request(
299-
verb,
300-
url,
301-
params=_params,
302-
json=_json,
303-
headers=headers,
304-
)
305-
else:
306-
return await session.request(
307-
verb,
308-
url,
309-
params=_params,
310-
data=_data,
311-
headers=headers,
312-
)
267+
return await session.request(
268+
verb,
269+
url,
270+
params=_params,
271+
json=_json,
272+
data=_form_data or _data,
273+
headers=headers,
274+
)
313275

314276
def __get_session(self) -> aiohttp.ClientSession:
315277
"""

jigsawstack/audio.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ async def speech_to_text(
133133
) -> Union[SpeechToTextResponse, SpeechToTextWebhookResponse]:
134134
options = options or {}
135135
path = "/ai/transcribe"
136-
params = options or {}
137136
if isinstance(blob, dict):
138137
resp = await AsyncRequest(
139138
config=self.config,
@@ -147,7 +146,7 @@ async def speech_to_text(
147146
resp = await AsyncRequest(
148147
config=self.config,
149148
path=path,
150-
params=params,
149+
params=options,
151150
verb="post",
152151
files=files,
153152
).perform_with_content()

jigsawstack/embedding.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ def __init__(
4646
@overload
4747
def execute(self, params: EmbeddingParams) -> EmbeddingResponse: ...
4848
@overload
49-
def execute(self, blob: bytes, options: EmbeddingParams = None) -> EmbeddingResponse: ...
49+
def execute(
50+
self, blob: bytes, options: EmbeddingParams = None
51+
) -> EmbeddingResponse: ...
5052

5153
def execute(
5254
self,
@@ -69,7 +71,6 @@ def execute(
6971
config=self.config,
7072
path=path,
7173
params=options,
72-
data=blob,
7374
files=files,
7475
verb="post",
7576
).perform_with_content()
@@ -95,7 +96,9 @@ def __init__(
9596
@overload
9697
async def execute(self, params: EmbeddingParams) -> EmbeddingResponse: ...
9798
@overload
98-
async def execute(self, blob: bytes, options: EmbeddingParams = None) -> EmbeddingResponse: ...
99+
async def execute(
100+
self, blob: bytes, options: EmbeddingParams = None
101+
) -> EmbeddingResponse: ...
99102

100103
async def execute(
101104
self,
@@ -118,7 +121,6 @@ async def execute(
118121
config=self.config,
119122
path=path,
120123
params=options,
121-
data=blob,
122124
files=files,
123125
verb="post",
124126
).perform_with_content()

jigsawstack/embedding_v2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def __init__(
4444
@overload
4545
def execute(self, params: EmbeddingV2Params) -> EmbeddingV2Response: ...
4646
@overload
47-
def execute(self, blob: bytes, options: EmbeddingV2Params = None) -> EmbeddingV2Response: ...
47+
def execute(
48+
self, blob: bytes, options: EmbeddingV2Params = None
49+
) -> EmbeddingV2Response: ...
4850

4951
def execute(
5052
self,
@@ -67,7 +69,6 @@ def execute(
6769
config=self.config,
6870
path=path,
6971
params=options,
70-
data=blob,
7172
files=files,
7273
verb="post",
7374
).perform_with_content()
@@ -118,7 +119,6 @@ async def execute(
118119
config=self.config,
119120
path=path,
120121
params=options,
121-
data=blob,
122122
files=files,
123123
verb="post",
124124
).perform_with_content()

jigsawstack/request.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(
2828
headers: Dict[str, str] = None,
2929
data: Union[bytes, None] = None,
3030
stream: Union[bool, None] = False,
31-
files: Union[Dict[str, Any], None] = None, # Change from 'file' to 'files'
31+
files: Union[Dict[str, Any], None] = None,
3232
):
3333
self.path = path
3434
self.params = params
@@ -39,7 +39,7 @@ def __init__(
3939
self.headers = headers or {"Content-Type": "application/json"}
4040
self.disable_request_logging = config.get("disable_request_logging")
4141
self.stream = stream
42-
self.files = files # Change from 'file' to 'files'
42+
self.files = files
4343

4444
def perform(self) -> Union[T, None]:
4545
"""Is the main function that makes the HTTP request
@@ -93,7 +93,10 @@ def perform_file(self) -> Union[T, None]:
9393
# handle error in case there is a statusCode attr present
9494
# and status != 200 and response is a json.
9595

96-
if "application/json" not in resp.headers["content-type"] and resp.status_code != 200:
96+
if (
97+
"application/json" not in resp.headers["content-type"]
98+
and resp.status_code != 200
99+
):
97100
raise_for_code_and_type(
98101
code=500,
99102
message="Failed to parse JigsawStack API response. Please try again.",
@@ -253,7 +256,7 @@ def make_request(self, url: str) -> requests.Response:
253256
params = self.params
254257
verb = self.verb
255258
data = self.data
256-
files = self.files # Change from 'file' to 'files'
259+
files = self.files
257260

258261
_requestParams = None
259262
_json = None
@@ -262,23 +265,14 @@ def make_request(self, url: str) -> requests.Response:
262265

263266
if verb.lower() in ["get", "delete"]:
264267
_requestParams = params
265-
elif files:
266-
# For multipart requests
268+
elif files: # multipart request
267269
_files = files
268-
# Add params as 'body' field in multipart form (JSON stringified)
269270
if params and isinstance(params, dict):
270-
# Convert params to JSON string and add as 'body' field
271271
_data = {"body": json.dumps(params)}
272-
elif data:
273-
# For binary data without multipart
274-
_data = data
275-
# Pass params as query parameters for binary uploads
276-
if params and isinstance(params, dict):
277-
_requestParams = params
278-
else:
279-
# For JSON requests
280-
_json = params
272+
headers.pop("Content-Type", None) # let requests set it for multipart
281273

274+
else: # pure JSON request
275+
_json = params
282276
try:
283277
return requests.request(
284278
verb,

jigsawstack/translate.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ def text(self, params: TranslateParams) -> TranslateResponse:
8383
return resp
8484

8585
@overload
86-
def image(self, params: TranslateImageParams) -> Union[TranslateImageResponse, bytes]: ...
86+
def image(
87+
self, params: TranslateImageParams
88+
) -> Union[TranslateImageResponse, bytes]: ...
8789
@overload
8890
def image(
8991
self, blob: bytes, options: TranslateImageParams = None
@@ -112,7 +114,6 @@ def image(
112114
config=self.config,
113115
path=path,
114116
params=options,
115-
data=blob,
116117
files=files,
117118
verb="post",
118119
).perform_with_content()
@@ -145,7 +146,9 @@ async def text(self, params: TranslateParams) -> TranslateResponse:
145146
return resp
146147

147148
@overload
148-
async def image(self, params: TranslateImageParams) -> Union[TranslateImageResponse, bytes]: ...
149+
async def image(
150+
self, params: TranslateImageParams
151+
) -> Union[TranslateImageResponse, bytes]: ...
149152
@overload
150153
async def image(
151154
self, blob: bytes, options: TranslateImageParams = None
@@ -172,7 +175,6 @@ async def image(
172175
config=self.config,
173176
path=path,
174177
params=options,
175-
data=blob,
176178
files=files,
177179
verb="post",
178180
).perform_with_content()

jigsawstack/validate.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ def nsfw(
117117
config=self.config,
118118
path=path,
119119
params=options,
120-
data=blob,
121120
files=files,
122121
verb="post",
123122
).perform_with_content()
@@ -204,7 +203,6 @@ async def nsfw(
204203
config=self.config,
205204
path=path,
206205
params=options,
207-
data=blob,
208206
files=files,
209207
verb="post",
210208
).perform_with_content()

jigsawstack/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.3.3"
1+
__version__ = "0.3.4"
22

33

44
def get_version() -> str:

jigsawstack/vision.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,15 @@ def vocr(
209209
config=self.config,
210210
path=path,
211211
params=options,
212-
data=blob,
213212
files=files,
214213
verb="post",
215214
).perform_with_content()
216215
return resp
217216

218217
@overload
219-
def object_detection(self, params: ObjectDetectionParams) -> ObjectDetectionResponse: ...
218+
def object_detection(
219+
self, params: ObjectDetectionParams
220+
) -> ObjectDetectionResponse: ...
220221
@overload
221222
def object_detection(
222223
self, blob: bytes, options: ObjectDetectionParams = None
@@ -242,7 +243,6 @@ def object_detection(
242243
config=self.config,
243244
path=path,
244245
params=options,
245-
data=blob,
246246
files=files,
247247
verb="post",
248248
).perform_with_content()
@@ -291,14 +291,15 @@ async def vocr(
291291
config=self.config,
292292
path=path,
293293
params=options,
294-
data=blob,
295294
files=files,
296295
verb="post",
297296
).perform_with_content()
298297
return resp
299298

300299
@overload
301-
async def object_detection(self, params: ObjectDetectionParams) -> ObjectDetectionResponse: ...
300+
async def object_detection(
301+
self, params: ObjectDetectionParams
302+
) -> ObjectDetectionResponse: ...
302303
@overload
303304
async def object_detection(
304305
self, blob: bytes, options: ObjectDetectionParams = None
@@ -327,7 +328,6 @@ async def object_detection(
327328
config=self.config,
328329
path=path,
329330
params=options,
330-
data=blob,
331331
files=files,
332332
verb="post",
333333
).perform_with_content()

0 commit comments

Comments
 (0)