Skip to content

Commit 44d8c0e

Browse files
odesenfanshoh
authored andcommitted
UserSession + AuthenticatedUserSession
1 parent 4ed8277 commit 44d8c0e

File tree

9 files changed

+51
-50
lines changed

9 files changed

+51
-50
lines changed

examples/httpgateway.py

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

2020
from aiohttp import web
2121

22-
from aleph_client.user_session import UserSession
22+
from aleph_client.user_session import AuthenticatedUserSession
2323

2424
app = web.Application()
2525
routes = web.RouteTableDef()
@@ -44,7 +44,7 @@ async def source_post(request):
4444
return web.json_response(
4545
{"status": "error", "message": "unauthorized secret"}
4646
)
47-
async with UserSession(account=app["account"], api_server="https://api2.aleph.im") as session:
47+
async with AuthenticatedUserSession(account=app["account"], api_server="https://api2.aleph.im") as session:
4848
message, _status = await create_post(
4949
session=session,
5050
post_content=data,

examples/store.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from aleph_client.chains.ethereum import ETHAccount
1010
from aleph_client.conf import settings
1111
from aleph_client.types import MessageStatus
12-
from aleph_client.user_session import UserSession
12+
from aleph_client.user_session import AuthenticatedUserSession
1313

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

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

2626

2727
async def do_upload(account, engine, channel, filename=None, file_hash=None):
28-
async with UserSession(account=account, api_server=settings.API_HOST) as session:
28+
async with AuthenticatedUserSession(account=account, api_server=settings.API_HOST) as session:
2929
print(filename, account.get_address())
3030
if filename:
3131
try:

src/aleph_client/asynchronous.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
BroadcastError,
4949
)
5050
from .models import MessagesResponse
51-
from .user_session import UserSession
51+
from .user_session import AuthenticatedUserSession, UserSession
5252
from .utils import get_message_type_value
5353

5454
logger = logging.getLogger(__name__)
@@ -60,7 +60,7 @@
6060
magic = None # type:ignore
6161

6262

63-
async def ipfs_push(session: UserSession, content: Mapping) -> str:
63+
async def ipfs_push(session: AuthenticatedUserSession, content: Mapping) -> str:
6464
"""Push arbitrary content as JSON to the IPFS service."""
6565

6666
url = "/api/v0/ipfs/add_json"
@@ -71,7 +71,7 @@ async def ipfs_push(session: UserSession, content: Mapping) -> str:
7171
return (await resp.json()).get("hash")
7272

7373

74-
async def storage_push(session: UserSession, content: Mapping) -> str:
74+
async def storage_push(session: AuthenticatedUserSession, content: Mapping) -> str:
7575
"""Push arbitrary content as JSON to the storage service."""
7676

7777
url = "/api/v0/storage/add_json"
@@ -82,7 +82,9 @@ async def storage_push(session: UserSession, content: Mapping) -> str:
8282
return (await resp.json()).get("hash")
8383

8484

85-
async def ipfs_push_file(session: UserSession, file_content: Union[str, bytes]) -> str:
85+
async def ipfs_push_file(
86+
session: AuthenticatedUserSession, file_content: Union[str, bytes]
87+
) -> str:
8688
"""Push a file to the IPFS service."""
8789
data = aiohttp.FormData()
8890
data.add_field("file", file_content)
@@ -95,7 +97,7 @@ async def ipfs_push_file(session: UserSession, file_content: Union[str, bytes])
9597
return (await resp.json()).get("hash")
9698

9799

