Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AlephClient classes #65

Merged
merged 6 commits into from
Oct 23, 2023
Merged
4 changes: 2 additions & 2 deletions examples/httpgateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from aleph.sdk.chains.common import get_fallback_private_key
from aleph.sdk.chains.ethereum import ETHAccount
from aleph.sdk.client import AuthenticatedAlephClient
from aleph.sdk.client import AuthenticatedAlephHttpClient

app = web.Application()
routes = web.RouteTableDef()
Expand All @@ -32,7 +32,7 @@ async def source_post(request):
return web.json_response(
{"status": "error", "message": "unauthorized secret"}
)
async with AuthenticatedAlephClient(
async with AuthenticatedAlephHttpClient(
account=app["account"], api_server="https://api2.aleph.im"
) as session:
message, _status = await session.create_post(
Expand Down
23 changes: 13 additions & 10 deletions examples/metrics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
""" Server metrics upload.
"""
# -*- coding: utf-8 -*-

import asyncio
import os
import platform
import time
Expand All @@ -12,9 +11,11 @@
from aleph_message.status import MessageStatus

from aleph.sdk.chains.ethereum import get_fallback_account
from aleph.sdk.client import AuthenticatedAlephClient, AuthenticatedUserSessionSync
from aleph.sdk.client import AuthenticatedAlephHttpClient
from aleph.sdk.conf import settings

# -*- coding: utf-8 -*-


def get_sysinfo():
uptime = int(time.time() - psutil.boot_time())
Expand Down Expand Up @@ -53,10 +54,12 @@ def get_cpu_cores():
return [c._asdict() for c in psutil.cpu_times_percent(0, percpu=True)]


def send_metrics(
session: AuthenticatedUserSessionSync, metrics
async def send_metrics(
session: AuthenticatedAlephHttpClient, metrics
) -> Tuple[AlephMessage, MessageStatus]:
return session.create_aggregate(key="metrics", content=metrics, channel="SYSINFO")
return await session.create_aggregate(
key="metrics", content=metrics, channel="SYSINFO"
)


def collect_metrics():
Expand All @@ -68,17 +71,17 @@ def collect_metrics():
}


def main():
async def main():
account = get_fallback_account()
with AuthenticatedAlephClient(
async with AuthenticatedAlephHttpClient(
account=account, api_server=settings.API_HOST
) as session:
while True:
metrics = collect_metrics()
message, status = send_metrics(session, metrics)
message, status = await send_metrics(session, metrics)
print("sent", message.item_hash)
time.sleep(10)


if __name__ == "__main__":
main()
asyncio.run(main())
8 changes: 4 additions & 4 deletions examples/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from aleph.sdk.chains.common import get_fallback_private_key
from aleph.sdk.chains.ethereum import ETHAccount
from aleph.sdk.client import AuthenticatedAlephClient
from aleph.sdk.client import AuthenticatedAlephHttpClient
from aleph.sdk.conf import settings


Expand All @@ -26,8 +26,8 @@ def get_input_data(value):
return value.decode("utf-8")


def send_metrics(account, metrics):
with AuthenticatedAlephClient(
async def send_metrics(account, metrics):
async with AuthenticatedAlephHttpClient(
account=account, api_server=settings.API_HOST
) as session:
return session.create_aggregate(
Expand Down Expand Up @@ -100,7 +100,7 @@ async def gateway(
if not userdata["received"]:
await client.reconnect()

async with AuthenticatedAlephClient(
async with AuthenticatedAlephHttpClient(
account=account, api_server=settings.API_HOST
) as session:
for key, value in state.items():
Expand Down
4 changes: 2 additions & 2 deletions examples/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from aleph.sdk.chains.common import get_fallback_private_key
from aleph.sdk.chains.ethereum import ETHAccount
from aleph.sdk.client import AuthenticatedAlephClient
from aleph.sdk.client import AuthenticatedAlephHttpClient
from aleph.sdk.conf import settings

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


async def do_upload(account, engine, channel, filename=None, file_hash=None):
async with AuthenticatedAlephClient(
async with AuthenticatedAlephHttpClient(
account=account, api_server=settings.API_HOST
) as session:
print(filename, account.get_address())
Expand Down
4 changes: 2 additions & 2 deletions src/aleph/sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pkg_resources import DistributionNotFound, get_distribution

from aleph.sdk.client import AlephClient, AuthenticatedAlephClient
from aleph.sdk.client import AlephHttpClient, AuthenticatedAlephHttpClient

try:
# Change here if project is renamed and does not equal the package name
Expand All @@ -11,4 +11,4 @@
finally:
del get_distribution, DistributionNotFound

__all__ = ["AlephClient", "AuthenticatedAlephClient"]
__all__ = ["AlephHttpClient", "AuthenticatedAlephHttpClient"]
Loading