Skip to content

Commit

Permalink
Add RTCIceCandidate (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Nov 1, 2024
1 parent 268cad2 commit 2029525
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 69 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ warn_unused_ignores = true
"T201", # print found
]

[tool.pylint]
extension-pkg-whitelist = ["orjson"]

[tool.pylint.BASIC]
good-names = [
"_",
Expand Down
64 changes: 7 additions & 57 deletions tests/__snapshots__/test_init.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
]),
})
# ---
# name: test_decoding_and_encoding[RTCConfiguration-RTCConfiguration_empty.json][dict]
dict({
})
# ---
# name: test_decoding_and_encoding[RTCConfiguration-RTCConfiguration_multiple_iceServers.json][dataclass]
dict({
'ice_servers': list([
Expand All @@ -28,23 +24,6 @@
]),
})
# ---
# name: test_decoding_and_encoding[RTCConfiguration-RTCConfiguration_multiple_iceServers.json][dict]
dict({
'iceServers': list([
dict({
'urls': 'stun:stun.home-assisant.io:80',
}),
dict({
'credential': 'credential',
'urls': list([
'stun:stun.home-assisant.io:80',
'stun:stun.l.google.com:19302',
]),
'username': 'username',
}),
]),
})
# ---
# name: test_decoding_and_encoding[RTCConfiguration-RTCConfiguration_one_iceServer.json][dataclass]
dict({
'ice_servers': list([
Expand All @@ -56,31 +35,24 @@
]),
})
# ---
# name: test_decoding_and_encoding[RTCConfiguration-RTCConfiguration_one_iceServer.json][dict]
# name: test_decoding_and_encoding[RTCIceCandidate-RTCIceCandidate_candidate.json][dataclass]
dict({
'iceServers': list([
dict({
'urls': 'stun:stun.home-assisant.io:80',
}),
]),
'candidate': '3932168448 1 udp 1694498815 1.2.3.4 10676 typ srflx raddr 0.0.0.0 rport 37566',
})
# ---
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_only_urls_list.json][dataclass]
# name: test_decoding_and_encoding[RTCIceCandidate-RTCIceCandidate_end.json][dataclass]
dict({
'credential': None,
'urls': list([
'stun:stun.home-assisant.io:80',
'stun:stun.l.google.com:19302',
]),
'username': None,
'candidate': '',
})
# ---
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_only_urls_list.json][dict]
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_only_urls_list.json][dataclass]
dict({
'credential': None,
'urls': list([
'stun:stun.home-assisant.io:80',
'stun:stun.l.google.com:19302',
]),
'username': None,
})
# ---
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_only_urls_string.json][dataclass]
Expand All @@ -90,11 +62,6 @@
'username': None,
})
# ---
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_only_urls_string.json][dict]
dict({
'urls': 'stun:stun.home-assisant.io:80',
})
# ---
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_urls_list.json][dataclass]
dict({
'credential': 'credential',
Expand All @@ -105,27 +72,10 @@
'username': 'username',
})
# ---
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_urls_list.json][dict]
dict({
'credential': 'credential',
'urls': list([
'stun:stun.home-assisant.io:80',
'stun:stun.l.google.com:19302',
]),
'username': 'username',
})
# ---
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_urls_string.json][dataclass]
dict({
'credential': 'credential',
'urls': 'stun:stun.home-assisant.io:80',
'username': 'username',
})
# ---
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_urls_string.json][dict]
dict({
'credential': 'credential',
'urls': 'stun:stun.home-assisant.io:80',
'username': 'username',
})
# ---
3 changes: 3 additions & 0 deletions tests/fixtures/RTCIceCandidate_candidate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"candidate": "3932168448 1 udp 1694498815 1.2.3.4 10676 typ srflx raddr 0.0.0.0 rport 37566"
}
3 changes: 3 additions & 0 deletions tests/fixtures/RTCIceCandidate_end.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"candidate": ""
}
29 changes: 17 additions & 12 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from __future__ import annotations

import re
from typing import TYPE_CHECKING

import orjson
import pytest

from tests import load_fixture
from webrtc_models import RTCConfiguration, RTCIceServer
from webrtc_models import RTCConfiguration, RTCIceCandidate, RTCIceServer

if TYPE_CHECKING:
from mashumaro.mixins.orjson import DataClassORJSONMixin
Expand All @@ -27,6 +27,9 @@
(RTCConfiguration, "RTCConfiguration_empty.json"),
(RTCConfiguration, "RTCConfiguration_one_iceServer.json"),
(RTCConfiguration, "RTCConfiguration_multiple_iceServers.json"),
# RTCIceCandidate
(RTCIceCandidate, "RTCIceCandidate_end.json"),
(RTCIceCandidate, "RTCIceCandidate_candidate.json"),
],
)
def test_decoding_and_encoding(
Expand All @@ -35,14 +38,16 @@ def test_decoding_and_encoding(
filename: str,
) -> None:
"""Test decoding/encoding."""
# Json section
file_content = load_fixture(filename)
ice_server = clazz.from_json(file_content)
assert ice_server == snapshot(name="dataclass")
# replace spaces and newlines
assert ice_server.to_json() == re.sub(r"\s", "", file_content)

# Dict section
ice_server_dict = ice_server.to_dict()
assert ice_server_dict == snapshot(name="dict")
assert ice_server == clazz.from_dict(ice_server_dict)
instance = clazz.from_json(file_content)
assert instance == snapshot(name="dataclass")

file_content_dict = orjson.loads(file_content)
instance_dict = instance.to_dict()

# Verify json
assert instance.to_json() == orjson.dumps(file_content_dict).decode()

# Verify dict
assert instance_dict == file_content_dict
assert instance == clazz.from_dict(instance_dict)
16 changes: 16 additions & 0 deletions webrtc_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
from mashumaro.config import BaseConfig
from mashumaro.mixins.orjson import DataClassORJSONMixin

__all__ = [
"RTCConfiguration",
"RTCIceCandidate",
"RTCIceServer",
]


class _RTCBaseModel(DataClassORJSONMixin):
"""Base class for RTC models."""
Expand Down Expand Up @@ -40,3 +46,13 @@ class RTCConfiguration(_RTCBaseModel):
ice_servers: list[RTCIceServer] = field(
metadata=field_options(alias="iceServers"), default_factory=list
)


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

candidate: str

0 comments on commit 2029525

Please sign in to comment.