Skip to content

Commit 0a744f6

Browse files
committed
Merge branch 'main' into feature_get_balance and resolved conflicts
2 parents 449220d + 05c3200 commit 0a744f6

File tree

6 files changed

+88
-33
lines changed

6 files changed

+88
-33
lines changed

src/lighthouseweb3/__init__.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
deal_status,
88
get_uploads as getUploads,
99
download as _download,
10-
get_balance as getBalance
10+
get_file_info as getFileInfo
1111
)
1212

1313

@@ -44,18 +44,6 @@ def uploadBlob(self, source: io.BufferedReader, filename: str, tag: str = ''):
4444
return d.uploadBlob(source, filename, self.token, tag)
4545
except Exception as e:
4646
raise e
47-
48-
def getBalance(self):
49-
"""
50-
Retrieve the balance information of a user from the Lighthouse.
51-
52-
:param publicKey: str, The public key of the user.
53-
:return: dict[str, any], A dictionary containing the data usage and data limit details.
54-
"""
55-
try:
56-
return getBalance.get_balance(self.token)
57-
except Exception as e:
58-
raise e
5947

6048
@staticmethod
6149
def downloadBlob(dist: io.BufferedWriter, cid: str, chunk_size=1024*1024*10):
@@ -86,18 +74,16 @@ def getDealStatus(cid: str):
8674
return deal_status.get_deal_status(cid)
8775
except Exception as e:
8876
raise e
89-
90-
@staticmethod
91-
def getUploads(publicKey: str, pageNo: int = 1):
77+
78+
def getUploads(self, lastKey: str = None):
9279
"""
9380
Get uploads from the Lighthouse.
9481
95-
:param publicKey: str, public key
96-
:param pageNo: int, page number (default: 1)
82+
:param lastKey: To navigate to different pages of results
9783
:return: List[t.DealData], list of deal data
9884
"""
9985
try:
100-
return getUploads.get_uploads(publicKey, pageNo)
86+
return getUploads.get_uploads(self.token, lastKey)
10187
except Exception as e:
10288
raise e
10389

@@ -114,6 +100,19 @@ def download(cid: str):
114100
return _download.get_file(cid)
115101
except Exception as e:
116102
raise e
103+
104+
@staticmethod
105+
def getFileInfo(cid: str):
106+
"""
107+
Retrieves information about a file using its CID (Content Identifier).
108+
:param cid: str, Content Identifier for the data to be downloaded
109+
returns: dict, A dictionary containing file information.
110+
"""
111+
112+
try:
113+
return getFileInfo.get_file_info(cid)
114+
except Exception as e:
115+
raise e
117116

