Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jigsawstack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .summary import Summary
from .geo import Geo
from .prompt_engine import PromptEngine
from .exceptions import JigsawStackError
# from .version import get_version

class JigsawStack:
Expand Down Expand Up @@ -66,4 +67,4 @@ def __init__(self, api_key: Union[str, None] = None, api_url: Union[str, None] =
self.prompt_engine = PromptEngine(api_key=api_key, api_url=api_url)

# Create a global instance of the Web class
__all__ = ["JigsawStack", "Search"]
__all__ = ["JigsawStack", "Search", "JigsawStackError"]
20 changes: 6 additions & 14 deletions jigsawstack/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ class JigsawStackError(Exception):
def __init__(
self,
code: Union[str, int],
error_type: str,
message: str,
suggested_action: str,
err: Union[str, Dict[str, Any]] = None,
):
Exception.__init__(self, message)
self.code = code
self.message = message
self.suggested_action = suggested_action
self.error_type = error_type
self.error = err
self.success = False


class MissingApiKeyError(JigsawStackError):
Expand Down Expand Up @@ -172,7 +173,7 @@ def __init__(


def raise_for_code_and_type(
code: Union[str, int], error_type: str, message: str
code: Union[str, int], message: str, err : Union[str, Dict[str, Any]] = None
) -> None:
"""Raise the appropriate error based on the code and type.

Expand Down Expand Up @@ -201,21 +202,12 @@ def raise_for_code_and_type(
# Handle the case where the error might be unknown
if error is None:
raise JigsawStackError(
code=code, message=message, error_type=error_type, suggested_action=""
code=code, message=message, err=err, suggested_action=""
)

# Raise error from errors list
error_from_list = error.get(error_type)

if error_from_list is not None:
raise error_from_list(
code=code,
message=message,
error_type=error_type,
)
# defaults to JigsawStackError if finally can't find error type
raise JigsawStackError(
code=code, message=message, error_type=error_type, suggested_action=""
code=code, message=message, err=err, suggested_action=""
)


Expand Down
27 changes: 16 additions & 11 deletions jigsawstack/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,17 @@ def perform(self) -> Union[T, None]:
if "application/json" not in resp.headers["content-type"]:
raise_for_code_and_type(
code=500,
message="Failed to parse JigsawStack API response. Please try again.",
error_type="InternalServerError",
message="Failed to parse JigsawStack API response. Please try again."
)

# handle error in case there is a statusCode attr present
# and status != 200 and response is a json.
if resp.status_code != 200 and resp.json().get("statusCode"):
if resp.status_code != 200:
error = resp.json()
raise_for_code_and_type(
code=error.get("statusCode"),
code=resp.status_code,
message=error.get("message"),
error_type=error.get("name"),
err=error.get("error"),
)

return cast(T, resp.json())
Expand All @@ -74,18 +73,24 @@ def perform_file(self) -> Union[T, None]:
# delete calls do not return a body
if resp.text == "" and resp.status_code == 200:
return None


# handle error in case there is a statusCode attr present
# and status != 200 and response is a json.
if resp.status_code != 200 and resp.json().get("statusCode"):


if "application/json" not in resp.headers["content-type"] and resp.status_code != 200:
raise_for_code_and_type(
code=500,
message="Failed to parse JigsawStack API response. Please try again.",
error_type="InternalServerError",
)

if resp.status_code != 200:
error = resp.json()
raise_for_code_and_type(
code=error.get("statusCode"),
code=resp.status_code,
message=error.get("message"),
error_type=error.get("name"),
err=error.get("error"),
)

return resp

def perform_with_content(self) -> T:
Expand Down
4 changes: 1 addition & 3 deletions jigsawstack/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ class Store(ClientConfig):
def upload(self, file: bytes, options=FileUploadParams) -> Any:
overwrite = options.get("overwrite")
filename = options.get("filename")
path =f"/store/file?overwrite={overwrite}&filename={filename}"


path =f"/store/file?overwrite={overwrite}&key={filename}"
headers = options.get("headers")
_headers = {"Content-Type":"application/octet-stream"}
if headers:
Expand Down
2 changes: 1 addition & 1 deletion jigsawstack/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def html_to_any(self, params: HTMLToAnyParams) -> Any:
api_key=self.api_key,
api_url=self.api_url,
path=path, params=cast(Dict[Any, Any], params), verb="post"
).perform_with_content()
).perform_with_content_file()
return resp

def dns(self, params: DNSParams) -> DNSResponse:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="jigsawstack",
version="0.1.7",
version="0.1.8",
description="JigsawStack Python SDK",
long_description=open("README.md", encoding="utf8").read(),
long_description_content_type="text/markdown",
Expand Down