Cleanup connection send_message from earlier refactoring (#488)

This commit is contained in:
J. Nick Koston 2023-07-18 13:15:56 -05:00 committed by GitHub
parent 7817674cd1
commit 7b215038f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@ import time
from dataclasses import astuple, dataclass from dataclasses import astuple, dataclass
from functools import partial from functools import partial
from typing import ( from typing import (
TYPE_CHECKING,
Any, Any,
Callable, Callable,
Coroutine, Coroutine,
@ -539,20 +540,20 @@ class APIConnection:
f"Connection isn't established yet ({self._connection_state})" f"Connection isn't established yet ({self._connection_state})"
) )
frame_helper = self._frame_helper message_type = PROTO_TO_MESSAGE_TYPE.get(type(msg))
assert frame_helper is not None
_msg_type = type(msg)
message_type = PROTO_TO_MESSAGE_TYPE.get(_msg_type)
if not message_type: if not message_type:
raise ValueError(f"Message type id not found for type {_msg_type}") raise ValueError(f"Message type id not found for type {type(msg)}")
encoded = msg.SerializeToString()
if self._debug_enabled(): if self._debug_enabled():
_LOGGER.debug("%s: Sending %s: %s", self.log_name, _msg_type.__name__, msg) _LOGGER.debug("%s: Sending %s: %s", self.log_name, type(msg).__name__, msg)
if TYPE_CHECKING:
assert self._frame_helper is not None
encoded = msg.SerializeToString()
try: try:
frame_helper.write_packet(message_type, encoded) self._frame_helper.write_packet(message_type, encoded)
except SocketAPIError as err: # pylint: disable=broad-except except SocketAPIError as err:
# If writing packet fails, we don't know what state the frames # If writing packet fails, we don't know what state the frames
# are in anymore and we have to close the connection # are in anymore and we have to close the connection
_LOGGER.info("%s: Error writing packet: %s", self.log_name, err) _LOGGER.info("%s: Error writing packet: %s", self.log_name, err)