|
5 | 5 | from urllib.parse import urlparse |
6 | 6 |
|
7 | 7 | import aiohttp |
| 8 | +from aiohttp.client import _RequestContextManager |
| 9 | + |
8 | 10 | from aleph_message.models import Chain, ItemHash |
9 | 11 | from eth_account.messages import encode_defunct |
10 | 12 | from jwcrypto import jwk |
@@ -127,6 +129,32 @@ async def perform_operation( |
127 | 129 | logger.error(f"HTTP error during operation {operation}: {str(e)}") |
128 | 130 | return None, str(e) |
129 | 131 |
|
| 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 | + |
130 | 158 | async def get_logs(self, vm_id: ItemHash) -> AsyncGenerator[str, None]: |
131 | 159 | if not self.pubkey_signature_header: |
132 | 160 | self.pubkey_signature_header = ( |
|
0 commit comments