98-
async def storage_push_file(session: UserSession, file_content) -> str:
100+
async def storage_push_file(session: AuthenticatedUserSession, file_content) -> str:
99101
"""Push a file to the storage service."""
100102
data = aiohttp.FormData()
101103
data.add_field("file", file_content)
@@ -162,7 +164,7 @@ async def _handle_broadcast_deprecated_response(
162164

163165

164166
async def _broadcast_deprecated(
165-
session: UserSession, message_dict: Mapping[str, Any]
167+
session: AuthenticatedUserSession, message_dict: Mapping[str, Any]
166168
) -> None:
167169

168170
"""
@@ -212,7 +214,7 @@ async def _handle_broadcast_response(
212214

213215

214216
async def _broadcast(
215-
session: UserSession,
217+
session: AuthenticatedUserSession,
216218
message: AlephMessage,
217219
sync: bool,
218220
) -> MessageStatus:
@@ -247,7 +249,7 @@ async def _broadcast(
247249

248250

249251
async def create_post(
250-
session: UserSession,
252+
session: AuthenticatedUserSession,
251253
post_content,
252254
post_type: str,
253255
ref: Optional[str] = None,
@@ -292,7 +294,7 @@ async def create_post(
292294

293295

294296
async def create_aggregate(
295-
session: UserSession,
297+
session: AuthenticatedUserSession,
296298
key: str,
297299
content: Mapping[str, Any],
298300
address: Optional[str] = None,
@@ -331,7 +333,7 @@ async def create_aggregate(
331333

332334

333335
async def create_store(
334-
session: UserSession,
336+
session: AuthenticatedUserSession,
335337
address: Optional[str] = None,
336338
file_content: Optional[bytes] = None,
337339
file_path: Optional[Union[str, Path]] = None,
@@ -414,7 +416,7 @@ async def create_store(
414416

415417

416418
async def create_program(
417-
session: UserSession,
419+
session: AuthenticatedUserSession,
418420
program_ref: str,
419421
entrypoint: str,
420422
runtime: str,
@@ -517,7 +519,7 @@ async def create_program(
517519

518520

519521
async def forget(
520-
session: UserSession,
522+
session: AuthenticatedUserSession,
521523
hashes: List[str],
522524
reason: Optional[str],
523525
storage_engine: StorageEnum = StorageEnum.storage,
@@ -566,7 +568,7 @@ def compute_sha256(s: str) -> str:
566568

567569

568570
async def _prepare_aleph_message(
569-
session: UserSession,
571+
session: AuthenticatedUserSession,
570572
message_type: MessageType,
571573
content: Dict[str, Any],
572574
channel: Optional[str],
@@ -609,7 +611,7 @@ async def _prepare_aleph_message(
609611

610612

611613
async def submit(
612-
session: UserSession,
614+
session: AuthenticatedUserSession,
613615
content: Dict[str, Any],
614616
message_type: MessageType,
615617
channel: Optional[str] = None,

src/aleph_client/user_session.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66

77

88
class UserSession:
9-
account: Account
109
api_server: str
1110
http_session: aiohttp.ClientSession
1211

13-
def __init__(self, account: Account, api_server: str):
14-
self.account = account
12+
def __init__(self, api_server: str):
1513
self.api_server = api_server
1614
self.http_session = aiohttp.ClientSession(base_url=api_server)
1715

@@ -31,3 +29,11 @@ async def __aenter__(self):
3129

3230
async def __aexit__(self, exc_type, exc_val, exc_tb):
3331
await self.http_session.close()
32+
33+
34+
class AuthenticatedUserSession(UserSession):
35+
account: Account
36+
37+
def __init__(self, account: Account, api_server: str):
38+
super().__init__(api_server=api_server)
39+
self.account = account

tests/integration/itest_aggregates.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
create_aggregate,
77
fetch_aggregate,
88
)
9-
from aleph_client.user_session import UserSession
9+
from aleph_client.user_session import AuthenticatedUserSession
1010
from tests.integration.toolkit import try_until
1111
from .config import REFERENCE_NODE, TARGET_NODE
1212

@@ -21,7 +21,7 @@ async def create_aggregate_on_target(
2121
receiver_node: str,
2222
channel="INTEGRATION_TESTS",
2323
):
24-
async with UserSession(account=account, api_server=emitter_node) as tx_session:
24+
async with AuthenticatedUserSession(account=account, api_server=emitter_node) as tx_session:
2525
aggregate_message, message_status = await create_aggregate(
2626
session=tx_session,
2727
key=key,
@@ -40,7 +40,7 @@ async def create_aggregate_on_target(
4040
assert aggregate_message.content.address == account.get_address()
4141
assert aggregate_message.content.content == content
4242

43-
async with UserSession(account=account, api_server=receiver_node) as rx_session:
43+
async with AuthenticatedUserSession(account=account, api_server=receiver_node) as rx_session:
4444
aggregate_from_receiver = await try_until(
4545
fetch_aggregate,
4646
lambda aggregate: aggregate is not None,

tests/integration/itest_forget.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from aleph_client.asynchronous import create_post, get_posts, get_messages, forget
66
from aleph_client.types import Account
7-
from aleph_client.user_session import UserSession
7+
from aleph_client.user_session import AuthenticatedUserSession
88
from .config import REFERENCE_NODE, TARGET_NODE, TEST_CHANNEL
99
from .toolkit import try_until
1010

@@ -17,7 +17,7 @@ async def wait_matching_posts(
1717
condition: Callable[[Dict], bool],
1818
timeout: int = 5,
1919
):
20-
async with UserSession(account=account, api_server=receiver_node) as rx_session:
20+
async with AuthenticatedUserSession(account=account, api_server=receiver_node) as rx_session:
2121
return await try_until(
2222
get_posts,
2323
condition,
@@ -26,7 +26,7 @@ async def wait_matching_posts(
2626
hashes=[item_hash],
2727
)
2828

29-
async with UserSession(account=account, api_server=emitter_node) as tx_session:
29+
async with AuthenticatedUserSession(account=account, api_server=emitter_node) as tx_session:
3030
post_message, message_status = await create_post(
3131
session=tx_session,
3232
post_content="A considerate and politically correct post.",
@@ -44,7 +44,7 @@ async def wait_matching_posts(
4444

4545
post_hash = post_message.item_hash
4646
reason = "This well thought-out content offends me!"
47-
async with UserSession(account=account, api_server=emitter_node) as tx_session:
47+
async with AuthenticatedUserSession(account=account, api_server=emitter_node) as tx_session:
4848
forget_message, forget_status = await forget(
4949
session=tx_session,
5050
hashes=[post_hash],
@@ -100,7 +100,7 @@ async def test_forget_a_forget_message(fixture_account):
100100

101101
# TODO: this test should be moved to the PyAleph API tests, once a framework is in place.
102102
post_hash = await create_and_forget_post(fixture_account, TARGET_NODE, TARGET_NODE)
103-
async with UserSession(account=fixture_account, api_server=TARGET_NODE) as session:
103+
async with AuthenticatedUserSession(account=fixture_account, api_server=TARGET_NODE) as session:
104104
get_post_response = await get_posts(session=session, hashes=[post_hash])
105105
assert len(get_post_response["posts"]) == 1
106106
post = get_post_response["posts"][0]

tests/integration/itest_posts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
create_post,
77
get_messages,
88
)
9-
from aleph_client.user_session import UserSession
9+
from aleph_client.user_session import AuthenticatedUserSession
1010
from tests.integration.toolkit import try_until
1111
from .config import REFERENCE_NODE, TARGET_NODE
1212

@@ -17,7 +17,7 @@ async def create_message_on_target(
1717
"""
1818
Create a POST message on the target node, then fetch it from the reference node.
1919
"""
20-
async with UserSession(account=fixture_account, api_server=emitter_node) as tx_session:
20+
async with AuthenticatedUserSession(account=fixture_account, api_server=emitter_node) as tx_session:
2121
post_message, message_status = await create_post(
2222
session=tx_session,
2323
post_content=None,
@@ -28,7 +28,7 @@ async def create_message_on_target(
2828
def response_contains_messages(response: MessagesResponse) -> bool:
2929
return len(response.messages) > 0
3030

31-
async with UserSession(account=fixture_account, api_server=receiver_node) as rx_session:
31+
async with AuthenticatedUserSession(account=fixture_account, api_server=receiver_node) as rx_session:
3232
responses = await try_until(
3333
get_messages,
3434
response_contains_messages,

tests/unit/test_asynchronous_get.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
fetch_aggregate,
1212
)
1313
from aleph_client.conf import settings
14-
from aleph_client.types import Account
1514
from aleph_client.user_session import UserSession
1615

1716

18-
def make_mock_session(mock_account: Account, get_return_value: Dict[str, Any]):
17+
def make_mock_session(get_return_value: Dict[str, Any]):
1918

2019
mock_response = AsyncMock()
2120
mock_response.status = 200
@@ -29,15 +28,14 @@ def make_mock_session(mock_account: Account, get_return_value: Dict[str, Any]):
2928

3029
user_session = AsyncMock()
3130
user_session.http_session = mock_session
32-
user_session.account = mock_account
3331

3432
return user_session
3533

3634

3735
@pytest.mark.asyncio
38-
async def test_fetch_aggregate(ethereum_account: Account):
36+
async def test_fetch_aggregate():
3937
mock_session = make_mock_session(
40-
ethereum_account, {"data": {"corechannel": {"nodes": [], "resource_nodes": []}}}
38+
{"data": {"corechannel": {"nodes": [], "resource_nodes": []}}}
4139
)
4240

4341
response = await fetch_aggregate(
@@ -49,9 +47,9 @@ async def test_fetch_aggregate(ethereum_account: Account):
4947

5048

5149
@pytest.mark.asyncio
52-
async def test_fetch_aggregates(ethereum_account: Account):
50+
async def test_fetch_aggregates():
5351
mock_session = make_mock_session(
54-
ethereum_account, {"data": {"corechannel": {"nodes": [], "resource_nodes": []}}}
52+
{"data": {"corechannel": {"nodes": [], "resource_nodes": []}}}
5553
)
5654

5755
response = await fetch_aggregates(
@@ -62,10 +60,8 @@ async def test_fetch_aggregates(ethereum_account: Account):
6260

6361

6462
@pytest.mark.asyncio
65-
async def test_get_posts(ethereum_account: Account):
66-
async with UserSession(
67-
account=ethereum_account, api_server=settings.API_HOST
68-
) as session:
63+
async def test_get_posts():
64+
async with UserSession(api_server=settings.API_HOST) as session:
6965
response: MessagesResponse = await get_messages(
7066
session=session,
7167
pagination=2,
@@ -79,10 +75,8 @@ async def test_get_posts(ethereum_account: Account):
7975

8076

8177
@pytest.mark.asyncio
82-
async def test_get_messages(ethereum_account: Account):
83-
async with UserSession(
84-
account=ethereum_account, api_server=settings.API_HOST
85-
) as session:
78+
async def test_get_messages():
79+
async with UserSession(api_server=settings.API_HOST) as session:
8680
response: MessagesResponse = await get_messages(
8781
session=session,
8882
pagination=2,

tests/unit/test_synchronous_get.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
from aleph_client.conf import settings
44
from aleph_client.synchronous import get_messages
5-
from aleph_client.types import Account
65
from aleph_client.user_session import UserSession
76

87

9-
def test_get_posts(ethereum_account: Account):
10-
with UserSession(account=ethereum_account, api_server=settings.API_HOST) as session:
8+
def test_get_posts():
9+
with UserSession(api_server=settings.API_HOST) as session:
1110
response: MessagesResponse = get_messages(
1211
session=session,
1312
pagination=2,

0 commit comments

Comments
 (0)