118117
def getTagged(self, tag: str):
119118
"""
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def get_file_info(cid: str):
5+
url = f"{Config.lighthouse_api}/api/lighthouse/file_info?cid={cid}"
6+
try:
7+
response = req.get(url)
8+
except Exception as e:
9+
raise Exception("Failed to get file metadata")
10+
11+
return response.json()

src/lighthouseweb3/functions/get_uploads.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ def bytes_to_size(bytes_size):
1111
return f"{round(bytes_size, 2)} {units[index]}"
1212

1313

14-
def get_uploads(publicKey: str, pageNo: int = 1) :
14+
def get_uploads(token: str, lastKey: str = None) :
15+
headers = {
16+
"Authorization": f"Bearer {token}",
17+
}
18+
1519
try:
16-
url = f"{Config.lighthouse_api}/api/user/files_uploaded?publicKey={publicKey}&pageNo={pageNo}"
17-
response = requests.get(url)
20+
url = f"{Config.lighthouse_api}/api/user/files_uploaded?lastKey={lastKey}"
21+
response = requests.get(url, headers=headers)
1822
response.raise_for_status()
1923
return response.json()
2024
except requests.HTTPError as error:

tests/test_deal_status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_deal_status(self):
1515
"QmT9shXpKcn4HRbJhXJ1ZywzwjEo2QWbxAx4SVgW4eYKjG")
1616
self.assertIsInstance(res, list, "data is a list")
1717
self.assertIsInstance(res[0].get(
18-
"dealId"), int, "dealId is Int")
18+
"DealID"), int, "DealID is Int")
1919

2020
def test_deal_status_init(self):
2121
"""test deal_status function"""
@@ -25,4 +25,4 @@ def test_deal_status_init(self):
2525
"QmT9shXpKcn4HRbJhXJ1ZywzwjEo2QWbxAx4SVgW4eYKjG")
2626
self.assertIsInstance(res, list, "data is a list")
2727
self.assertIsInstance(res[0].get(
28-
"dealId"), int, "dealId is Int")
28+
"DealID"), int, "DealID is Int")

tests/test_get_file_info.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import unittest
4+
from src.lighthouseweb3 import Lighthouse
5+
from .setup import parse_env
6+
7+
8+
class TestGetFileInfo(unittest.TestCase):
9+
10+
def test_get_file_info(self):
11+
"""test get_file_info function"""
12+
parse_env()
13+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
14+
res = l.getFileInfo("Qmd5MBBScDUV3Ly8qahXtZFqyRRfYSmUwEcxpYcV4hzKfW")
15+
self.assertIsInstance(res, dict, "data is a dict")
16+
self.assertEqual(res.get("cid"),"Qmd5MBBScDUV3Ly8qahXtZFqyRRfYSmUwEcxpYcV4hzKfW", "cid is matching")
17+
18+
def test_get_file_info_invalid_token(self):
19+
"""test get_upload with invalid token"""
20+
with self.assertRaises(Exception) as context:
21+
l = Lighthouse("invalid_token")
22+
l.getFileInfo("Qmd5MBBScDUV3Ly8qahXtZFqyRRfYSmUwEcxpYcV4hzKfW")
23+
self.assertIn("authentication failed", str(context.exception).lower())
24+
25+
def test_get_file_info_invalid_cid(self):
26+
parse_env()
27+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
28+
res = l.getFileInfo("invalid_cid")
29+
self.assertIsInstance(res, dict, "res is dict")
30+
self.assertIsInstance(res.get('error'), dict, "error is dict")
31+
self.assertEqual(res.get("error").get('message'), 'Not Found', 'cid not found')

tests/test_get_uploads.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,28 @@
77
from .setup import parse_env
88

99

10-
class TestDealStatus(unittest.TestCase):
10+
class TestGetUploads(unittest.TestCase):
1111

1212
def test_get_upload(self):
13-
"""test static test_get_upload function"""
14-
res = Lighthouse.getUploads(
15-
"0xB23809427cFc9B3346AEC5Bb70E7e574696cAF80")
16-
self.assertIsInstance(res.get("fileList"), list, "data is a list")
17-
18-
def test_get_upload_init(self):
1913
"""test get_upload function"""
2014
parse_env()
2115
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
22-
res = Lighthouse.getUploads(
23-
"0xB23809427cFc9B3346AEC5Bb70E7e574696cAF80")
16+
res = l.getUploads()
17+
self.assertIsInstance(res.get("fileList"), list, "data is a list")
18+
self.assertIsInstance(res.get('totalFiles'), int, "totalFiles is an int")
19+
20+
def test_get_upload_with_last_key(self):
21+
"""test get_upload function with lastKey"""
22+
parse_env()
23+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
24+
res = l.getUploads('b5f60ba0-b708-41a3-b0f2-5c808ce63b48')
2425
self.assertIsInstance(res.get("fileList"), list, "data is a list")
26+
self.assertIsInstance(res.get('totalFiles'), int, "totalFiles is an int")
27+
28+
def test_get_upload_with_invalid_token(self):
29+
"""test get_upload function with invalid token"""
30+
parse_env()
31+
l = Lighthouse("invalid_token")
32+
with self.assertRaises(Exception) as context:
33+
l.getUploads()
34+
self.assertIn("authentication failed", str(context.exception).lower())

0 commit comments

Comments
 (0)