Skip to content

Commit 37160ab

Browse files
authored
Merge pull request #4 from lighthouse-web3/feature/file-tags
Feature/file tags
2 parents a7ee467 + b156225 commit 37160ab

File tree

7 files changed

+144
-34
lines changed

7 files changed

+144
-34
lines changed

.github/workflows/workflow.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This workflow will upload a Python Package using Twine when a release is created
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
3+
4+
# This workflow uses actions that are not certified by GitHub.
5+
# They are provided by a third-party and are governed by
6+
# separate terms of service, privacy policy, and support
7+
# documentation.
8+
9+
name: Upload Python Package
10+
11+
on:
12+
release:
13+
types: [published]
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
deploy:
20+
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- uses: actions/checkout@v3
25+
- name: Set up Python
26+
uses: actions/setup-python@v3
27+
with:
28+
python-version: '3.x'
29+
- name: Install dependencies
30+
run: |
31+
python -m pip install --upgrade pip
32+
pip install build
33+
- name: Build package
34+
run: python -m build
35+
- name: Publish package
36+
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
37+
with:
38+
user: nanditmehra
39+
password: ${{ secrets.PYPI_API_TOKEN }}

setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
from setuptools import setup, find_packages
44

55
setup(
6-
name="lighthouseweb3",
7-
version="0.0.7",
6+
name="lighthouse-web3/Lighthouse-Python-SDK",
7+
version="0.1.1",
88
license="GNU GENERAL PUBLIC LICENSE",
99
description="Lighthouse Python SDK",
10-
author="Ayobami Oki| Ravish Sharma | Perfection Loveday",
11-
author_email="ravish@lighthouse.storage",
10+
author="Ravish Sharma | Ayobami Oki | Nandit Mehra",
11+
author_email="nandit123@lighthouse.storage",
1212
url="https://github.com/lighthouse-web3/lighthouse-python-sdk",
1313
packages=find_packages("src"),
1414
package_dir={"": "src"},
@@ -24,6 +24,7 @@
2424
"Programming Language :: Python :: 3.8",
2525
"Programming Language :: Python :: 3.9",
2626
"Programming Language :: Python :: 3.10",
27+
"Programming Language :: Python :: 3.11",
2728
],
2829
keywords="lighthouse storage sdk python filecoin ipfs web3 perpetual",
2930
long_description=(

src/lighthouseweb3/__init__.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ def __init__(self, token: str = ""):
1414
"No token provided: Please provide a token or set the LIGHTHOUSE_TOKEN environment variable"
1515
)
1616

17-
def upload(self, source: str) -> t.Upload:
17+
def upload(self, source: str, tag: str = '') -> t.Upload:
1818
"""
1919
Upload a file or directory to the Lighthouse.
2020
2121
:param source: str, path to file or directory
2222
:return: t.Upload, the upload result
2323
"""
2424
try:
25-
return d.upload(source, self.token)
25+
return d.upload(source, self.token, tag)
2626
except Exception as e:
2727
raise e
2828

