Skip to content

Commit 8a0b36b

Browse files
committed
Clean up
1 parent c1144e2 commit 8a0b36b

File tree

6 files changed

+87
-23
lines changed

6 files changed

+87
-23
lines changed

jigsawstack/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .search import Search
77
from .prediction import Prediction
88
from .sql import SQL
9-
from .store import KV, File
9+
from .store import KV, Store
1010
from .translate import Translate
1111
from .web import Web
1212
from .sentiment import Sentiment
@@ -21,7 +21,7 @@ class JigsawStack:
2121
vision : Vision
2222
prediction: Prediction
2323
sql: SQL
24-
file: File
24+
file: Store
2525
kv: KV
2626
translate: Translate
2727
web: Web
@@ -59,7 +59,7 @@ def __init__(self, api_key: Union[str, None] = None, api_url: Union[str, None] =
5959
self.vision = Vision(api_key=api_key, api_url=api_url)
6060
self.prediction = Prediction(api_key=api_key, api_url=api_url)
6161
self.sql = SQL(api_key=api_key, api_url=api_url)
62-
self.file = File(api_key=api_key, api_url=api_url)
62+
self.store = Store(api_key=api_key, api_url=api_url)
6363
self.kv = KV(api_key=api_key, api_url=api_url)
6464
self.translate = Translate(api_key=api_key, api_url=api_url)
6565
self.geo = Geo(api_key=api_key, api_url=api_url)

jigsawstack/request.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ def __init__(
1616
api_key:str,
1717
path: str,
1818
params: Union[Dict[Any, Any], List[Dict[Any, Any]]],
19-
verb: RequestVerb
19+
verb: RequestVerb,
20+
headers: Dict[str, str] = {"Content-Type": "application/json"},
21+
data : Union[bytes, None] = None
2022
):
2123
self.path = path
2224
self.params = params
2325
self.verb = verb
2426
self.api_url = api_url
2527
self.api_key = api_key
28+
self.data = data
29+
self.headers = headers
2630

2731
def perform(self) -> Union[T, None]:
2832
"""Is the main function that makes the HTTP request
@@ -37,13 +41,6 @@ def perform(self) -> Union[T, None]:
3741
"""
3842
resp = self.make_request(url=f"{self.api_url}{self.path}")
3943

40-
41-
url= f"{self.api_url}{self.path}"
42-
43-
print(url)
44-
45-
46-
4744
# delete calls do not return a body
4845
if resp.text == "" and resp.status_code == 200:
4946
return None
@@ -66,7 +63,30 @@ def perform(self) -> Union[T, None]:
6663
message=error.get("message"),
6764
error_type=error.get("name"),
6865
)
66+
6967
return cast(T, resp.json())
68+
69+
70+
def perform_file(self) -> Union[T, None]:
71+
72+
resp = self.make_request(url=f"{self.api_url}{self.path}")
73+
74+
# delete calls do not return a body
75+
if resp.text == "" and resp.status_code == 200:
76+
return None
77+
78+
79+
# handle error in case there is a statusCode attr present
80+
# and status != 200 and response is a json.
81+
if resp.status_code != 200 and resp.json().get("statusCode"):
82+
error = resp.json()
83+
raise_for_code_and_type(
84+
code=error.get("statusCode"),
85+
message=error.get("message"),
86+
error_type=error.get("name"),
87+
)
88+
89+
return resp
7090

