Skip to content

Commit 840e4e7

Browse files
committed
Merge branch 'main' into feature_get_api_key
2 parents 4b48e36 + 4c6ada8 commit 840e4e7

File tree

8 files changed

+132
-29
lines changed

8 files changed

+132
-29
lines changed

src/lighthouseweb3/__init__.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
deal_status,
88
get_uploads as getUploads,
99
download as _download,
10-
get_api_key as getApiKey
10+
get_file_info as getFileInfo,
11+
get_balance as getBalance
1112
)
1213

1314

@@ -44,6 +45,17 @@ def uploadBlob(self, source: io.BufferedReader, filename: str, tag: str = ''):
4445
return d.uploadBlob(source, filename, self.token, tag)
4546
except Exception as e:
4647
raise e
48+
49+
def getBalance(self):
50+
"""
51+
Retrieve the balance information of a user from the Lighthouse.
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
4759

4860
@staticmethod
4961
def downloadBlob(dist: io.BufferedWriter, cid: str, chunk_size=1024*1024*10):
@@ -74,18 +86,16 @@ def getDealStatus(cid: str):
7486
return deal_status.get_deal_status(cid)
7587
except Exception as e:
7688
raise e
77-
78-
@staticmethod
79-
def getUploads(publicKey: str, pageNo: int = 1):
89+
90+
def getUploads(self, lastKey: str = None):
8091
"""
8192
Get uploads from the Lighthouse.
8293
83-
:param publicKey: str, public key
84-
:param pageNo: int, page number (default: 1)
94+
:param lastKey: To navigate to different pages of results
8595
:return: List[t.DealData], list of deal data
8696
"""
8797
try:
88-
return getUploads.get_uploads(publicKey, pageNo)
98+
return getUploads.get_uploads(self.token, lastKey)
8999
except Exception as e:
90100
raise e
91101

@@ -104,18 +114,15 @@ def download(cid: str):
104114
raise e
105115

106116
@staticmethod
107-
def getApiKey(publicKey: str, signedMessage: str):
117+
def getFileInfo(cid: str):
108118
"""
109-
Generates and returns an API key for the given public key and signed message.
110-
111-
:param publicKey: str, The public key associated with the user.
112-
:param signedMessage: str, The message signed by the user's private key.
113-
:return: dict, A dict with generated API key.
119+
Retrieves information about a file using its CID (Content Identifier).
120+
:param cid: str, Content Identifier for the data to be downloaded
121+
returns: dict, A dictionary containing file information.
114122
"""
115123

116-
117124
try:
118-
return getApiKey.get_api_key(publicKey, signedMessage)
125+
return getFileInfo.get_file_info(cid)
119126
except Exception as e:
120127
raise e
121128

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def get_balance(token:str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/user/user_data_usage"
9+
try:
10+
response = req.get(url, headers=headers)
11+
except Exception as e:
12+
raise Exception("Failed to get account balance")
13+
14+
return response.json()
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_balance.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 TestGetBalance(unittest.TestCase):
9+
10+
def test_get_balance(self):
11+
"""test get_balance function"""
12+
parse_env()
13+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
14+
res = l.getBalance()
15+
self.assertIsInstance(res, dict, "data is a dict")
16+
self.assertIsInstance(res.get("dataLimit"), int, "data limit is a integer")
17+
self.assertIsInstance(res.get("dataUsed"), int, "data used is a integer")
18+
19+
def test_get_balance_invalid_token(self):
20+
"""test get_balance function with invalid token"""
21+
parse_env()
22+
l = Lighthouse('invalid_token')
23+
res = l.getBalance()
24+
self.assertIsInstance(res, dict, "res is a dict")
25+
self.assertIn("authentication failed", str(res).lower())
26+

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)