29-
def uploadBlob(self, source: io.BufferedReader, filename: str) -> t.Upload:
29+
def uploadBlob(self, source: io.BufferedReader, filename: str, tag: str = '') -> t.Upload:
3030
"""
3131
Upload Blob a file or directory to the Lighthouse.
3232
@@ -36,7 +36,25 @@ def uploadBlob(self, source: io.BufferedReader, filename: str) -> t.Upload:
3636
if not (hasattr(source, 'read') and hasattr(source, 'close')):
3737
raise TypeError("source must have 'read' and 'close' methods")
3838
try:
39-
return d.uploadBlob(source, filename, self.token)
39+
return d.uploadBlob(source, filename, self.token, tag)
40+
except Exception as e:
41+
raise e
42+
43+
@staticmethod
44+
def downloadBlob(dist: io.BufferedWriter, cid: str, chunk_size=1024*1024*10) -> t.Upload:
45+
"""
46+
Download a Blob (file or directory) from the Lighthouse.
47+
48+
:param dist: BufferedWriter, destination to write the downloaded data
49+
:param cid: str, Content Identifier for the data to be downloaded
50+
:param chunk_size: int, size of chunks in which the file will be downloaded (default: 10MB)
51+
:param useCidAsTag: bool, flag to use CID as a tag (default: False)
52+
:return: t.Upload, the download result
53+
"""
54+
if not (hasattr(dist, 'read') and hasattr(dist, 'close')):
55+
raise TypeError("source must have 'read' and 'close' methods")
56+
try:
57+
return _download.download_file_into_writable(cid, dist, chunk_size)
4058
except Exception as e:
4159
raise e
4260

@@ -85,13 +103,26 @@ def getUploads(publicKey: str, pageNo: int = 1) -> List[t.DealData]:
85103
@staticmethod
86104
def download(cid: str) -> bytes:
87105
"""
88-
Get uploads from the Lighthouse.
106+
Download content from the Lighthouse using its Content Identifier (CID).
89107
90-
:param publicKey: str, public key
91-
:param pageNo: int, page number (default: 1)
92-
:return: List[t.DealData], list of deal data
108+
:param cid: str, Content Identifier for the data to be downloaded
109+
:param useCidAsTag: bool, flag to use CID as a tag (default: False)
110+
:return: bytes, the downloaded content
93111
"""
94112
try:
95113
return _download.get_file(cid)
96114
except Exception as e:
97115
raise e
116+
117+
def getTagged(self, tag: str) -> t.Upload:
118+
"""
119+
Retrieve an upload from the Lighthouse using its tag.
120+
121+
:param tag: str, tag associated with the file or directory
122+
:return: t.Upload, the upload result
123+
"""
124+
try:
125+
return _download.getTaggedCid(tag, self.token)
126+
except Exception as e:
127+
raise e
128+

src/lighthouseweb3/functions/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ class Config:
66

77
# lighthouse_api = "http://13.234.35.183:5050" # "https://api.lighthouse.storage"
88
lighthouse_api = 'https://api.lighthouse.storage'
9+
lighthouse_api_test = "http://34.131.52.103"
910
lighthouse_node = "https://node.lighthouse.storage"
1011
lighthouse_bls_node = "https://encryption.lighthouse.storage"
12+
lighthouse_gateway = "https://gateway.lighthouse.storage/ipfs"

src/lighthouseweb3/functions/upload.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from . import types as t
99

1010

11-
def upload(source: str | BufferedReader | NamedBufferedReader, token: str) -> t.Upload:
11+
def upload(source: str | BufferedReader | NamedBufferedReader, token: str, tag: str = "") -> t.Upload:
1212
"""
1313
Deploy a file or directory to the lighthouse network
1414
@params {source}: str, path to file or directory
@@ -40,15 +40,24 @@ def upload(source: str | BufferedReader | NamedBufferedReader, token: str) -> t.
4040
file_dict["files"] = [source]
4141
file_dict["is_dir"] = False
4242
file_dict["path"] = source
43-
return {"data": axios.post_files(file_dict, headers)}
43+
hashData = axios.post_files(file_dict, headers)
4444
else:
45-
return {"data": axios.post_blob(source, source.name, headers)}
45+
hashData = axios.post_blob(source, source.name, headers)
46+
47+
if len(tag):
48+
_axios = Axios(Config.lighthouse_api_test + "/api/user/create_tag")
49+
data = _axios.post({
50+
"tag": tag,
51+
"cid": hashData.get("Hash")
52+
}, {
53+
"Authorization": f"Bearer {token}", })
54+
return {"data": hashData}
4655
except Exception as e:
4756
print(e)
4857
raise e
4958

5059

