Skip to content

Commit 4ed8277

Browse files
odesenfanshoh
authored andcommitted
Internal: user session class
Problem: we often pass 2-3 objects to each function that represent information on how to connect to the Aleph network: * the user account * the API server URL * the aiohttp session. This can be simplified to improve the user experience and reduce the complexity of the SDK. Solution: introduce a new state class: `UserSession`. This object is now passed as the first parameter of every public function. It is initialized with the user account and the API server URL. It is in charge of configuring and managing the aiohttp session object. The typical usage of the library now looks like this: ``` account = load_my_account() api_server = "https://aleph.cloud" # example async with UserSession(account=account, api_server=api_server) as session: message, status = await create_post( session=session, ... ) ``` This enables the following improvements: * Less clutter in function signatures: all public SDK functions now have 1 or 2 fewer arguments. * The API server URL is now managed when initializing the aiohttp session inside the user session object. Implementations can simply specify the endpoint URL. Ex: `f"{api_server}/api/v0/messages.json` can now be expressed as `"/api/v0/messages.json"`. Breaking changes: * The signatures of all public functions of the SDK have been modified. The user must now initialize the user session object and pass it as parameter.
1 parent 1d778a6 commit 4ed8277

File tree

16 files changed

+491
-565
lines changed

16 files changed

+491
-565
lines changed

examples/httpgateway.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
from aiohttp import web
2121

22+
from aleph_client.user_session import UserSession
23+
2224
app = web.Application()
2325
routes = web.RouteTableDef()
2426

@@ -42,13 +44,13 @@ async def source_post(request):
4244
return web.json_response(
4345
{"status": "error", "message": "unauthorized secret"}
4446
)
45-
message, _status = await create_post(
46-
app["account"],
47-
data,
48-
"event",
49-
channel=app["channel"],
50-
api_server="https://api2.aleph.im",
51-
)
47+
async with UserSession(account=app["account"], api_server="https://api2.aleph.im") as session:
48+
message, _status = await create_post(
49+
session=session,
50+
post_content=data,
51+
post_type="event",
52+
channel=app["channel"],
53+
)
5254

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

examples/store.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from aleph_client.asynchronous import create_store
88
from aleph_client.chains.common import get_fallback_private_key
99
from aleph_client.chains.ethereum import ETHAccount
10+
from aleph_client.conf import settings
1011
from aleph_client.types import MessageStatus
12+
from aleph_client.user_session import UserSession
1113

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

@@ -23,7 +25,7 @@ async def print_output_hash(message: StoreMessage, status: MessageStatus):
2325

2426

2527
async def do_upload(account, engine, channel, filename=None, file_hash=None):
26-
async with aiohttp.ClientSession() as session:
28+
async with UserSession(account=account, api_server=settings.API_HOST) as session:
2729
print(filename, account.get_address())
2830
if filename:
2931
try:
@@ -34,23 +36,21 @@ async def do_upload(account, engine, channel, filename=None, file_hash=None):
3436
print("File too big for native STORAGE engine")
3537
return
3638
message, status = await create_store(
37-
account,
39+
session=session,
3840
file_content=content,
3941
channel=channel,
4042
storage_engine=engine.lower(),
41-
session=session,
4243
)
4344
except IOError:
4445
print("File not accessible")
4546
raise
4647

4748
elif file_hash:
4849
message, status = await create_store(
49-
account,
50+
session=session,
5051
file_hash=file_hash,
5152
channel=channel,
5253
storage_engine=engine.lower(),
53-
session=session,
5454
)
5555

5656
await print_output_hash(message, status)

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ testing =
6060
pytest
6161
pytest-cov
6262
pytest-asyncio
63+
pytest-mock
6364
mypy
6465
secp256k1
6566
pynacl
@@ -69,6 +70,7 @@ testing =
6970
httpx
7071
requests
7172
aleph-pytezos==0.1.0
73+
types-certifi
7274
types-setuptools
7375
black
7476
mqtt =

0 commit comments

Comments
 (0)