Skip to content

Commit 2029525

Browse files
authored
Add RTCIceCandidate (#1)
1 parent 268cad2 commit 2029525

File tree

6 files changed

+49
-69
lines changed

6 files changed

+49
-69
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ warn_unused_ignores = true
9898
"T201", # print found
9999
]
100100

101+
[tool.pylint]
102+
extension-pkg-whitelist = ["orjson"]
103+
101104
[tool.pylint.BASIC]
102105
good-names = [
103106
"_",

tests/__snapshots__/test_init.ambr

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
]),
66
})
77
# ---
8-
# name: test_decoding_and_encoding[RTCConfiguration-RTCConfiguration_empty.json][dict]
9-
dict({
10-
})
11-
# ---
128
# name: test_decoding_and_encoding[RTCConfiguration-RTCConfiguration_multiple_iceServers.json][dataclass]
139
dict({
1410
'ice_servers': list([
@@ -28,23 +24,6 @@
2824
]),
2925
})
3026
# ---
31-
# name: test_decoding_and_encoding[RTCConfiguration-RTCConfiguration_multiple_iceServers.json][dict]
32-
dict({
33-
'iceServers': list([
34-
dict({
35-
'urls': 'stun:stun.home-assisant.io:80',
36-
}),
37-
dict({
38-
'credential': 'credential',
39-
'urls': list([
40-
'stun:stun.home-assisant.io:80',
41-
'stun:stun.l.google.com:19302',
42-
]),
43-
'username': 'username',
44-
}),
45-
]),
46-
})
47-
# ---
4827
# name: test_decoding_and_encoding[RTCConfiguration-RTCConfiguration_one_iceServer.json][dataclass]
4928
dict({
5029
'ice_servers': list([
@@ -56,31 +35,24 @@
5635
]),
5736
})
5837
# ---
59-
# name: test_decoding_and_encoding[RTCConfiguration-RTCConfiguration_one_iceServer.json][dict]
38+
# name: test_decoding_and_encoding[RTCIceCandidate-RTCIceCandidate_candidate.json][dataclass]
6039
dict({
61-
'iceServers': list([
62-
dict({
63-
'urls': 'stun:stun.home-assisant.io:80',
64-
}),
65-
]),
40+
'candidate': '3932168448 1 udp 1694498815 1.2.3.4 10676 typ srflx raddr 0.0.0.0 rport 37566',
6641
})
6742
# ---
68-
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_only_urls_list.json][dataclass]
43+
# name: test_decoding_and_encoding[RTCIceCandidate-RTCIceCandidate_end.json][dataclass]
6944
dict({
70-
'credential': None,
71-
'urls': list([
72-
'stun:stun.home-assisant.io:80',
73-
'stun:stun.l.google.com:19302',
74-
]),
75-
'username': None,
45+
'candidate': '',
7646
})
7747
# ---
78-
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_only_urls_list.json][dict]
48+
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_only_urls_list.json][dataclass]
7949
dict({
50+
'credential': None,
8051
'urls': list([
8152
'stun:stun.home-assisant.io:80',
8253
'stun:stun.l.google.com:19302',
8354
]),
55+
'username': None,
8456
})
8557
# ---
8658
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_only_urls_string.json][dataclass]
@@ -90,11 +62,6 @@
9062
'username': None,
9163
})
9264
# ---
93-
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_only_urls_string.json][dict]
94-
dict({
95-
'urls': 'stun:stun.home-assisant.io:80',
96-
})
97-
# ---
9865
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_urls_list.json][dataclass]
9966
dict({
10067
'credential': 'credential',
@@ -105,27 +72,10 @@
10572
'username': 'username',
10673
})
10774
# ---
108-
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_urls_list.json][dict]
109-
dict({
110-
'credential': 'credential',
111-
'urls': list([
112-
'stun:stun.home-assisant.io:80',
113-
'stun:stun.l.google.com:19302',
114-
]),
115-
'username': 'username',
116-
})
117-
# ---
11875
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_urls_string.json][dataclass]
11976
dict({
12077
'credential': 'credential',
12178
'urls': 'stun:stun.home-assisant.io:80',
12279
'username': 'username',
12380
})
12481
# ---
125-
# name: test_decoding_and_encoding[RTCIceServer-RTCIceServer_urls_string.json][dict]
126-
dict({
127-
'credential': 'credential',
128-
'urls': 'stun:stun.home-assisant.io:80',
129-
'username': 'username',
130-
})
131-
# ---
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"candidate": "3932168448 1 udp 1694498815 1.2.3.4 10676 typ srflx raddr 0.0.0.0 rport 37566"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"candidate": ""
3+
}

