Skip to content

Commit c103e0a

Browse files
committed
support for readable Objects
1 parent 3313ec0 commit c103e0a

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
3+
from io import BufferedReader
4+
from typing import Dict, List, Tuple
5+
from .axios import Axios
6+
from .utils import is_dir, walk_dir_tree, extract_file_name, NamedBufferedReader
7+
from .config import Config
8+
from . import types as t
9+
10+
11+
def upload(source: str | BufferedReader | NamedBufferedReader, token: str) -> t.Upload:
12+
"""
13+
Deploy a file or directory to the lighthouse network
14+
@params {source}: str, path to file or directory
15+
@params {token}: str, lighthouse api token
16+
"""
17+
# create headers
18+
headers = {
19+
"Authorization": f"Bearer {token}",
20+
# "Content-Type": "multipart/form-data",
21+
"Encryption": "false",
22+
"Mime-Type": "application/octet-stream",
23+
}
24+
try:
25+
# create http object
26+
axios = Axios(Config.lighthouse_node + "/api/v0/add")
27+
# create list of files to upload
28+
29+
if (isinstance(source, str)):
30+
file_dict: t.FileDict = {}
31+
32+
# check if source is a directory
33+
if is_dir(source):
34+
# walk directory tree and add files to list
35+
file_dict["files"], root = walk_dir_tree(source)
36+
file_dict["is_dir"] = True
37+
file_dict["path"] = root
38+
else:
39+
# add file to list
40+
file_dict["files"] = [source]
41+
file_dict["is_dir"] = False
42+
file_dict["path"] = source
43+
return {"data": axios.post_files(file_dict, headers)}
44+
else:
45+
return {"data": axios.post_blob(source, headers)}
46+
except Exception as e:
47+
print(e)
48+
raise e

tests/test_upload.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import io
4+
import unittest
5+
from src.lighthouseweb3 import Lighthouse
6+
from src.lighthouseweb3.functions.utils import NamedBufferedReader
7+
from .setup import parse_env
8+
9+
10+
class TestUpload(unittest.TestCase):
11+
def setUp(self) -> None:
12+
"""setup test environment"""
13+
parse_env()
14+
15+
def test_env(self):
16+
"""test env var"""
17+
self.assertNotEqual(
18+
os.environ.get("LIGHTHOUSE_TOKEN"), None, "token is not None"
19+
)
20+
21+
def test_Upload_file(self):
22+
"""test Upload function"""
23+
l = Lighthouse() # will use env var
24+
res = l.upload("tests/testdir/testfile.txt")
25+
self.assertNotEqual(res.get("data"), None, "data is None")
26+
self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
27+
28+
def test_Upload_dir(self):
29+
"""test Upload function"""
30+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
31+
res = l.upload("tests/testdir/")
32+
self.assertNotEqual(res.get("data"), None, "data is None")
33+
self.assertIsInstance(res.get("data"), dict, "data is a dict")
34+
self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
35+
36+
def test_Upload_Blob(self):
37+
"""test Upload function"""
38+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
39+
res = l.upload(NamedBufferedReader(
40+
io.BytesIO(b"tests/testdir/"), "hwh.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+
45+
def test_Upload_File(self):
46+
"""test Upload function"""
47+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
48+
with open("./.gitignore", "rb") as file:
49+
res = l.upload(file)
50+
self.assertNotEqual(res.get("data"), None, "data is None")
51+
self.assertIsInstance(res.get("data"), dict, "data is a dict")
52+
self.assertNotEqual(res.get("data").get(
53+
"Hash"), None, "data is None")
54+
55+
56+
if __name__ == "__main__":
57+
unittest.main()

0 commit comments

Comments
 (0)