Skip to content

Commit 254379a

Browse files
committed
Fix: cleaning the code
1 parent 1004c8c commit 254379a

File tree

2 files changed

+46
-41
lines changed

2 files changed

+46
-41
lines changed

src/aleph/sdk/client.py

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
Type,
2323
TypeVar,
2424
Union,
25-
Protocol,
2625
)
2726
from io import BytesIO
2827

@@ -48,7 +47,7 @@
4847
from pydantic import ValidationError, BaseModel
4948

5049
from aleph.sdk.types import Account, GenericMessage, StorageEnum
51-
50+
from aleph.sdk.utils import copy_async_readable_to_buffer, Writable, AsyncReadable
5251
from .conf import settings
5352
from .exceptions import (
5453
BroadcastError,
@@ -69,28 +68,6 @@
6968
magic = None # type:ignore
7069

7170
T = TypeVar("T")
72-
C = TypeVar("C", str, bytes, covariant=True)
73-
U = TypeVar("U", str, bytes, contravariant=True)
74-
75-
76-
class AsyncReadable(Protocol[C]):
77-
async def read(self, n: int = -1) -> C:
78-
...
79-
80-
81-
class Writable(Protocol[U]):
82-
def write(self, buffer: U) -> int:
83-
...
84-
85-
86-
async def copy_async_readable_to_buffer(
87-
readable: AsyncReadable[C], buffer: Writable[C], chunk_size: int
88-
):
89-
while True:
90-
chunk = await readable.read(chunk_size)
91-
if not chunk:
92-
break
93-
buffer.write(chunk)
9471

9572

9673
def async_wrapper(f):
@@ -651,22 +628,21 @@ async def download_file_to_buffer(
651628
:param output_buffer: Writable binary buffer. The file will be written to this buffer.
652629
"""
653630

654-
async with aiohttp.ClientSession() as session:
655-
async with self.http_session.get(
656-
f"/api/v0/storage/raw/{file_hash}"
657-
) as response:
658-
if response.status == 200:
659-
await copy_async_readable_to_buffer(
660-
response.content, output_buffer, chunk_size=16 * 1024
631+
async with self.http_session.get(
632+
f"/api/v0/storage/raw/{file_hash}"
633+
) as response:
634+
if response.status == 200:
635+
await copy_async_readable_to_buffer(
636+
response.content, output_buffer, chunk_size=16 * 1024
637+
)
638+
if response.status == 413:
639+
ipfs_hash = ItemHash(file_hash)
640+
if ipfs_hash.item_type == ItemType.ipfs:
641+
return await self.download_file_ipfs_to_buffer(
642+
file_hash, output_buffer
661643
)
662-
if response.status == 413:
663-
ipfs_hash = ItemHash(file_hash)
664-
if ItemType.from_hash(ipfs_hash) == ItemType.ipfs:
665-
return await self.download_file_ipfs_to_buffer(
666-
file_hash, output_buffer
667-
)
668-
else:
669-
raise FileTooLarge(f"The file from {file_hash} is too large")
644+
else:
645+
raise FileTooLarge(f"The file from {file_hash} is too large")
670646

671647
async def download_file_ipfs_to_buffer(
672648
self,
@@ -687,8 +663,6 @@ async def download_file_ipfs_to_buffer(
687663
await copy_async_readable_to_buffer(
688664
response.content, output_buffer, chunk_size=16 * 1024
689665
)
690-
elif response.status == 413:
691-
raise FileTooLarge()
692666
else:
693667
response.raise_for_status()
694668

src/aleph/sdk/utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
from aleph.sdk.conf import settings
1313
from aleph.sdk.types import GenericMessage
1414

15+
from typing import (
16+
Tuple,
17+
Type,
18+
TypeVar,
19+
Protocol,
20+
)
21+
1522
logger = logging.getLogger(__name__)
1623

1724
try:
@@ -76,3 +83,27 @@ def check_unix_socket_valid(unix_socket_path: str) -> bool:
7683
unix_socket_path,
7784
)
7885
return True
86+
87+
88+
C = TypeVar("C", str, bytes, covariant=True)
89+
U = TypeVar("U", str, bytes, contravariant=True)
90+
91+
92+
class AsyncReadable(Protocol[C]):
93+
async def read(self, n: int = -1) -> C:
94+
...
95+
96+
97+
class Writable(Protocol[U]):
98+
def write(self, buffer: U) -> int:
99+
...
100+
101+
102+
async def copy_async_readable_to_buffer(
103+
readable: AsyncReadable[C], buffer: Writable[C], chunk_size: int
104+
):
105+
while True:
106+
chunk = await readable.read(chunk_size)
107+
if not chunk:
108+
break
109+
buffer.write(chunk)

0 commit comments

Comments
 (0)