Skip to content

Commit c0dc282

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 c0dc282

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/aleph/sdk/client/vm_client.py

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

77
import aiohttp
8+
from aiohttp.client import _RequestContextManager
89
from aleph_message.models import Chain, ItemHash
910
from eth_account.messages import encode_defunct
1011
from jwcrypto import jwk
@@ -127,6 +128,32 @@ async def perform_operation(
127128
logger.error(f"HTTP error during operation {operation}: {str(e)}")
128129
return None, str(e)
129130

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

0 commit comments

Comments
 (0)