Skip to content

Commit ca0be0f

Browse files
authored
Merge pull request #295 from Bandwidth/VAPI-3161-add-refer-verb
VAPI-3161: Add <Refer> BXML verb support
2 parents d85f4ad + bb89c32 commit ca0be0f

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

bandwidth/models/bxml/verbs/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .phone_number import PhoneNumber
1212
from .play_audio import PlayAudio
1313
from .record import Record
14+
from .refer import Refer
1415
from .redirect import Redirect
1516
from .resume_recording import ResumeRecording
1617
from .ring import Ring
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
refer.py
3+
4+
Bandwidth's Refer BXML verb
5+
6+
@copyright Bandwidth INC
7+
"""
8+
from ..nestable_verb import NestableVerb
9+
from .sip_uri import SipUri
10+
11+
12+
class Refer(NestableVerb):
13+
14+
def __init__(
15+
self, sip_uri: SipUri=None,
16+
refer_complete_url: str=None, refer_complete_method: str=None,
17+
tag: str=None
18+
):
19+
"""Initialize a <Refer> verb
20+
21+
The <Refer> verb sends a SIP REFER to the remote endpoint, asking it
22+
to redirect the call to a new SIP URI. Unlike <Transfer>, a successful
23+
REFER terminates the call on Bandwidth's side: the remote endpoint
24+
redirects away from Bandwidth entirely. This is a SIP protocol
25+
property, not a Bandwidth design choice. As a result, BXML returned in
26+
response to the referComplete callback is only meaningful for failure
27+
handling - there is no live call to act on after success.
28+
29+
Args:
30+
sip_uri (SipUri): The SIP URI to refer the call to. Required.
31+
Exactly one <SipUri> child element is allowed. This is the
32+
same SipUri model used by <Transfer>.
33+
refer_complete_url (str, optional): URL to send the Refer Complete
34+
event to when the REFER flow finishes (success or failure).
35+
May be a relative URL. Defaults to None.
36+
refer_complete_method (str, optional): The HTTP method to use for
37+
the request to referCompleteUrl. GET or POST. Default value
38+
is POST. Defaults to None.
39+
tag (str, optional): A custom string that will be sent with this
40+
and all future callbacks unless overwritten by a future tag
41+
attribute or cleared. May be cleared by setting tag="". Max
42+
length 256 characters. Defaults to None.
43+
"""
44+
self.sip_uri = sip_uri
45+
self.refer_complete_url = refer_complete_url
46+
self.refer_complete_method = refer_complete_method
47+
self.tag = tag
48+
super().__init__(
49+
tag="Refer",
50+
nested_verbs=[sip_uri] if sip_uri is not None else []
51+
)
52+
53+
@property
54+
def _attributes(self):
55+
return {
56+
"referCompleteUrl": self.refer_complete_url,
57+
"referCompleteMethod": self.refer_complete_method,
58+
"tag": self.tag
59+
}
60+
61+
def set_sip_uri(self, sip_uri: SipUri) -> None:
62+
"""Set the SIP URI destination for this <Refer> verb.
63+
64+
Args:
65+
sip_uri (SipUri): The SIP URI to refer the call to.
66+
"""
67+
self.sip_uri = sip_uri
68+
self._nested_verbs = [sip_uri]

bandwidth/models/bxml/verbs/sip_uri.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def __init__(
1818
):
1919
"""Initialize a <SipUri> verb
2020
21+
This SipUri is shared between the <Transfer> and <Refer> verbs.
22+
2123
Args:
2224
uri (str): A SIP URI to transfer the call to (e.g. sip:user@server.com)
2325
uui (str, optional): he value of the User-To-User header to send within the initial INVITE. Must include the encoding parameter as specified in RFC 7433. Only base64 and jwt encoding are currently allowed. This value, including the encoding specifier, may not exceed 256 characters. Defaults to None.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
test_refer.py
3+
4+
Unit tests for the <Refer> BXML verb
5+
6+
@copyright Bandwidth Inc.
7+
"""
8+
import unittest
9+
10+
from bandwidth.models.bxml import Refer, SipUri, Verb, NestableVerb
11+
12+
13+
class TestRefer(unittest.TestCase):
14+
15+
def setUp(self):
16+
self.sip_uri = SipUri(uri="sip:alice@atlanta.example.com")
17+
self.refer = Refer(
18+
sip_uri=self.sip_uri,
19+
refer_complete_url="https://example.com/handleRefer",
20+
refer_complete_method="POST",
21+
tag="test"
22+
)
23+
24+
def test_instance(self):
25+
assert isinstance(self.refer, Refer)
26+
assert isinstance(self.refer, NestableVerb)
27+
assert isinstance(self.refer, Verb)
28+
29+
def test_to_bxml(self):
30+
expected = '<Refer referCompleteUrl="https://example.com/handleRefer" referCompleteMethod="POST" tag="test"><SipUri>sip:alice@atlanta.example.com</SipUri></Refer>'
31+
assert expected == self.refer.to_bxml()
32+
33+
def test_set_sip_uri(self):
34+
refer = Refer()
35+
refer.set_sip_uri(SipUri(uri="sip:bob@example.com"))
36+
expected = '<Refer><SipUri>sip:bob@example.com</SipUri></Refer>'
37+
assert expected == refer.to_bxml()

0 commit comments

Comments
 (0)