Skip to content

Commit 27a8c86

Browse files
committed
feature: Added all the IPNS APIs and tested
1 parent 678e49e commit 27a8c86

File tree

9 files changed

+232
-1
lines changed

9 files changed

+232
-1
lines changed

src/lighthouseweb3/__init__.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
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+
ipns_generate_key as ipnsGenerateKey,
11+
ipns_publish_record as ipnsPublishRecord,
12+
get_ipns_record as getIpnsRecord,
13+
remove_ipns_record as removeIpnsRecord,
14+
)
615

716

817
class Lighthouse:
@@ -38,6 +47,54 @@ def uploadBlob(self, source: io.BufferedReader, filename: str, tag: str = ''):
3847
return d.uploadBlob(source, filename, self.token, tag)
3948
except Exception as e:
4049
raise e
50+
51+
def generateKey(self):
52+
"""
53+
Generate a new IPNS key for the authenticated user.
54+
:return: dict, The generated IPNS key information.
55+
"""
56+
try:
57+
return ipnsGenerateKey.ipns_generate_key(self.token)
58+
except Exception as e:
59+
raise e
60+
61+
def publishRecord(self, cid: str, keyName: str):
62+
"""
63+
Publish an IPNS record for a given CID and key name.
64+
65+
:param cid: str, Content Identifier to publish
66+
:param keyName: str, Name of the IPNS key to use
67+
:return: dict, The published IPNS record information
68+
"""
69+
try:
70+
return ipnsPublishRecord.ipns_publish_record(self.token, cid, keyName)
71+
except Exception as e:
72+
raise e
73+
74+
def getAllKeys(self):
75+
"""
76+
Retrieves all IPNS records associated with the current token.
77+
78+
return: list A list of IPNS records retrieved using the provided token.
79+
"""
80+
81+
try:
82+
return getIpnsRecord.get_ipns_records(self.token)
83+
except Exception as e:
84+
raise e
85+
86+
def removeKey(self, keyName: str):
87+
"""
88+
Remove IPNS record of the given keyName
89+
90+
:param keyName: str, Name of the IPNS key to use
91+
:return: dict, A dict of removed IPNS record.
92+
"""
93+
94+
try:
95+
return removeIpnsRecord.remove_ipns_record(self.token, keyName)
96+
except Exception as e:
97+
raise e
4198

