Skip to content

Commit 9e14bc4

Browse files
committed
update to be in init instead of request
1 parent 1c24ece commit 9e14bc4

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

‎jigsawstack/__init__.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Union
1+
from typing import Union, Dict
22
import os
33
from .audio import Audio, AsyncAudio
44
from .vision import Vision, AsyncVision
@@ -27,13 +27,15 @@ class JigsawStack:
2727
classification: Classification
2828
api_key: str
2929
api_url: str
30-
disable_request_logging: bool
30+
headers: Dict[str, str]
31+
# disable_request_logging: bool
3132

3233
def __init__(
3334
self,
3435
api_key: Union[str, None] = None,
3536
api_url: Union[str, None] = None,
36-
disable_request_logging: Union[bool, None] = None,
37+
# disable_request_logging: Union[bool, None] = None,
38+
headers: Union[Dict[str, str], None] = None,
3739
) -> None:
3840
if api_key is None:
3941
api_key = os.environ.get("JIGSAWSTACK_API_KEY")
@@ -51,6 +53,10 @@ def __init__(
5153
self.api_key = api_key
5254
self.api_url = api_url
5355

56+
self.headers = headers or {}
57+
58+
disable_request_logging = self.headers.get("x-jigsaw-no-request-log")
59+
5460
self.audio = Audio(
5561
api_key=api_key,
5662
api_url=api_url,

‎jigsawstack/async_request.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ def __init__(
3636
self.disable_request_logging = config.get("disable_request_logging")
3737
self.stream = stream
3838

39-
def __convert_params(self, params: Union[Dict[Any, Any], List[Dict[Any, Any]]]) -> Dict[str, str]:
39+
def __convert_params(
40+
self, params: Union[Dict[Any, Any], List[Dict[Any, Any]]]
41+
) -> Dict[str, str]:
4042
"""
4143
Convert parameters to string values for URL encoding.
4244
"""
@@ -45,10 +47,10 @@ def __convert_params(self, params: Union[Dict[Any, Any], List[Dict[Any, Any]]])
4547

4648
if isinstance(params, str):
4749
return params
48-
50+
4951
if isinstance(params, list):
5052
return {} # List params are only used in JSON body
51-
53+
5254
converted = {}
5355
for key, value in params.items():
5456
if isinstance(value, bool):
@@ -67,7 +69,15 @@ async def perform(self) -> Union[T, None]:
6769
# For binary responses
6870
if resp.status == 200:
6971
content_type = resp.headers.get("content-type", "")
70-
if not resp.text or any(t in content_type for t in ["audio/", "image/", "application/octet-stream", "image/png"]):
72+
if not resp.text or any(
73+
t in content_type
74+
for t in [
75+
"audio/",
76+
"image/",
77+
"application/octet-stream",
78+
"image/png",
79+
]
80+
):
7181
content = await resp.read()
7282
return cast(T, content)
7383

‎jigsawstack/request.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class RequestConfig(TypedDict):
1313
api_url: str
1414
api_key: str
15-
headers: Dict[str, str]
15+
disable_request_logging: Union[bool, None] = False
1616

1717

1818
# This class wraps the HTTP request creation logic
@@ -34,9 +34,7 @@ def __init__(
3434
self.api_key = config.get("api_key")
3535
self.data = data
3636
self.headers = headers
37-
self.disable_request_logging = config.get("headers", {}).get(
38-
"x-jigsaw-no-request-log"
39-
)
37+
self.disable_request_logging = config.get("disable_request_logging")
4038
self.stream = stream
4139

4240
def perform(self) -> Union[T, None]:

0 commit comments

Comments
 (0)