Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 11 additions & 10 deletions src/lighthouseweb3/functions/axios.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import requests as req
from . import utils
from .exceptions import LighthouseAPIError


class Axios:
Expand All @@ -17,17 +18,17 @@ def parse_url_query(self, query):
if query is not None and isinstance(query, dict):
for key, value in query.items():
self.url += f"&{key}={value}"
except Exception as e:
raise e
except (LighthouseAPIError, req.exceptions.HTTPError):
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
raise

def get(self, headers = None, **kwargs) :
try:
self.parse_url_query(kwargs.get("query", None))
r = req.get(self.url, headers=headers)
r.raise_for_status()
return r.json()
except Exception as e:
raise e
except req.exceptions.HTTPError as e:
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
raise LighthouseAPIError(str(e)) from e

def post(
self, body=None, headers= None, **kwargs
Expand All @@ -37,8 +38,8 @@ def post(
r = req.post(self.url, data=body, headers=headers)
r.raise_for_status()
return r.json()
except Exception as e:
raise e
except req.exceptions.HTTPError as e:
raise LighthouseAPIError(str(e)) from e

def post_files(
self, file, headers = None, **kwargs
Expand All @@ -54,9 +55,9 @@ def post_files(
except Exception:
temp = r.text.split("\n")
return json.loads(temp[len(temp) - 2])
except Exception as e:
except req.exceptions.HTTPError as e:
utils.close_files_after_upload(files)
raise e
raise LighthouseAPIError(str(e)) from e

def post_blob(
self, file: BufferedReader, filename: str, headers = None, **kwargs
Expand All @@ -79,6 +80,6 @@ def post_blob(
except Exception:
temp = r.text.split("\n")
return json.loads(temp[len(temp) - 2])
except Exception as e:
except req.exceptions.HTTPError as e:
file.close()
raise e
raise LighthouseAPIError(str(e)) from e
3 changes: 2 additions & 1 deletion src/lighthouseweb3/functions/deal_status.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
from .config import Config
from .exceptions import LighthouseAPIError


def get_deal_status(cid: str):
Expand All @@ -9,4 +10,4 @@ def get_deal_status(cid: str):
response.raise_for_status()
return response.json()
except requests.HTTPError as error:
raise Exception(error.response.text)
raise LighthouseAPIError(error.response.text)
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
19 changes: 19 additions & 0 deletions src/lighthouseweb3/functions/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Custom exceptions for Lighthouse Web3 SDK."""


class LighthouseError(Exception):
"""Base exception class for all Lighthouse SDK errors."""

pass


class LighthouseAPIError(LighthouseError):
"""Exception raised for HTTP/API errors from Lighthouse services."""

pass


class LighthouseUploadError(LighthouseError):
"""Exception raised for upload-related errors."""

pass
7 changes: 3 additions & 4 deletions src/lighthouseweb3/functions/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .axios import Axios
from .utils import is_dir, walk_dir_tree, extract_file_name, NamedBufferedReader
from .config import Config
from .exceptions import LighthouseUploadError


def upload(source, token: str, tag: str = ""):
Expand Down Expand Up @@ -52,8 +53,7 @@ def upload(source, token: str, tag: str = ""):
"Authorization": f"Bearer {token}", })
return {"data": hashData}
except Exception as e:
print(e)
raise e
raise LighthouseUploadError(str(e)) from e
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.


def uploadBlob(source: BufferedReader, filename: str, token: str, tag: str = ""):
Expand Down Expand Up @@ -84,5 +84,4 @@ def uploadBlob(source: BufferedReader, filename: str, token: str, tag: str = ""
"Authorization": f"Bearer {token}", })
return {"data": hashData}
except Exception as e:
print(e)
raise e
raise LighthouseUploadError(str(e)) from e