Skip to content

Commit b8ecf1a

Browse files
authored
DX-2849 Add webrtc_utils (#110)
* DX-2849 Add `webrtc_utils` Add WebRTC Utilities module with helper methods to generate transfer BXML * Made it fancier * Refactor function names
1 parent 0ddc183 commit b8ecf1a

File tree

4 files changed

+74
-14
lines changed

4 files changed

+74
-14
lines changed

bandwidth/utilities/init.py

Whitespace-only changes.

bandwidth/utilities/web_rtc.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from bandwidth.model.bxml.response import Response
2+
from bandwidth.model.bxml.verbs import *
3+
4+
5+
def _generate_transfer_model(device_token: str, voice_call_id: str, sip_uri: str = 'sip:sipx.webrtc.bandwidth.com:5060') -> Transfer:
6+
"""Generate a Transfer object for a WebRTC Session
7+
8+
Args:
9+
device_token (str): The device token.
10+
voice_call_id (str): The Bandwidth voice Call ID.
11+
sip_uri (str, optional): The SIP URI to transfer the call to. Defaults to 'sip:sipx.webrtc.bandwidth.com:5060'.
12+
13+
Returns:
14+
Transfer: Returns a Transfer BXML Verb Object
15+
"""
16+
uui = "".join(voice_call_id.split("-")[1::])
17+
sip_uri = SipUri(
18+
uui=f"{uui};encoding=base64,{device_token};encoding=jwt",
19+
uri=sip_uri
20+
)
21+
transfer = Transfer(
22+
sip_uris=[sip_uri]
23+
)
24+
return transfer
25+
26+
27+
def generate_transfer_bxml_verb(device_token: str, voice_call_id: str, sip_uri: str = 'sip:sipx.webrtc.bandwidth.com:5060') -> str:
28+
"""Returns the Transfer verb to perform the SIP transfer without the Response wrapper
29+
30+
Args:
31+
device_token (str): The device token.
32+
voice_call_id (str): The Bandwidth voice Call ID.
33+
sip_uri (str, optional): The SIP URI to transfer the call to. Defaults to 'sip:sipx.webrtc.bandwidth.com:5060'.
34+
35+
Returns:
36+
str: <Transfer> BXML Verb
37+
"""
38+
return _generate_transfer_model(device_token, voice_call_id, sip_uri).to_bxml()
39+
40+
41+
def generate_transfer_bxml(device_token: str, voice_call_id: str, sip_uri: str = 'sip:sipx.webrtc.bandwidth.com:5060') -> str:
42+
"""Generate BXML document with WebRTC a device token to perform a SIP transfer
43+
44+
Args:
45+
device_token (str): The device token.
46+
voice_call_id (str): The Bandwidth voice Call ID.
47+
sip_uri (str, optional): The SIP URI to transfer the call to. Defaults to 'sip:sipx.webrtc.bandwidth.com:5060'.
48+
49+
Returns:
50+
str: BXML document with transfer BXML
51+
"""
52+
response = Response()
53+
response.add_verb(_generate_transfer_model(device_token, voice_call_id, sip_uri))
54+
return response.to_bxml()

test/unit/test_bxml.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from bandwidth.model.bxml.response import Response
1111
from bandwidth.model.bxml.bxml import Bxml
1212
from bandwidth.model.bxml.verbs import *
13+
from bandwidth.utilities import web_rtc
1314

1415

1516
class TestBxml:
@@ -396,20 +397,6 @@ def test_bxml_speak_sentence_pause(self):
396397
bxml.add_verb(speak_sentence)
397398
bxml.add_verb(pause)
398399
assert bxml.to_bxml() == expected_bxml
399-
400-
@pytest.mark.skip(reason="This model is not yet implemented")
401-
def test_generate_transfer_bxml(self):
402-
expected = '<?xml version="1.0" encoding="UTF-8"?><Response><Transfer><SipUri uui="93d6f3c0be5845960b744fa28015d8ede84bd1a4;encoding=base64,asdf;encoding=jwt">sip:sipx.webrtc.bandwidth.com:5060</SipUri></Transfer></Response>'
403-
actual = generate_transfer_bxml(
404-
'asdf', 'c-93d6f3c0-be584596-0b74-4fa2-8015-d8ede84bd1a4')
405-
assert actual == expected
406-
407-
@pytest.mark.skip(reason="This model is not yet implemented")
408-
def test_generate_transfer_bxml_verb(self):
409-
expected = '<Transfer><SipUri uui="93d6f3c0be5845960b744fa28015d8ede84bd1a4;encoding=base64,asdf;encoding=jwt">sip:sipx.webrtc.bandwidth.com:5060</SipUri></Transfer>'
410-
actual = generate_transfer_bxml_verb(
411-
'asdf', 'c-93d6f3c0-be584596-0b74-4fa2-8015-d8ede84bd1a4')
412-
assert actual == expected
413400

414401
def test_start_stream_bxml_verb(self):
415402
expected = '<?xml version="1.0" encoding="UTF-8"?><Response><StartStream destination="https://www.test.com/stream" name="test_stream" tracks="inbound" streamEventUrl="https://www.test.com/event" streamEventMethod="POST" username="username" password="password"/></Response>'

test/unit/test_webrtc_utilities.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from bandwidth.utilities.web_rtc import *
2+
3+
4+
class TestWebRtcUtilities:
5+
"""
6+
Class for the WebRTC Utilities Tests
7+
"""
8+
9+
def test_generate_transfer_bxml(self):
10+
expected = '<Transfer><SipUri uui="93d6f3c0be5845960b744fa28015d8ede84bd1a4;encoding=base64,asdf;encoding=jwt">sip:sipx.webrtc.bandwidth.com:5060</SipUri></Transfer>'
11+
actual = generate_transfer_bxml_verb(
12+
'asdf', 'c-93d6f3c0-be584596-0b74-4fa2-8015-d8ede84bd1a4')
13+
assert actual == expected
14+
15+
def test_generate_transfer_bxml_document(self):
16+
expected = '<?xml version="1.0" encoding="UTF-8"?><Response><Transfer><SipUri uui="93d6f3c0be5845960b744fa28015d8ede84bd1a4;encoding=base64,asdf;encoding=jwt">sip:sipx.webrtc.bandwidth.com:5060</SipUri></Transfer></Response>'
17+
actual = generate_transfer_bxml(
18+
'asdf', 'c-93d6f3c0-be584596-0b74-4fa2-8015-d8ede84bd1a4')
19+
assert actual == expected

0 commit comments

Comments
 (0)