Skip to content

Commit 3b46244

Browse files
committed
feature: get balance API added and tested successfully
1 parent 678e49e commit 3b46244

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/lighthouseweb3/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import os
44
import io
5-
from .functions import upload as d,deal_status, get_uploads as getUploads, download as _download
5+
from .functions import (
6+
upload as d,
7+
deal_status,
8+
get_uploads as getUploads,
9+
download as _download,
10+
get_balance as getBalance
11+
)
612

713

814
class Lighthouse:
@@ -38,6 +44,18 @@ def uploadBlob(self, source: io.BufferedReader, filename: str, tag: str = ''):
3844
return d.uploadBlob(source, filename, self.token, tag)
3945
except Exception as e:
4046
raise e
47+
48+
def getBalance(self, publicKey: str):
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, publicKey)
57+
except Exception as e:
58+
raise e
4159

4260
@staticmethod
4361
def downloadBlob(dist: io.BufferedWriter, cid: str, chunk_size=1024*1024*10):
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, publicKey: str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/user/user_data_usage?publicKey={publicKey}"
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()

tests/test_get_balance.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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("0x1FB9Be96d228De94F4C57962675433Ae55a6c4a5")
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")

0 commit comments

Comments
 (0)