51-
def uploadBlob(source: BufferedReader, filename: str, token: str) -> t.Upload:
60+
def uploadBlob(source: BufferedReader, filename: str, token: str, tag: str = "") -> t.Upload:
5261
"""
5362
Upload a Buffer or readable Object
5463
@params {source}: str, path to file or directory
@@ -65,7 +74,16 @@ def uploadBlob(source: BufferedReader, filename: str, token: str) -> t.Upload:
6574
# create http object
6675
axios = Axios(Config.lighthouse_node + "/api/v0/add")
6776
# create list of files to upload
68-
return {"data": axios.post_blob(source, filename, headers)}
77+
78+
hashData = axios.post_blob(source, filename, headers)
79+
if len(tag):
80+
_axios = Axios(Config.lighthouse_api_test + "/api/user/create_tag")
81+
data = _axios.post({
82+
"tag": tag,
83+
"cid": hashData.get("Hash")
84+
}, {
85+
"Authorization": f"Bearer {token}", })
86+
return {"data": hashData}
6987
except Exception as e:
7088
print(e)
7189
raise e

tests/test_download.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,15 @@ def test_download_file(self):
3333
self.assertIsInstance(res, bytes, "type doesn't match")
3434
self.assertEqual(res, b'tests/testdir/', "data doesn't match")
3535

36-
# def test_download_blob(self):
37-
# """test Upload function"""
38-
# l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
39-
# res = l.uploadBlob(
40-
# io.BytesIO(b"tests/testdir/"), f"{generate_random_string(16)}.txt")
41-
# self.assertNotEqual(res.get("data"), None, "data is None")
42-
# self.assertIsInstance(res.get("data"), dict, "data is a dict")
43-
# self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
44-
4536
def test_download_blob_file(self):
4637
"""test download_blob function"""
4738
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
4839
with open("./image.png", "wb") as file:
4940
res = l.downloadBlob(
5041
file, "QmPT11PFFQQD3mT6BdwfSHQGHRdF8ngmRmcvxtSBiddWEa", chunk_size=1024*100)
51-
self.assertEqual(file.tell(), 123939, "File Size dont match")
52-
self.assertEqual(res, True, "data doesn't match")
42+
self.assertEqual(res.get("data").get("Size"),
43+
123939, "File Size dont match")
44+
5345

5446

5547
if __name__ == "__main__":

tests/test_upload.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,25 @@ def test_env(self):
2525
os.environ.get("LIGHTHOUSE_TOKEN"), None, "token is not None"
2626
)
2727

28-
def test_Upload_file(self):
28+
def test_upload_file(self):
2929
"""test Upload function"""
3030
l = Lighthouse() # will use env var
3131
res = l.upload("tests/testdir/testfile.txt")
3232
self.assertNotEqual(res.get("data"), None, "data is None")
3333
self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
3434

35-
def test_Upload_dir(self):
35+
def test_upload_dir(self):
3636
"""test Upload function"""
3737
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
38-
res = l.upload("tests/testdir/")
38+
res = l.upload("tests/")
3939
self.assertNotEqual(res.get("data"), None, "data is None")
4040
self.assertIsInstance(res.get("data"), dict, "data is a dict")
41-
self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
41+
self.assertIsInstance(res.get("data").get(
42+
"Hash"), str, "Instance is not of type String")
43+
self.assertIsInstance(res.get("data").get(
44+
"Size"), str, "Instance is not of type String")
4245

43-
def test_Upload_Blob(self):
46+
def test_upload_Blob(self):
4447
"""test Upload function"""
4548
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
4649
res = l.uploadBlob(
@@ -49,7 +52,7 @@ def test_Upload_Blob(self):
4952
self.assertIsInstance(res.get("data"), dict, "data is a dict")
5053
self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
5154

52-
def test_Upload_File(self):
55+
def test_upload_File(self):
5356
"""test Upload function"""
5457
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
5558
with open("./.gitignore", "rb") as file:
@@ -59,6 +62,30 @@ def test_Upload_File(self):
5962
self.assertNotEqual(res.get("data").get(
6063
"Hash"), None, "data is None")
6164

65+
def test_upload_with_tag(self):
66+
"""test Upload with tag function"""
67+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
68+
tag = generate_random_string(8)
69+
res = l.uploadBlob(
70+
io.BytesIO(b"tests/testdir/"+generate_random_string(80).encode("utf-8")), f"{generate_random_string(16)}.txt", tag)
71+
self.assertNotEqual(res.get("data"), None, "data is None")
72+
self.assertIsInstance(res.get("data"), dict, "data is a dict")
73+
self.assertIsInstance(res.get("data").get(
74+
"Hash"), str, "Hash is not of type string")
75+
76+
tagData = l.getTagged(tag)
77+
self.assertEqual(tag, tagData.get("data").get("tag"), "Tag dont match")
78+
self.assertEqual(res.get("data").get("Hash"), tagData.get(
79+
"data").get("cid"), "Tag dont match")
80+
81+
# overWrite tag
82+
res = l.uploadBlob(
83+
io.BytesIO(b"tests/testdir/"+generate_random_string(80).encode("utf-8")), f"{generate_random_string(16)}.txt", tag)
84+
tagData = l.getTagged(tag)
85+
self.assertEqual(tag, tagData.get("data").get("tag"), "Tag dont match")
86+
self.assertEqual(res.get("data").get("Hash"), tagData.get(
87+
"data").get("cid"), "Tag dont match")
88+
6289

6390
if __name__ == "__main__":
6491
unittest.main()

0 commit comments

Comments
 (0)