Skip to content

Commit d8f62f1

Browse files
committed
feat: Add bidirectional flag
1 parent 1ad6e17 commit d8f62f1

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

opentok/opentok.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ def connect_audio_to_websocket(
19931993
String 'uri': A publicly reachable WebSocket URI to be used for the destination of the audio stream (such as "wss://example.com/ws-endpoint").
19941994
List 'streams' Optional: A list of stream IDs for the OpenTok streams you want to include in the WebSocket audio. If you omit this property, all streams in the session will be included.
19951995
Dictionary 'headers' Optional: An object of key-value pairs of headers to be sent to your WebSocket server with each message, with a maximum length of 512 bytes.
1996+
Boolean 'bidirectional' Optional: If true, enables bidirectional audio streaming over the WebSocket connection.
19961997
"""
19971998
self.validate_websocket_options(websocket_options)
19981999

@@ -2044,6 +2045,10 @@ def validate_websocket_options(self, options):
20442045
if "uri" not in options:
20452046
raise InvalidWebSocketOptionsError("Provide a WebSocket URI.")
20462047

2048+
if "bidirectional" in options:
2049+
if not isinstance(options["bidirectional"], bool):
2050+
raise InvalidWebSocketOptionsError("'bidirectional' must be a boolean if provided.")
2051+
20472052
def start_captions(
20482053
self,
20492054
session_id: str,

opentok/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# see: http://legacy.python.org/dev/peps/pep-0440/#public-version-identifiers
2-
__version__ = "3.11.1"
2+
__version__ = "3.12.0"
33

opentok/websocket_audio_connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class WebSocketAudioConnection:
77
def __init__(self, kwargs):
88
self.id = kwargs.get("id")
99
self.connectionId = kwargs.get("connectionId")
10+
self.bidirectional = kwargs.get("bidirectional")
1011

1112
def json(self):
1213
"""Returns a JSON representation of the WebSocket audio connection information."""

tests/test_audio_connector.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,38 @@
1212

1313

1414
class OpenTokAudioConnectorLiteTest(unittest.TestCase):
15+
@httpretty.activate
16+
def test_connect_audio_to_websocket_with_bidirectional(self):
17+
httpretty.register_uri(
18+
httpretty.POST,
19+
u(f"https://api.opentok.com/v2/project/{self.api_key}/connect"),
20+
body=self.response_body,
21+
status=200,
22+
content_type=u("application/json"),
23+
)
24+
25+
websocket_options = {"uri": "wss://service.com/ws-endpoint", "bidirectional": True}
26+
27+
websocket_audio_connection = self.opentok.connect_audio_to_websocket(
28+
self.session_id, self.token, websocket_options
29+
)
30+
31+
validate_jwt_header(self, httpretty.last_request().headers[u("x-opentok-auth")])
32+
expect(httpretty.last_request().headers[u("user-agent")]).to(contain(u("OpenTok-Python-SDK/") + __version__))
33+
expect(httpretty.last_request().headers[u("content-type")]).to(equal(u("application/json")))
34+
if PY2:
35+
body = json.loads(httpretty.last_request().body)
36+
if PY3:
37+
body = json.loads(httpretty.last_request().body.decode("utf-8"))
38+
39+
expect(body).to(have_key(u("token")))
40+
expect(body["websocket"]).to(have_key("bidirectional"))
41+
expect(body["websocket"]["bidirectional"]).to(be_true)
42+
expect(websocket_audio_connection).to(be_a(WebSocketAudioConnection))
43+
expect(websocket_audio_connection).to(have_property(u("id"), u("b0a5a8c7-dc38-459f-a48d-a7f2008da853")))
44+
expect(websocket_audio_connection).to(have_property(u("connectionId"), u("e9f8c166-6c67-440d-994a-04fb6dfed007")))
45+
# The bidirectional property should be present (None if not in response, True if in response)
46+
expect(hasattr(websocket_audio_connection, "bidirectional")).to(be_true)
1547
def setUp(self):
1648
self.api_key = u("123456")
1749
self.api_secret = u("1234567890abcdef1234567890abcdef1234567890")

0 commit comments

Comments
 (0)