4299
@staticmethod
43100
def downloadBlob(dist: io.BufferedWriter, cid: str, chunk_size=1024*1024*10):
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def get_ipns_records(token: str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/ipns/get_ipns_records"
9+
try:
10+
response = req.get(url, headers=headers)
11+
except Exception as e:
12+
raise Exception("Failed to get ipns records")
13+
14+
if response.status_code != 200:
15+
return response.json()
16+
17+
return {
18+
"data": response.json()
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def ipns_generate_key(token: str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/ipns/generate_key"
9+
try:
10+
response = req.get(url, headers=headers)
11+
except Exception as e:
12+
raise Exception("Failed to ipns generate key")
13+
14+
if response.status_code != 200:
15+
return response.json()
16+
17+
return {
18+
"data": response.json()
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def ipns_publish_record(token: str, cid: str, keyName: str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/ipns/publish_record?cid={cid}&keyName={keyName}"
9+
try:
10+
response = req.get(url, headers=headers)
11+
except Exception as e:
12+
raise Exception("Failed to ipns generate key")
13+
14+
if response.status_code != 200:
15+
return response.json()
16+
17+
return {
18+
"data": response.json()
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .config import Config
2+
import requests as req
3+
4+
def remove_ipns_record(token: str, keyName: str):
5+
headers = {
6+
"Authorization": f"Bearer {token}",
7+
}
8+
url = f"{Config.lighthouse_api}/api/ipns/remove_key?keyName={keyName}"
9+
try:
10+
response = req.delete(url, headers=headers)
11+
except Exception as e:
12+
raise Exception("Failed to remove ipns record")
13+
14+
if response.status_code != 200:
15+
return response.json()
16+
17+
return {
18+
"data": response.json()
19+
}

tests/test_get_ipns_record.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 TestGetIPNSRecord(unittest.TestCase):
9+
10+
def test_get_ipns_records(self):
11+
"""test get_ipns_records function"""
12+
parse_env()
13+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
14+
res = l.getAllKeys()
15+
self.assertIsInstance(res, dict, "result is a dict")
16+
self.assertIsInstance(res.get("data"), list , "data is a dict")
17+
self.assertIsInstance(res.get("data")[0].get("ipnsName"), str , "ipnsName is a str")
18+
self.assertIsInstance(res.get("data")[0].get("ipnsId"), str , "ipnsId is a str")
19+
self.assertIsInstance(res.get("data")[0].get("publicKey"), str , "publicKey is a str")
20+
self.assertIsInstance(res.get("data")[0].get("cid"), str , "cid is a str")
21+
self.assertIsInstance(res.get("data")[0].get("lastUpdate"), int , "lastUpdate is a int")

tests/test_ipns_generate_key.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 TestIPNSGenerateKey(unittest.TestCase):
9+
10+
def test_ipns_generate_key(self):
11+
"""test ipns_generate_key function"""
12+
parse_env()
13+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
14+
res = l.generateKey()
15+
self.assertIsInstance(res, dict, "result is a dict")
16+
self.assertIsInstance(res.get("data"), dict , "data is a dict")
17+
self.assertIsInstance(res.get("data").get("ipnsName"), str , "ipnsName is a str")
18+
self.assertIsInstance(res.get("data").get("ipnsId"), str , "ipnsId is a dict")

tests/test_ipns_publish_record.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 TestIPNSPublishRecord(unittest.TestCase):
9+
10+
def test_ipns_publish_record(self):
11+
"""test ipns_publish_record function"""
12+
parse_env()
13+
14+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
15+
res = l.generateKey()
16+
17+
self.assertIsInstance(res, dict, "result is a dict")
18+
self.assertIsInstance(res.get("data"), dict , "data is a dict")
19+
self.assertIsInstance(res.get("data").get("ipnsName"), str , "ipnsName is a str")
20+
self.assertIsInstance(res.get("data").get("ipnsId"), str , "ipnsId is a dict")
21+
22+
record = l.publishRecord(
23+
'QmeMsykMDyD76zpAbinCy1cjb1KL6CVNBfB44am15U1XHh',
24+
res.get('data').get('ipnsName')
25+
)
26+
27+
self.assertIsInstance(record, dict, "record is a dict")
28+
self.assertIsInstance(record.get("data"), dict, "data is a dict")
29+
self.assertIsInstance(record.get("data").get("Name"), str, "name is a str")
30+
self.assertIsInstance(record.get("data").get("Value"), str, "value is a str")
31+
self.assertEqual(record.get("data").get("Value"), "/ipfs/QmeMsykMDyD76zpAbinCy1cjb1KL6CVNBfB44am15U1XHh")

tests/test_remove_ipns_record.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 TestRemoveIPNSRecord(unittest.TestCase):
9+
10+
def test_ipns_generate_key(self):
11+
"""test ipns_generate_key function"""
12+
parse_env()
13+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
14+
15+
key = l.generateKey()
16+
17+
self.assertIsInstance(key, dict, "result is a dict")
18+
self.assertIsInstance(key.get("data"), dict , "data is a dict")
19+
self.assertIsInstance(key.get("data").get("ipnsName"), str , "ipnsName is a str")
20+
self.assertIsInstance(key.get("data").get("ipnsId"), str , "ipnsId is a dict")
21+
22+
23+
res = l.removeKey(key.get('data').get('ipnsName'))
24+
self.assertIsInstance(res, dict, "result is a dict")
25+
self.assertIsInstance(res.get("data"), dict , "data is a dict")
26+
self.assertIsInstance(res.get("data").get("Keys"), list , "Keys is a list")
27+
self.assertEqual(res.get("data").get("Keys")[0].get('Name'), key.get('data').get('ipnsName'))
28+
self.assertEqual(res.get("data").get("Keys")[0].get('Id'), key.get('data').get('ipnsId'))

0 commit comments

Comments
 (0)