Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions examples/httpgateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@
"""
# -*- coding: utf-8 -*-

import os

# import requests
import platform

# import socket
import time
import asyncio

import click
from aleph_client.asynchronous import create_post
from aiohttp import web

# from aleph_client.chains.nuls1 import NULSAccount, get_fallback_account
from aleph_client.chains.ethereum import ETHAccount
from aleph_client.chains.common import get_fallback_private_key

from aiohttp import web
from aleph_client.chains.ethereum import ETHAccount
from aleph_client.user_session import AuthenticatedUserSession

app = web.Application()
routes = web.RouteTableDef()
Expand All @@ -42,13 +34,14 @@ async def source_post(request):
return web.json_response(
{"status": "error", "message": "unauthorized secret"}
)
message, _status = await create_post(
app["account"],
data,
"event",
channel=app["channel"],
api_server="https://api2.aleph.im",
)
async with AuthenticatedUserSession(
account=app["account"], api_server="https://api2.aleph.im"
) as session:
message, _status = await session.create_post(
post_content=data,
post_type="event",
channel=app["channel"],
)

return web.json_response({"status": "success", "item_hash": message.item_hash})

Expand Down
26 changes: 18 additions & 8 deletions examples/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import os
import platform
import time
from typing import Tuple

import psutil
from aleph_message.models import AlephMessage

from aleph_client import AuthenticatedUserSession
from aleph_client.chains.ethereum import get_fallback_account
from aleph_client.synchronous import create_aggregate
from aleph_client.conf import settings
from aleph_client.types import MessageStatus
from aleph_client.user_session import AuthenticatedUserSessionSync


def get_sysinfo():
Expand Down Expand Up @@ -49,8 +54,10 @@ def get_cpu_cores():
return [c._asdict() for c in psutil.cpu_times_percent(0, percpu=True)]


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


def collect_metrics():
Expand All @@ -64,11 +71,14 @@ def collect_metrics():

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


if __name__ == "__main__":
Expand Down
23 changes: 17 additions & 6 deletions examples/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

from aleph_client.chains.common import get_fallback_private_key
from aleph_client.chains.ethereum import ETHAccount
from aleph_client.main import create_aggregate
from aleph_client import AuthenticatedUserSession
from aleph_client.conf import settings


def get_input_data(value):
Expand All @@ -26,7 +27,12 @@ def get_input_data(value):


def send_metrics(account, metrics):
return create_aggregate(account, "metrics", metrics, channel="SYSINFO")
with AuthenticatedUserSession(
account=account, api_server=settings.API_HOST
) as session:
return session.create_aggregate(
key="metrics", content=metrics, channel="SYSINFO"
)


def on_disconnect(client, userdata, rc):
Expand Down Expand Up @@ -95,10 +101,15 @@ async def gateway(
if not userdata["received"]:
await client.reconnect()

for key, value in state.items():
message, status = create_aggregate(account, key, value, channel="IOT_TEST")
print("sent", message.item_hash)
userdata["received"] = False
async with AuthenticatedUserSession(
account=account, api_server=settings.API_HOST
) as session:
for key, value in state.items():
message, status = await session.create_aggregate(
key=key, content=value, channel="IOT_TEST"
)
print("sent", message.item_hash)
userdata["received"] = False


@click.command()
Expand Down
16 changes: 7 additions & 9 deletions examples/store.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asyncio

import aiohttp
import click
from aleph_message.models import StoreMessage

from aleph_client.asynchronous import create_store
from aleph_client.chains.common import get_fallback_private_key
from aleph_client.chains.ethereum import ETHAccount
from aleph_client.conf import settings
from aleph_client.types import MessageStatus
from aleph_client.user_session import AuthenticatedUserSession

DEFAULT_SERVER = "https://api2.aleph.im"

Expand All @@ -23,7 +23,9 @@ async def print_output_hash(message: StoreMessage, status: MessageStatus):


async def do_upload(account, engine, channel, filename=None, file_hash=None):
async with aiohttp.ClientSession() as session:
async with AuthenticatedUserSession(
account=account, api_server=settings.API_HOST
) as session:
print(filename, account.get_address())
if filename:
try:
Expand All @@ -33,24 +35,20 @@ async def do_upload(account, engine, channel, filename=None, file_hash=None):
if len(content) > 4 * 1024 * 1024 and engine == "STORAGE":
print("File too big for native STORAGE engine")
return
message, status = await create_store(
account,
message, status = await session.create_store(
file_content=content,
channel=channel,
storage_engine=engine.lower(),
session=session,
)
except IOError:
print("File not accessible")
raise

elif file_hash:
message, status = await create_store(
account,
message, status = await session.create_store(
file_hash=file_hash,
channel=channel,
storage_engine=engine.lower(),
session=session,
)

await print_output_hash(message, status)
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ testing =
pytest
pytest-cov
pytest-asyncio
pytest-mock
mypy
secp256k1
pynacl
Expand All @@ -69,6 +70,7 @@ testing =
httpx
requests
aleph-pytezos==0.1.0
types-certifi
types-setuptools
black
mqtt =
Expand Down
4 changes: 4 additions & 0 deletions src/aleph_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pkg_resources import get_distribution, DistributionNotFound
from .user_session import AuthenticatedUserSession, UserSession

try:
# Change here if project is renamed and does not equal the package name
Expand All @@ -8,3 +9,6 @@
__version__ = "unknown"
finally:
del get_distribution, DistributionNotFound


__all__ = ["AuthenticatedUserSession", "UserSession"]
Loading