Skip to content

Commit 47c32d0

Browse files
committed
Fix: Restructure and remove the sync wrapper
This reorganizes the code and removes the wrapper to call async code synchronously since it added a lot of hacky code.
1 parent 3d7504d commit 47c32d0

25 files changed

+320
-639
lines changed

examples/httpgateway.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from aleph.sdk.chains.common import get_fallback_private_key
99
from aleph.sdk.chains.ethereum import ETHAccount
10-
from aleph.sdk.client import AuthenticatedAlephClient
10+
from aleph.sdk.client import AuthenticatedAlephHttpClient
1111

1212
app = web.Application()
1313
routes = web.RouteTableDef()
@@ -32,7 +32,7 @@ async def source_post(request):
3232
return web.json_response(
3333
{"status": "error", "message": "unauthorized secret"}
3434
)
35-
async with AuthenticatedAlephClient(
35+
async with AuthenticatedAlephHttpClient(
3636
account=app["account"], api_server="https://api2.aleph.im"
3737
) as session:
3838
message, _status = await session.create_post(

examples/metrics.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from aleph_message.status import MessageStatus
1313

1414
from aleph.sdk.chains.ethereum import get_fallback_account
15-
from aleph.sdk.client import AuthenticatedAlephClient, AuthenticatedUserSessionSync
15+
from aleph.sdk.client import AuthenticatedAlephHttpClient, AuthenticatedAlephClientSync
1616
from aleph.sdk.conf import settings
1717

1818

@@ -54,7 +54,7 @@ def get_cpu_cores():
5454

5555

5656
def send_metrics(
57-
session: AuthenticatedUserSessionSync, metrics
57+
session: AuthenticatedAlephClientSync, metrics
5858
) -> Tuple[AlephMessage, MessageStatus]:
5959
return session.create_aggregate(key="metrics", content=metrics, channel="SYSINFO")
6060

@@ -70,7 +70,7 @@ def collect_metrics():
7070

7171
def main():
7272
account = get_fallback_account()
73-
with AuthenticatedAlephClient(
73+
with AuthenticatedAlephHttpClient(
7474
account=account, api_server=settings.API_HOST
7575
) as session:
7676
while True:

examples/mqtt.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from aleph.sdk.chains.common import get_fallback_private_key
1212
from aleph.sdk.chains.ethereum import ETHAccount
13-
from aleph.sdk.client import AuthenticatedAlephClient
13+
from aleph.sdk.client import AuthenticatedAlephHttpClient
1414
from aleph.sdk.conf import settings
1515

1616

@@ -27,7 +27,7 @@ def get_input_data(value):
2727

2828

2929
def send_metrics(account, metrics):
30-
with AuthenticatedAlephClient(
30+
with AuthenticatedAlephHttpClient(
3131
account=account, api_server=settings.API_HOST
3232
) as session:
3333
return session.create_aggregate(
@@ -100,7 +100,7 @@ async def gateway(
100100
if not userdata["received"]:
101101
await client.reconnect()
102102

103-
async with AuthenticatedAlephClient(
103+
async with AuthenticatedAlephHttpClient(
104104
account=account, api_server=settings.API_HOST
105105
) as session:
106106
for key, value in state.items():

examples/store.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from aleph.sdk.chains.common import get_fallback_private_key
88
from aleph.sdk.chains.ethereum import ETHAccount
9-
from aleph.sdk.client import AuthenticatedAlephClient
9+
from aleph.sdk.client import AuthenticatedAlephHttpClient
1010
from aleph.sdk.conf import settings
1111

1212
DEFAULT_SERVER = "https://api2.aleph.im"
@@ -23,7 +23,7 @@ async def print_output_hash(message: StoreMessage, status: MessageStatus):
2323

2424

2525
async def do_upload(account, engine, channel, filename=None, file_hash=None):
26-
async with AuthenticatedAlephClient(
26+
async with AuthenticatedAlephHttpClient(
2727
account=account, api_server=settings.API_HOST
2828
) as session:
2929
print(filename, account.get_address())

src/aleph/sdk/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pkg_resources import DistributionNotFound, get_distribution
22

3-
from aleph.sdk.client import AlephClient, AuthenticatedAlephClient
3+
from aleph.sdk.client import AlephHttpClient, AuthenticatedAlephHttpClient
44

55
try:
66
# Change here if project is renamed and does not equal the package name
@@ -11,4 +11,4 @@
1111
finally:
1212
del get_distribution, DistributionNotFound
1313

14-
__all__ = ["AlephClient", "AuthenticatedAlephClient"]
14+
__all__ = ["AlephHttpClient", "AuthenticatedAlephHttpClient"]

src/aleph/sdk/client/__init__.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
from .authenticated import AuthenticatedAlephClient, AuthenticatedUserSessionSync
2-
from .base import BaseAlephClient, BaseAuthenticatedAlephClient
3-
from .client import AlephClient, UserSessionSync
1+
from .abstract import AlephClient, AuthenticatedAlephClient
2+
from .authenticated_http import AuthenticatedAlephHttpClient
3+
from .http import AlephHttpClient
44

55
__all__ = [
6-
"BaseAlephClient",
7-
"BaseAuthenticatedAlephClient",
86
"AlephClient",
97
"AuthenticatedAlephClient",
10-
"UserSessionSync",
11-
"AuthenticatedUserSessionSync",
8+
"AlephHttpClient",
9+
"AuthenticatedAlephHttpClient",
1210
]

src/aleph/sdk/client/base.py src/aleph/sdk/client/abstract.py

+59-4
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
from aleph_message.models.execution.program import Encoding
2626
from aleph_message.status import MessageStatus
2727

28-
from ..models.message import MessageFilter
29-
from ..models.post import PostFilter, PostsResponse
28+
from ..query.filters import MessageFilter, PostFilter
29+
from ..query.responses import PostsResponse
3030
from ..types import GenericMessage, StorageEnum
31+
from ..utils import Writable
3132

3233
DEFAULT_PAGE_SIZE = 200
3334

3435

35-
class BaseAlephClient(ABC):
36+
class AlephClient(ABC):
3637
@abstractmethod
3738
async def fetch_aggregate(self, address: str, key: str) -> Dict[str, Dict]:
3839
"""
@@ -110,6 +111,44 @@ async def download_file(
110111
"""
111112
pass
112113

114+
async def download_file_ipfs(
115+
self,
116+
file_hash: str,
117+
) -> bytes:
118+
"""
119+
Get a file from the ipfs storage engine as raw bytes.
120+
121+
Warning: Downloading large files can be slow.
122+
123+
:param file_hash: The hash of the file to retrieve.
124+
"""
125+
raise NotImplementedError()
126+
127+
async def download_file_ipfs_to_buffer(
128+
self,
129+
file_hash: str,
130+
output_buffer: Writable[bytes],
131+
) -> None:
132+
"""
133+
Download a file from the storage engine and write it to the specified output buffer.
134+
135+
:param file_hash: The hash of the file to retrieve.
136+
:param output_buffer: The binary output buffer to write the file data to.
137+
"""
138+
raise NotImplementedError()
139+
140+
async def download_file_to_buffer(
141+
self,
142+
file_hash: str,
143+
output_buffer: Writable[bytes],
144+
) -> None:
145+
"""
146+
Download a file from the storage engine and write it to the specified output buffer.
147+
:param file_hash: The hash of the file to retrieve.
148+
:param output_buffer: Writable binary buffer. The file will be written to this buffer.
149+
"""
150+
raise NotImplementedError()
151+
113152
@abstractmethod
114153
async def get_messages(
115154
self,
@@ -180,7 +219,7 @@ def watch_messages(
180219
pass
181220

182221

183-
class BaseAuthenticatedAlephClient(BaseAlephClient):
222+
class AuthenticatedAlephClient(AlephClient):
184223
@abstractmethod
185224
async def create_post(
186225
self,
@@ -350,3 +389,19 @@ async def submit(
350389
:param sync: If true, waits for the message to be processed by the API server (Default: False)
351390
"""
352391
pass
392+
393+
async def ipfs_push(self, content: Mapping) -> str:
394+
"""
395+
Push a file to IPFS.
396+
397+
:param content: Content of the file to push
398+
"""
399+
raise NotImplementedError()
400+
401+
async def storage_push(self, content: Mapping) -> str:
402+
"""
403+
Push arbitrary content as JSON to the storage service.
404+
405+
:param content: The dict-like content to upload
406+
"""
407+
raise NotImplementedError()

0 commit comments

Comments
 (0)