Skip to content

Commit 04ef392

Browse files
committed
feat: refactored into python modules
1 parent cf350cd commit 04ef392

File tree

9 files changed

+75
-24
lines changed

9 files changed

+75
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ coverage.xml
5050
.hypothesis/
5151
.pytest_cache/
5252
cover/
53+
*_test.txt
5354

5455
# Translations
5556
*.mo

axios.py renamed to lighthouse/axios.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ def parse_url_params(self, params: Dict[str, str]):
1313
for key, value in params.items():
1414
self.url += f"&{key}={value}"
1515
except Exception as e:
16-
print(e)
17-
return e
16+
raise e
1817

1918
def get(self, headers: Dict[str, str] = None, **kwargs) -> dict | Exception:
2019
try:
@@ -23,8 +22,7 @@ def get(self, headers: Dict[str, str] = None, **kwargs) -> dict | Exception:
2322
r.raise_for_status()
2423
return r.json()
2524
except Exception as e:
26-
print(e)
27-
return e
25+
raise e
2826

2927
def post(
3028
self, body=None, headers: Dict[str, str] = None, **kwargs
@@ -35,17 +33,15 @@ def post(
3533
r.raise_for_status()
3634
return r.json()
3735
except Exception as e:
38-
print(e)
39-
return e
36+
raise e
4037

4138
def read_files(self, files: List[str]) -> List[BufferedReader]:
4239
file_list = []
4340
for file in files:
44-
with open(file, "rb") as f:
45-
file_list.append(f)
41+
file_list.append(open(file, "rb"))
4642
return file_list
4743

48-
def post_file(
44+
def post_files(
4945
self, file: List[str], headers: Dict[str, str] = None, **kwargs
5046
) -> dict | Exception:
5147
try:
@@ -55,5 +51,4 @@ def post_file(
5551
r.raise_for_status()
5652
return r.json()
5753
except Exception as e:
58-
print(e)
59-
return e
54+
raise e
File renamed without changes.
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
from axios import Axios
2-
from utils import is_dir, walk_dir_tree
3-
from config import Config
1+
from typing import List
2+
from .axios import Axios
3+
from .utils import is_dir, walk_dir_tree
4+
from .config import Config
5+
from . import types as t
46

57

6-
def deploy(source: str, token: str):
8+
def deploy(source: str, token: str) -> t.Deploy:
79
"""
810
Deploy a file or directory to the lighthouse network
911
@params {source}: str, path to file or directory
1012
@params {token}: str, lighthouse api token
1113
"""
1214
try:
1315
# create http object
14-
axios = Axios(Config.lighthouse_node)
16+
axios = Axios(Config.lighthouse_node + "/api/v0/add")
1517
# create list of files to upload
16-
file_list = []
18+
file_list: List[str] = []
1719
# check if source is a directory
1820
if is_dir(source):
1921
# walk directory tree and add files to list
@@ -23,18 +25,13 @@ def deploy(source: str, token: str):
2325
file_list.append(source)
2426

2527
# create headers
26-
"""
27-
"Content-type": `multipart/form-data; boundary= ${data._boundary}`,
28-
"Encryption": false,
29-
"Mime-Type": mimeType,
30-
"""
3128
headers = {
3229
"Authorization": f"Bearer {token}",
3330
"Content-Type": "multipart/form-data",
3431
"Encryption": "false",
3532
"Mime-Type": "application/octet-stream",
3633
}
3734
# upload files
38-
axios.post_file(file_list, headers)
35+
return axios.post_files(file_list, headers)
3936
except Exception as e:
40-
print(e)
37+
raise e

lighthouse/types.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from dataclasses import dataclass
2+
from typing import Dict, NewType, List, Tuple, TypedDict
3+
4+
5+
@dataclass
6+
class Deploy(TypedDict):
7+
"""typings for deploy function
8+
"""
9+
data: dict
File renamed without changes.

main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
3+
import lighthouse.deploy as d
4+
from lighthouse import types as t
5+
6+
7+
class Lighthouse:
8+
def __init__(self, token: str):
9+
self.token = token
10+
11+
def deploy(self, source: str) -> t.Deploy:
12+
"""
13+
Deploy a file or directory to the lighthouse network
14+
@params {source}: str, path to file or directory
15+
"""
16+
try:
17+
return d.deploy(source, self.token)
18+
except Exception as e:
19+
raise e

tests/__init__.py

Whitespace-only changes.

tests/deploy_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
from main import Lighthouse
5+
6+
7+
def run_test():
8+
"""setup test environment and run tests"""
9+
parse_env()
10+
test_deploy()
11+
12+
13+
def test_deploy():
14+
"""test deploy function"""
15+
l = Lighthouse(os.environ["LH_TOKEN"])
16+
assert l.deploy("tests/upload_test.txt") == {"data": {}}
17+
18+
19+
def parse_env():
20+
"""parse .env file"""
21+
with open(".env", "r") as f:
22+
for line in f.readlines():
23+
if line.startswith("#"):
24+
continue
25+
key, value = line.split("=")
26+
os.environ[key] = value.strip()
27+
28+
29+
if __name__ == "__main__":
30+
run_test()

0 commit comments

Comments
 (0)