diff --git a/src/lighthouseweb3/__init__.py b/src/lighthouseweb3/__init__.py index 8cf1d94..c5d6349 100644 --- a/src/lighthouseweb3/__init__.py +++ b/src/lighthouseweb3/__init__.py @@ -2,8 +2,12 @@ import os import io -from .functions import upload as d,deal_status, get_uploads as getUploads, download as _download - +from .functions import ( + upload as d,deal_status, + get_uploads as getUploads, + download as _download, + get_file_info as getFileInfo +) class Lighthouse: def __init__(self, token: str = ""): @@ -69,8 +73,7 @@ def getDealStatus(cid: str): except Exception as e: raise e - @staticmethod - def getUploads(publicKey: str, pageNo: int = 1): + def getUploads(self, lastKey: int = None): """ Get uploads from the Lighthouse. @@ -79,7 +82,7 @@ def getUploads(publicKey: str, pageNo: int = 1): :return: List[t.DealData], list of deal data """ try: - return getUploads.get_uploads(publicKey, pageNo) + return getUploads.get_uploads(self.token, lastKey) except Exception as e: raise e @@ -96,6 +99,19 @@ def download(cid: str): return _download.get_file(cid) except Exception as e: raise e + + @staticmethod + def getFileInfo(cid: str): + """ + Retrieves information about a file using its CID (Content Identifier). + :param cid: str, Content Identifier for the data to be downloaded + returns: dict, A dictionary containing file information. + """ + + try: + return getFileInfo.get_file_info(cid) + except Exception as e: + raise e def getTagged(self, tag: str): """ diff --git a/src/lighthouseweb3/functions/get_file_info.py b/src/lighthouseweb3/functions/get_file_info.py new file mode 100644 index 0000000..dbc89c7 --- /dev/null +++ b/src/lighthouseweb3/functions/get_file_info.py @@ -0,0 +1,11 @@ +from .config import Config +import requests as req + +def get_file_info(cid: str): + url = f"{Config.lighthouse_api}/api/lighthouse/file_info?cid={cid}" + try: + response = req.get(url) + except Exception as e: + raise Exception("Failed to get file metadata") + + return response.json() \ No newline at end of file diff --git a/src/lighthouseweb3/functions/get_uploads.py b/src/lighthouseweb3/functions/get_uploads.py index d2c8a6b..dcc15da 100644 --- a/src/lighthouseweb3/functions/get_uploads.py +++ b/src/lighthouseweb3/functions/get_uploads.py @@ -11,10 +11,13 @@ def bytes_to_size(bytes_size): return f"{round(bytes_size, 2)} {units[index]}" -def get_uploads(publicKey: str, pageNo: int = 1) : +def get_uploads(token: str, lastKey: int = None) : + headers = { + "Authorization": f"Bearer {token}", + } try: - url = f"{Config.lighthouse_api}/api/user/files_uploaded?publicKey={publicKey}&pageNo={pageNo}" - response = requests.get(url) + url = f"{Config.lighthouse_api}/api/user/files_uploaded?lastKey={lastKey}" + response = requests.get(url, headers=headers) response.raise_for_status() return response.json() except requests.HTTPError as error: diff --git a/tests/test_deal_status.py b/tests/test_deal_status.py index 7d8bedc..bb9b3ae 100644 --- a/tests/test_deal_status.py +++ b/tests/test_deal_status.py @@ -15,7 +15,7 @@ def test_deal_status(self): "QmT9shXpKcn4HRbJhXJ1ZywzwjEo2QWbxAx4SVgW4eYKjG") self.assertIsInstance(res, list, "data is a list") self.assertIsInstance(res[0].get( - "dealId"), int, "dealId is Int") + "DealID"), int, "DealID is Int") def test_deal_status_init(self): """test deal_status function""" @@ -25,4 +25,4 @@ def test_deal_status_init(self): "QmT9shXpKcn4HRbJhXJ1ZywzwjEo2QWbxAx4SVgW4eYKjG") self.assertIsInstance(res, list, "data is a list") self.assertIsInstance(res[0].get( - "dealId"), int, "dealId is Int") + "DealID"), int, "DealID is Int") diff --git a/tests/test_get_file_info.py b/tests/test_get_file_info.py new file mode 100644 index 0000000..def0a52 --- /dev/null +++ b/tests/test_get_file_info.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +import os +import unittest +from src.lighthouseweb3 import Lighthouse +from .setup import parse_env + + +class TestGetFileInfo(unittest.TestCase): + + def test_get_file_info(self): + """test get_file_info function""" + parse_env() + l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN")) + res = l.getFileInfo("Qmd5MBBScDUV3Ly8qahXtZFqyRRfYSmUwEcxpYcV4hzKfW") + self.assertIsInstance(res, dict, "data is a dict") + self.assertEqual(res.get("cid"),"Qmd5MBBScDUV3Ly8qahXtZFqyRRfYSmUwEcxpYcV4hzKfW", "cid is matching") + + diff --git a/tests/test_get_uploads.py b/tests/test_get_uploads.py index 82131a8..486eaa7 100644 --- a/tests/test_get_uploads.py +++ b/tests/test_get_uploads.py @@ -1,24 +1,17 @@ #!/usr/bin/env python3 import os -import io import unittest from src.lighthouseweb3 import Lighthouse -from src.lighthouseweb3.functions.utils import NamedBufferedReader from .setup import parse_env -class TestDealStatus(unittest.TestCase): +class TestGetUploads(unittest.TestCase): def test_get_upload(self): - """test static test_get_upload function""" - res = Lighthouse.getUploads( - "0xB23809427cFc9B3346AEC5Bb70E7e574696cAF80") - self.assertIsInstance(res.get("fileList"), list, "data is a list") - - def test_get_upload_init(self): """test get_upload function""" parse_env() l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN")) - res = Lighthouse.getUploads( - "0xB23809427cFc9B3346AEC5Bb70E7e574696cAF80") + res = l.getUploads() self.assertIsInstance(res.get("fileList"), list, "data is a list") + +