|
| 1 | +from typing import TYPE_CHECKING, Optional, Tuple |
| 2 | + |
| 3 | +from aleph_message.models import AggregateMessage, ItemHash |
| 4 | +from aleph_message.status import MessageStatus |
| 5 | + |
| 6 | +from aleph.sdk.client.services.base import AggregateConfig |
| 7 | +from aleph.sdk.client.services.port_forwarder import PortForwarder |
| 8 | +from aleph.sdk.exceptions import MessageNotProcessed, NotAuthorize |
| 9 | +from aleph.sdk.types import AllForwarders, Ports |
| 10 | +from aleph.sdk.utils import safe_getattr |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from aleph.sdk.client.abstract import AuthenticatedAlephClient |
| 14 | + |
| 15 | + |
| 16 | +class AuthenticatedPortForwarder(PortForwarder): |
| 17 | + """ |
| 18 | + Authenticated Port Forwarder services with create and update capabilities |
| 19 | + """ |
| 20 | + |
| 21 | + def __init__(self, client: "AuthenticatedAlephClient"): |
| 22 | + super().__init__(client) |
| 23 | + |
| 24 | + async def _verify_status_processed_and_ownership( |
| 25 | + self, item_hash: ItemHash |
| 26 | + ) -> Tuple[AggregateMessage, MessageStatus]: |
| 27 | + """ |
| 28 | + Verify that the message is well processed (and not rejected / pending), |
| 29 | + This also verify the ownership of the message |
| 30 | + """ |
| 31 | + message: AggregateMessage |
| 32 | + status: MessageStatus |
| 33 | + message, status = await self._client.get_message( |
| 34 | + item_hash=item_hash, |
| 35 | + with_status=True, |
| 36 | + ) |
| 37 | + |
| 38 | + # We ensure message is not Rejected (Might not be processed yet) |
| 39 | + if status not in [MessageStatus.PROCESSED, MessageStatus.PENDING]: |
| 40 | + raise MessageNotProcessed(item_hash=item_hash, status=status) |
| 41 | + |
| 42 | + message_content = safe_getattr(message, "content") |
| 43 | + address = safe_getattr(message_content, "address") |
| 44 | + |
| 45 | + if ( |
| 46 | + not hasattr(self._client, "account") |
| 47 | + or address != self._client.account.get_address() |
| 48 | + ): |
| 49 | + current_address = ( |
| 50 | + self._client.account.get_address() |
| 51 | + if hasattr(self._client, "account") |
| 52 | + else "unknown" |
| 53 | + ) |
| 54 | + raise NotAuthorize( |
| 55 | + item_hash=item_hash, |
| 56 | + target_address=address, |
| 57 | + current_address=current_address, |
| 58 | + ) |
| 59 | + return message, status |
| 60 | + |
| 61 | + async def get_address_ports( |
| 62 | + self, address: Optional[str] = None |
| 63 | + ) -> AggregateConfig[AllForwarders]: |
| 64 | + """ |
| 65 | + Get all port forwarding configurations for an address |
| 66 | +
|
| 67 | + Args: |
| 68 | + address: The address to fetch configurations for. |
| 69 | + If None, uses the authenticated client's account address. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + Port forwarding configurations |
| 73 | + """ |
| 74 | + if address is None: |
| 75 | + if not hasattr(self._client, "account") or not self._client.account: |
| 76 | + raise ValueError("No account provided and client is not authenticated") |
| 77 | + address = self._client.account.get_address() |
| 78 | + |
| 79 | + return await super().get_address_ports(address=address) |
| 80 | + |
| 81 | + async def get_ports( |
| 82 | + self, item_hash: ItemHash = None, address: Optional[str] = None |
| 83 | + ) -> Optional[Ports]: |
| 84 | + """ |
| 85 | + Get port forwarding configuration for a specific item hash |
| 86 | +
|
| 87 | + Args: |
| 88 | + address: The address to fetch configurations for. |
| 89 | + If None, uses the authenticated client's account address. |
| 90 | + item_hash: The hash of the item to get configuration for |
| 91 | +
|
| 92 | + Returns: |
| 93 | + Port configuration if found, otherwise empty Ports object |
| 94 | + """ |
| 95 | + if address is None: |
| 96 | + if not hasattr(self._client, "account") or not self._client.account: |
| 97 | + raise ValueError("No account provided and client is not authenticated") |
| 98 | + address = self._client.account.get_address() |
| 99 | + |
| 100 | + if item_hash is None: |
| 101 | + raise ValueError("item_hash must be provided") |
| 102 | + |
| 103 | + return await super().get_ports(address=address, item_hash=item_hash) |
| 104 | + |
| 105 | + async def create_ports( |
| 106 | + self, item_hash: ItemHash, ports: Ports |
| 107 | + ) -> Tuple[AggregateMessage, MessageStatus]: |
| 108 | + """ |
| 109 | + Create a new port forwarding configuration for an item hash |
| 110 | +
|
| 111 | + Args: |
| 112 | + item_hash: The hash of the item (instance/program/IPFS website) |
| 113 | + ports: Dictionary mapping port numbers to PortFlags |
| 114 | +
|
| 115 | + Returns: |
| 116 | + Dictionary with the result of the operation |
| 117 | + """ |
| 118 | + if not hasattr(self._client, "account") or not self._client.account: |
| 119 | + raise ValueError("An account is required for this operation") |
| 120 | + |
| 121 | + # Pre Check |
| 122 | + # _, _ = await self._verify_status_processed_and_ownership(item_hash=item_hash) |
| 123 | + |
| 124 | + content = {str(item_hash): ports.model_dump()} |
| 125 | + |
| 126 | + # Check if create_aggregate exists on the client |
| 127 | + return await self._client.create_aggregate( # type: ignore |
| 128 | + key=self.aggregate_key, content=content |
| 129 | + ) |
| 130 | + |
| 131 | + async def update_ports( |
| 132 | + self, item_hash: ItemHash, ports: Ports |
| 133 | + ) -> Tuple[AggregateMessage, MessageStatus]: |
| 134 | + """ |
| 135 | + Update an existing port forwarding configuration for an item hash |
| 136 | +
|
| 137 | + Args: |
| 138 | + item_hash: The hash of the item (instance/program/IPFS website) |
| 139 | + ports: Dictionary mapping port numbers to PortFlags |
| 140 | +
|
| 141 | + Returns: |
| 142 | + Dictionary with the result of the operation |
| 143 | + """ |
| 144 | + if not hasattr(self._client, "account") or not self._client.account: |
| 145 | + raise ValueError("An account is required for this operation") |
| 146 | + |
| 147 | + # Pre Check |
| 148 | + # _, _ = await self._verify_status_processed_and_ownership(item_hash=item_hash) |
| 149 | + |
| 150 | + content = {} |
| 151 | + |
| 152 | + content[str(item_hash)] = ports.model_dump() |
| 153 | + |
| 154 | + message, status = await self._client.create_aggregate( # type: ignore |
| 155 | + key=self.aggregate_key, content=content |
| 156 | + ) |
| 157 | + |
| 158 | + return message, status |
| 159 | + |
| 160 | + async def delete_ports( |
| 161 | + self, item_hash: ItemHash |
| 162 | + ) -> Tuple[AggregateMessage, MessageStatus]: |
| 163 | + """ |
| 164 | + Delete port forwarding configuration for an item hash |
| 165 | +
|
| 166 | + Args: |
| 167 | + item_hash: The hash of the item (instance/program/IPFS website) to delete configuration for |
| 168 | +
|
| 169 | + Returns: |
| 170 | + Dictionary with the result of the operation |
| 171 | + """ |
| 172 | + if not hasattr(self._client, "account") or not self._client.account: |
| 173 | + raise ValueError("An account is required for this operation") |
| 174 | + |
| 175 | + # Pre Check |
| 176 | + # _, _ = await self._verify_status_processed_and_ownership(item_hash=item_hash) |
| 177 | + |
| 178 | + # Get the Port Config of the item_hash |
| 179 | + port: Optional[Ports] = await self.get_ports(item_hash=item_hash) |
| 180 | + if not port: |
| 181 | + raise |
| 182 | + |
| 183 | + content = {} |
| 184 | + content[str(item_hash)] = port.model_dump() |
| 185 | + |
| 186 | + # Create a new aggregate with the updated content |
| 187 | + message, status = await self._client.create_aggregate( # type: ignore |
| 188 | + key=self.aggregate_key, content=content |
| 189 | + ) |
| 190 | + return message, status |
0 commit comments