tests/test_init.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
from __future__ import annotations
44

5-
import re
65
from typing import TYPE_CHECKING
76

7+
import orjson
88
import pytest
99

1010
from tests import load_fixture
11-
from webrtc_models import RTCConfiguration, RTCIceServer
11+
from webrtc_models import RTCConfiguration, RTCIceCandidate, RTCIceServer
1212

1313
if TYPE_CHECKING:
1414
from mashumaro.mixins.orjson import DataClassORJSONMixin
@@ -27,6 +27,9 @@
2727
(RTCConfiguration, "RTCConfiguration_empty.json"),
2828
(RTCConfiguration, "RTCConfiguration_one_iceServer.json"),
2929
(RTCConfiguration, "RTCConfiguration_multiple_iceServers.json"),
30+
# RTCIceCandidate
31+
(RTCIceCandidate, "RTCIceCandidate_end.json"),
32+
(RTCIceCandidate, "RTCIceCandidate_candidate.json"),
3033
],
3134
)
3235
def test_decoding_and_encoding(
@@ -35,14 +38,16 @@ def test_decoding_and_encoding(
3538
filename: str,
3639
) -> None:
3740
"""Test decoding/encoding."""
38-
# Json section
3941
file_content = load_fixture(filename)
40-
ice_server = clazz.from_json(file_content)
41-
assert ice_server == snapshot(name="dataclass")
42-
# replace spaces and newlines
43-
assert ice_server.to_json() == re.sub(r"\s", "", file_content)
44-
45-
# Dict section
46-
ice_server_dict = ice_server.to_dict()
47-
assert ice_server_dict == snapshot(name="dict")
48-
assert ice_server == clazz.from_dict(ice_server_dict)
42+
instance = clazz.from_json(file_content)
43+
assert instance == snapshot(name="dataclass")
44+
45+
file_content_dict = orjson.loads(file_content)
46+
instance_dict = instance.to_dict()
47+
48+
# Verify json
49+
assert instance.to_json() == orjson.dumps(file_content_dict).decode()
50+
51+
# Verify dict
52+
assert instance_dict == file_content_dict
53+
assert instance == clazz.from_dict(instance_dict)

webrtc_models/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
from mashumaro.config import BaseConfig
77
from mashumaro.mixins.orjson import DataClassORJSONMixin
88

9+
__all__ = [
10+
"RTCConfiguration",
11+
"RTCIceCandidate",
12+
"RTCIceServer",
13+
]
14+
915

1016
class _RTCBaseModel(DataClassORJSONMixin):
1117
"""Base class for RTC models."""
@@ -40,3 +46,13 @@ class RTCConfiguration(_RTCBaseModel):
4046
ice_servers: list[RTCIceServer] = field(
4147
metadata=field_options(alias="iceServers"), default_factory=list
4248
)
49+
50+
51+
@dataclass(frozen=True)
52+
class RTCIceCandidate(_RTCBaseModel):
53+
"""RTC Ice Candidate.
54+
55+
See https://www.w3.org/TR/webrtc/#rtcicecandidate-interface
56+
"""
57+
58+
candidate: str

0 commit comments

Comments
 (0)