Skip to content

Commit a8fb1f5

Browse files
authored
Merge pull request #6 from lighthouse-web3/fix-requirements
[fix]: requirements
2 parents 3aea391 + fd37ea3 commit a8fb1f5

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,4 @@ cython_debug/
159159
# and can be added to the global gitignore or merged into this file. For a more nuclear
160160
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
161161
#.idea/
162-
src/lighthouseweb3/functions/download.py
163-
164162
image.png

requirements.txt

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
black==22.8.0
2-
certifi==2022.9.14
3-
charset-normalizer==2.1.1
4-
click==8.1.3
1+
certifi==2023.5.7
2+
charset-normalizer==3.1.0
53
idna==3.4
6-
mypy-extensions==0.4.3
7-
nose2==0.12.0
8-
pathspec==0.10.1
9-
platformdirs==2.5.2
10-
requests==2.28.1
11-
tomli==2.0.1
12-
urllib3==1.26.12
4+
requests==2.31.0
5+
urllib3==2.0.2
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import requests
2+
import warnings
3+
import io
4+
from .axios import Axios
5+
from .config import Config
6+
7+
8+
# 10MB chunks by default
9+
def download_file_into_writable(cid: str, writable_object: io.BufferedWriter, chunk_size=1024*1024*10):
10+
url = f"{Config.lighthouse_gateway}/{cid}"
11+
with requests.get(url, stream=True) as r:
12+
r.raise_for_status()
13+
for chunk in r.iter_content(chunk_size=chunk_size):
14+
if chunk: # filter out keep-alive new chunks
15+
writable_object.write(chunk)
16+
return {"data": {"Hash": cid, "Size": writable_object.tell()}}
17+
18+
19+
def get_url_body(url):
20+
response = requests.get(url)
21+
response.raise_for_status() # Raises stored HTTPError, if one occurred.
22+
return response.content, response.headers
23+
24+
25+
def get_file(cid: str) -> (bytes, str):
26+
try:
27+
url = f"{Config.lighthouse_gateway}/{cid}"
28+
29+
(body, headers) = get_url_body(url)
30+
31+
# show a warning if the file is greater then 2GB
32+
if (int(headers['Content-Length']) > 1024*1024*1024*2):
33+
warnings.warn(
34+
"This content of the file is grater then 2GB, use `downloadBlob` instead", UserWarning)
35+
return (body, headers['Content-Type'])
36+
except requests.HTTPError as error:
37+
raise Exception(error.response.text)
38+
39+
40+
def getTaggedCid(tag: str, token: str):
41+
42+
_axios = Axios(
43+
f"{Config.lighthouse_api}/api/user/get_tag_details?tag={tag}")
44+
data = _axios.get({
45+
"Authorization": f"Bearer {token}"
46+
})
47+
return data
48+
49+
# return {"data": {"Hash": cid, "Size": writable_object.tell()}}

tests/test_download.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_download_file(self):
3232
"Qmd5MBBScDUV3Ly8qahXtZFqyRRfYSmUwEcxpYcV4hzKfW")
3333
self.assertIsInstance(res, bytes, "type doesn't match")
3434
self.assertEqual(res, b'tests/testdir/', "data doesn't match")
35+
self.assertEqual(res.decode('utf-8'), 'tests/testdir/')
3536

3637
def test_download_blob_file(self):
3738
"""test download_blob function"""
@@ -43,6 +44,5 @@ def test_download_blob_file(self):
4344
123939, "File Size dont match")
4445

4546

46-
4747
if __name__ == "__main__":
4848
unittest.main()

tests/test_upload.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ def test_upload_with_tag(self):
8686
self.assertEqual(res.get("data").get("Hash"), tagData.get(
8787
"data").get("cid"), "Tag dont match")
8888

89+
def test_tag_notFound(self):
90+
"""test Upload with tag function"""
91+
l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN"))
92+
tag = generate_random_string(9)
93+
tagData = l.getTagged(tag)
94+
self.assertEqual(None, tagData.get("data"), "Tag dont match")
95+
8996

9097
if __name__ == "__main__":
9198
unittest.main()

0 commit comments

Comments
 (0)