Skip to content

Commit 3baf228

Browse files
committed
Add VmClient.operate
Variation on perform_operation that allow a raw response. Move from the aleph-client code introduced in aleph-im/aleph-client#304
1 parent 63fec9a commit 3baf228

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/aleph/sdk/client/vm_client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from urllib.parse import urlparse
66

77
import aiohttp
8+
from aiohttp.client import _RequestContextManager
9+
810
from aleph_message.models import Chain, ItemHash
911
from eth_account.messages import encode_defunct
1012
from jwcrypto import jwk
@@ -127,6 +129,32 @@ async def perform_operation(
127129
logger.error(f"HTTP error during operation {operation}: {str(e)}")
128130
return None, str(e)
129131

132+
def operate(
133+
self, vm_id: ItemHash, operation: str, method: str = "POST"
134+
) -> _RequestContextManager:
135+
"""Request a CRN an operation for a VM (eg reboot, logs)
136+
137+
This operation is authenticated via the user wallet.
138+
Use as an async context manager.
139+
e.g `async with client.operate(vm_id=item_hash, operation="logs", method="GET") as response:`
140+
"""
141+
142+
async def authenticated_request():
143+
if not self.pubkey_signature_header:
144+
self.pubkey_signature_header = (
145+
await self._generate_pubkey_signature_header()
146+
)
147+
148+
url, header = await self._generate_header(
149+
vm_id=vm_id, operation=operation, method=method
150+
)
151+
resp = await self.session._request(
152+
method=method, str_or_url=url, headers=header
153+
)
154+
return resp
155+
156+
return _RequestContextManager(authenticated_request())
157+
130158
async def get_logs(self, vm_id: ItemHash) -> AsyncGenerator[str, None]:
131159
if not self.pubkey_signature_header:
132160
self.pubkey_signature_header = (

0 commit comments

Comments
 (0)