7191
def perform_with_content(self) -> T:
7292
"""
@@ -82,6 +102,23 @@ def perform_with_content(self) -> T:
82102
if resp is None:
83103
raise NoContentError()
84104
return resp
105+
106+
107+
def perform_with_content_file(self) -> T:
108+
"""
109+
Perform an HTTP request and return the response content.
110+
111+
Returns:
112+
T: The content of the response
113+
114+
Raises:
115+
NoContentError: If the response content is `None`.
116+
"""
117+
resp = self.perform_file()
118+
if resp is None:
119+
raise NoContentError()
120+
return resp
121+
85122

86123
def __get_headers(self) -> Dict[Any, Any]:
87124
"""get_headers returns the HTTP headers that will be
@@ -90,11 +127,17 @@ def __get_headers(self) -> Dict[Any, Any]:
90127
Returns:
91128
Dict: configured HTTP Headers
92129
"""
93-
return {
130+
131+
h = {
94132
"Content-Type": "application/json",
95133
"Accept": "application/json",
96-
"x-api-key": f"{self.api_key}"
134+
"x-api-key": f"{self.api_key}",
97135
}
136+
_headers = h.copy()
137+
_headers.update(self.headers)
138+
139+
return _headers
140+
98141

99142
def make_request(self, url: str) -> requests.Response:
100143
"""make_request is a helper function that makes the actual
@@ -112,7 +155,9 @@ def make_request(self, url: str) -> requests.Response:
112155
headers = self.__get_headers()
113156
params = self.params
114157
verb = self.verb
158+
data = self.data
159+
115160
try:
116-
return requests.request(verb, url, json=params,headers=headers,)
161+
return requests.request(verb, url, json=params,headers=headers, data=data)
117162
except requests.HTTPError as e:
118163
raise e

jigsawstack/store.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,31 @@ class KVAddResponse(TypedDict):
2626

2727

2828

29-
class File(ClientConfig):
30-
def upload(self, file: bytes) -> Any:
31-
path ="/store/file"
29+
class FileUploadParams(TypedDict):
30+
overwrite:bool
31+
filename:str
32+
headers: Dict[str, str]
33+
34+
35+
36+
class Store(ClientConfig):
37+
def upload(self, file: bytes, options=FileUploadParams) -> Any:
38+
overwrite = options.get("overwrite")
39+
filename = options.get("filename")
40+
path =f"/store/file?overwrite={overwrite}&filename={filename}"
41+
42+
43+
headers = options.get("headers")
44+
_headers = {"Content-Type":"application/octet-stream"}
45+
if headers:
46+
_headers.update(headers)
47+
3248
resp = Request(
3349
api_key=self.api_key,
3450
api_url=self.api_url,
35-
path=path, params=cast(Dict[Any, Any], params={}), verb="post"
51+
params=None,
52+
path=path, data=file, headers=_headers, verb="post"
53+
3654
).perform_with_content()
3755
return resp
3856

@@ -41,8 +59,8 @@ def get(self, key: str) -> Any:
4159
resp = Request(
4260
api_key=self.api_key,
4361
api_url=self.api_url,
44-
path=path, params=cast(Dict[Any, Any], params={}), verb="get"
45-
).perform_with_content()
62+
path=path, params=None, verb="get"
63+
).perform_with_content_file()
4664
return resp
4765

4866
def delete(self, key: str) -> FileDeleteResponse:
@@ -71,7 +89,7 @@ def get(self, key: str) -> KVGetResponse:
7189
resp = Request(
7290
api_key=self.api_key,
7391
api_url=self.api_url,
74-
path=path, params=cast(Dict[Any, Any], params={}), verb="get"
92+
path=path, verb="get"
7593
).perform_with_content()
7694
return resp
7795

tests/test_prompt_engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
jigsaw = jigsawstack.JigsawStack()
99

10-
10+
@pytest.mark.skip(reason="Skipping TestWebAPI class for now")
1111
class TestPromptEngine(unittest.TestCase):
1212

1313
def test_get_prompt_engine_response_success(self) -> None:

tests/test_sentiment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
client = jigsawstack.JigsawStack()
1010

1111

12+
@pytest.mark.skip(reason="Skipping TestWebAPI class for now")
1213
class TestSentimentAPI(unittest.TestCase):
1314
def test_sentiment_response_success(self) -> None:
1415
params = {

tests/test_web.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_ai_scrape_success_response(self) -> None:
1515
"element_prompts": ["Plan title", "Plan price"],
1616
}
1717
try:
18-
result =client.web.ai_scrape(params)
18+
result =client.file.upload(params)
1919
assert result["success"] == True
2020
except JigsawStackError as e:
2121
assert e.message == "Failed to parse API response. Please try again."

0 commit comments

Comments
 (0)