Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Resch <[email protected]>
  • Loading branch information
sdb9696 and edenhaus authored Nov 13, 2024
1 parent acd81f0 commit dcd8042
Showing 1 changed file with 20 additions and 132 deletions.
152 changes: 20 additions & 132 deletions webrtc_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,157 +51,45 @@ class RTCConfiguration(_RTCBaseModel):


@dataclass(frozen=True)
class RTCIceCandidateInit(_RTCBaseModel):
"""RTC Ice Candidate Init.
class RTCIceCandidate(_RTCBaseModel):
"""RTC Ice Candidate.
See https://www.w3.org/TR/webrtc/#rtcicecandidate-interface
"""

candidate: str
sdp_m_line_index: int | None = field(
metadata=field_options(alias="sdpMLineIndex"), default=None, kw_only=True
)
sdp_mid: str | None = field(
metadata=field_options(alias="sdpMid"), default=None, kw_only=True
)
user_fragment: str | None = field(
metadata=field_options(alias="userFragment"), default=None, kw_only=True
)

def __post_init__(self) -> None:
"""Initialize class."""
msg = ("Using RTCIceCandidate is deprecated. Use "
"RTCIceCandidateInit instead")
warn(msg, DeprecationWarning, stacklevel=2)

@dataclass(frozen=True)
class RTCIceCandidate(_RTCBaseModel):
"""RTC Ice Candidate.
class RTCIceCandidateInit(RTCIceCandidate):
"""RTC Ice Candidate Init.
See https://www.w3.org/TR/webrtc/#rtcicecandidate-interface
See https://www.w3.org/TR/webrtc/#dom-rtcicecandidateinit
"""

class Config(BaseConfig):
"""Mashumaro config."""

# Serialize to spec conform names and omit default values
omit_default = False
serialize_by_alias = True

candidate_init: RTCIceCandidateInit | str | None = field(
default=None, metadata=field_options(serialize="omit")
candidate: str
sdp_mid: str | None = field(
metadata=field_options(alias="sdpMid"), default=None, kw_only=True
)

candidate: str | None = None

sdp_m_line_index: int | None = field(
metadata=field_options(alias="sdpMLineIndex"), default=None, init=False
)
sdp_mid: str | None = field(
metadata=field_options(alias="sdpMid"), default=None, init=False
metadata=field_options(alias="sdpMLineIndex"), default=None, kw_only=True
)
user_fragment: str | None = field(
metadata=field_options(alias="userFragment"), default=None, init=False
)

foundation: str | None = field(
default=None, init=False, metadata=field_options(serialize="omit")
)
component: str | None = field(
default=None, init=False, metadata=field_options(serialize="omit")
)
priority: int | None = field(
default=None, init=False, metadata=field_options(serialize="omit")
)
address: str | None = field(
default=None, init=False, metadata=field_options(serialize="omit")
)
protocol: str | None = field(
default=None, init=False, metadata=field_options(serialize="omit")
)
port: int | None = field(
default=None, init=False, metadata=field_options(serialize="omit")
)
type: str | None = field(
default=None, init=False, metadata=field_options(serialize="omit")
)
tcp_type: str | None = field(
metadata=field_options(alias="tcpType", serialize="omit"),
default=None,
init=False,
)
related_address: str | None = field(
metadata=field_options(alias="relatedAddress", serialize="omit"),
default=None,
init=False,
)
related_port: str | None = field(
metadata=field_options(alias="relatedPort", serialize="omit"),
default=None,
init=False,
)

relay_protocol: str | None = field(
metadata=field_options(alias="relayProtocol", serialize="omit"),
default=None,
init=False,
)
url: str | None = field(
default=None, init=False, metadata=field_options(serialize="omit")
metadata=field_options(alias="userFragment"), default=None, kw_only=True
)

@classmethod
def __pre_deserialize__(cls, d: dict[Any, Any]) -> dict[Any, Any]:
"""Per spec candidate serializes and deserialises to/from RTCIceCAndidateInit.
To invoke the toJSON() operation of the RTCIceCandidate interface,
run the following steps:
1. Let json be a new RTCIceCandidateInit dictionary.
2. For each attribute identifier attr in candidate, sdpMid,
sdpMLineIndex, usernameFragment:
3. Let value be the result of getting the underlying value of the attribute
identified by attr, given this RTCIceCandidate object.
4. Set json[attr] to value.
5. Return json.
This method reverses that logic.
"""
candidate_init = RTCIceCandidateInit.from_dict(d)
return {"candidate_init": candidate_init.to_dict()}

def __post_init__(self) -> None:
"""Initialize the class.
"""Initialize class.
Spec compliance: If both the sdpMid and sdpMLineIndex members of
candidateInitDict are null, throw a TypeError.
"""
if (candidate := self.candidate) or (
isinstance(self.candidate_init, str) and (candidate := self.candidate_init)
):
msg = (
"Passing a candidate str to RTCIceCandidate is deprecated, "
"pass an RTCIceCandidateInit object"
)
warn(msg, DeprecationWarning, stacklevel=2)
object.__setattr__(
self,
"candidate_init",
RTCIceCandidateInit(candidate, sdp_mid="0"),
)

if TYPE_CHECKING:
assert isinstance(self.candidate_init, RTCIceCandidateInit)

ric_init = self.candidate_init
if (
ric_init
and ric_init.candidate != ""
and ric_init.sdp_m_line_index is None
and ric_init.sdp_mid is None
):
msg = "sdp_m_line_index and sdp_mid cannot both be null."
raise TypeError(msg)

if ric_init:
# Attributes are readonly so set them via __setattr__
object.__setattr__(self, "candidate", ric_init.candidate)
object.__setattr__(self, "sdp_mid", ric_init.sdp_mid)
object.__setattr__(self, "sdp_m_line_index", ric_init.sdp_m_line_index)
object.__setattr__(self, "user_fragment", ric_init.user_fragment)
else:
object.__setattr__(self, "candidate", "")

if self.sdp_mid is None and self.sdp_m_line_index is None:
raise TypeError("sdpMid or sdpMLineIndex must be set")

0 comments on commit dcd8042

Please sign in to comment.