-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support websockets API #1515
Support websockets API #1515
Conversation
…ub.com/software-mansion/starknet.py into franciszekjob/1498-3-websockets
…tware-mansion/starknet.py into franciszekjob/1498-2-get-compiled-casm
…ub.com/software-mansion/starknet.py into franciszekjob/1498-3-websockets
def get_block_identifier( | ||
block_hash: Optional[Union[Hash, Tag]] = None, | ||
block_number: Optional[Union[int, Tag]] = None, | ||
default_tag: Optional[Tag] = "pending", | ||
) -> dict: | ||
return { | ||
"block_id": _get_raw_block_identifier(block_hash, block_number, default_tag) | ||
} | ||
|
||
|
||
def _get_raw_block_identifier( | ||
block_hash: Optional[Union[Hash, Tag]] = None, | ||
block_number: Optional[Union[int, Tag]] = None, | ||
default_tag: Optional[Tag] = "pending", | ||
) -> Union[dict, Hash, Tag, None]: | ||
if block_hash is not None and block_number is not None: | ||
raise ValueError( | ||
"Arguments block_hash and block_number are mutually exclusive." | ||
) | ||
|
||
if block_hash in ("latest", "pending") or block_number in ("latest", "pending"): | ||
return block_hash or block_number | ||
|
||
if block_hash is not None: | ||
return {"block_hash": _to_rpc_felt(block_hash)} | ||
|
||
if block_number is not None: | ||
return {"block_number": block_number} | ||
|
||
return default_tag |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: Moved it to it doesn't sit in full node client's file. It could be theoretically placed in a file which is not client specific or we could rename this file to e.g. rpc_utils.py
.
@post_load | ||
def make_dataclass(self, data, **kwargs) -> BlockHeader: | ||
return BlockHeader(**data) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: It was missing.
def _handle_received_message(self, message: Union[str, bytes]): | ||
""" | ||
Handles the received message. | ||
|
||
:param message: The message received from the WebSocket server. | ||
""" | ||
data = cast(Dict, json.loads(message)) | ||
|
||
# case when the message is a response to `subscribe_{method}` | ||
if "id" in data and data["id"] in self._pending_responses: | ||
future = self._pending_responses.pop(data["id"]) | ||
future.set_result(data) | ||
|
||
# case when the message is a notification | ||
elif "method" in data: | ||
self._handle_notification(data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: Both subscription responses and notifications are handled via recv_streaming
(used in async iterator for the connection) because we cannot simultaneously use recv
for immediate responses and maintain a continuous listener with recv_streaming
. Consequently, all incoming messages are processed through the streaming interface.
Relates #1498
Introduced changes
Support websockets API
WebsocketClient