Skip to content

Commit

Permalink
Add VmClient.operate (#194)
Browse files Browse the repository at this point in the history
Variation on perform_operation that allow a raw response.
Move from the aleph-client code introduced in aleph-im/aleph-client#304
  • Loading branch information
olethanh authored Dec 13, 2024
1 parent 63fec9a commit 1e2eebe
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/aleph/sdk/client/vm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from urllib.parse import urlparse

import aiohttp
from aiohttp.client import _RequestContextManager
from aleph_message.models import Chain, ItemHash
from eth_account.messages import encode_defunct
from jwcrypto import jwk
Expand Down Expand Up @@ -127,6 +128,32 @@ async def perform_operation(
logger.error(f"HTTP error during operation {operation}: {str(e)}")
return None, str(e)

def operate(
self, vm_id: ItemHash, operation: str, method: str = "POST"
) -> _RequestContextManager:
"""Request a CRN an operation for a VM (eg reboot, logs)
This operation is authenticated via the user wallet.
Use as an async context manager.
e.g `async with client.operate(vm_id=item_hash, operation="logs", method="GET") as response:`
"""

async def authenticated_request():
if not self.pubkey_signature_header:
self.pubkey_signature_header = (
await self._generate_pubkey_signature_header()
)

url, header = await self._generate_header(
vm_id=vm_id, operation=operation, method=method
)
resp = await self.session._request(
method=method, str_or_url=url, headers=header
)
return resp

return _RequestContextManager(authenticated_request())

async def get_logs(self, vm_id: ItemHash) -> AsyncGenerator[str, None]:
if not self.pubkey_signature_header:
self.pubkey_signature_header = (
Expand Down

0 comments on commit 1e2eebe

Please sign in to comment.