Skip to content

Added Get File Info Functionality #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
26 changes: 21 additions & 5 deletions src/lighthouseweb3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""):
Expand Down Expand Up @@ -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.

Expand All @@ -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

Expand All @@ -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):
"""
Expand Down
11 changes: 11 additions & 0 deletions src/lighthouseweb3/functions/get_file_info.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 6 additions & 3 deletions src/lighthouseweb3/functions/get_uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_deal_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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")
18 changes: 18 additions & 0 deletions tests/test_get_file_info.py
Original file line number Diff line number Diff line change
@@ -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")


15 changes: 4 additions & 11 deletions tests/test_get_uploads.py
Original file line number Diff line number Diff line change
@@ -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")