From dc47abb39d529306faf2ef6f7a15b9965116b4a6 Mon Sep 17 00:00:00 2001 From: Brian Date: Fri, 30 Sep 2022 09:54:51 -0400 Subject: [PATCH 1/4] DX-2896 Refactor Forward Verb --- bandwidth/model/bxml/verbs/forward.py | 69 +++++++++++++++++++++++++++ test/unit/bxml/test_forward.py | 35 ++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 bandwidth/model/bxml/verbs/forward.py create mode 100644 test/unit/bxml/test_forward.py diff --git a/bandwidth/model/bxml/verbs/forward.py b/bandwidth/model/bxml/verbs/forward.py new file mode 100644 index 00000000..c80095db --- /dev/null +++ b/bandwidth/model/bxml/verbs/forward.py @@ -0,0 +1,69 @@ +""" +forward.py + +Bandwidth's Forward BXML verb + +@copyright Bandwidth INC +""" +from ..verb import Verb + + +class Forward(Verb): + + def __init__( + self, to: str=None, from_: str=None, + call_timeout: str=None, diversion_treatment: str=None, + diversion_reason: str=None, uui: str=None + ): + """Initialize a verb + + Args: + to (str): The phone number destination of the call. + from_ (str, optional): The phone number that the recipient will receive the call from. + call_timeout (str, optional): The number of seconds to wait before timing out the call. + diversion_treatment (str, optional): Can be any of the following: + none: No diversion headers are sent on the outbound leg of the transferred call. + propagate: Copy the Diversion header from the inbound leg to the outbound leg. Ignored if there is no Diversion header present on the inbound leg. + stack: After propagating any Diversion header from the inbound leg to the outbound leg, stack on top another Diversion header based on the Request-URI of the inbound call. + + Defaults to none. If diversionTreatment is not specified, no diversion header will be included for the transfer even if one came with the inbound call. Defaults to None. + diversion_reason (str, optional): Can be any of the following values: + unknown + user-busy + no-answer + unavailable + unconditional + time-of-day + do-not-disturb + deflection + follow-me + out-of-service + away + + This parameter is considered only when diversionTreatment is set to stack. Defaults is unknown. + Defaults to None. + uui (str, optional): The value of the User-To-User header to send within the outbound INVITE when forwarding to a SIP URI. + 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. + """ + self.attributes = { + "to": to, + "from_": from_, + "callTimeout": call_timeout, + "diversionTreatment": diversion_treatment, + "diversionReason": diversion_reason, + "uui": uui, + } + + super().__init__(tag="Forward", content=None, attributes=self.attributes, nested_verbs=None) + + def add_verb(self, verb: Verb): + """Adding verbs is not allowed for + + Args: + verb (Verb): BXML verb + + Raises: + AttributeError: This method is not allowed for + """ + raise AttributeError('Adding verbs is not supported by ') diff --git a/test/unit/bxml/test_forward.py b/test/unit/bxml/test_forward.py new file mode 100644 index 00000000..ef31b079 --- /dev/null +++ b/test/unit/bxml/test_forward.py @@ -0,0 +1,35 @@ +""" +test_forward.py + +Unit tests for the BXML verb + +@copyright Bandwidth Inc. +""" +import os +import pytest +import unittest + +from bandwidth.model.bxml.verbs.forward import Forward + +class TestForward(unittest.TestCase): + + def setUp(self): + self.forward = Forward( + to="19195554321", + from_="19195554322", + call_timeout = "15", + diversion_treatment="propagate", + diversion_reason="away", + uui="93d6f3c0be5845960b744fa28015d8ede84bd1a4;encoding=base64,asdf;encoding=jwt" + ) + + def test_to_bxml(self): + if os.environ['PYTHON_VERSION'] == '3.7': + expected = '' + else: + expected = '' + assert(expected == self.forward.to_bxml()) + + def test_add_verb(self): + with pytest.raises(AttributeError): + self.forward.add_verb(self.test_verb) From ea545ae0f3d99dbe77e39b30d350d03df2a3418d Mon Sep 17 00:00:00 2001 From: Brian Date: Fri, 30 Sep 2022 15:54:27 -0400 Subject: [PATCH 2/4] Refactored to take into account new terminal verb --- bandwidth/model/bxml/verbs/__init__.py | 1 + bandwidth/model/bxml/verbs/forward.py | 23 ++++++----------------- test/unit/bxml/test_forward.py | 4 ++-- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/bandwidth/model/bxml/verbs/__init__.py b/bandwidth/model/bxml/verbs/__init__.py index 1466c782..60e525a3 100644 --- a/bandwidth/model/bxml/verbs/__init__.py +++ b/bandwidth/model/bxml/verbs/__init__.py @@ -1,4 +1,5 @@ from .bridge import Bridge +from .forward import Forward from .phone_number import PhoneNumber from .sip_uri import SipUri from .tag import Tag diff --git a/bandwidth/model/bxml/verbs/forward.py b/bandwidth/model/bxml/verbs/forward.py index c80095db..cac8b70a 100644 --- a/bandwidth/model/bxml/verbs/forward.py +++ b/bandwidth/model/bxml/verbs/forward.py @@ -5,10 +5,10 @@ @copyright Bandwidth INC """ -from ..verb import Verb +from ..terminal_verb import TerminalVerb -class Forward(Verb): +class Forward(TerminalVerb): def __init__( self, to: str=None, from_: str=None, @@ -49,21 +49,10 @@ def __init__( self.attributes = { "to": to, "from_": from_, - "callTimeout": call_timeout, - "diversionTreatment": diversion_treatment, - "diversionReason": diversion_reason, + "call_timeout": call_timeout, + "diversion_treatment": diversion_treatment, + "diversion_reason": diversion_reason, "uui": uui, } - super().__init__(tag="Forward", content=None, attributes=self.attributes, nested_verbs=None) - - def add_verb(self, verb: Verb): - """Adding verbs is not allowed for - - Args: - verb (Verb): BXML verb - - Raises: - AttributeError: This method is not allowed for - """ - raise AttributeError('Adding verbs is not supported by ') + super().__init__(tag="Forward", content=None, attributes=self.attributes) diff --git a/test/unit/bxml/test_forward.py b/test/unit/bxml/test_forward.py index ef31b079..60772edd 100644 --- a/test/unit/bxml/test_forward.py +++ b/test/unit/bxml/test_forward.py @@ -25,9 +25,9 @@ def setUp(self): def test_to_bxml(self): if os.environ['PYTHON_VERSION'] == '3.7': - expected = '' + expected = '' else: - expected = '' + expected = '' assert(expected == self.forward.to_bxml()) def test_add_verb(self): From 6ebc80dd8f8f9968447658c4c33ea0175480812c Mon Sep 17 00:00:00 2001 From: Brian Date: Mon, 3 Oct 2022 13:41:13 -0400 Subject: [PATCH 3/4] Refactored due to change in attribute constructor --- bandwidth/model/bxml/verbs/forward.py | 28 +++++++++++++++++---------- test/unit/bxml/test_forward.py | 3 +++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/bandwidth/model/bxml/verbs/forward.py b/bandwidth/model/bxml/verbs/forward.py index cac8b70a..e7f81d8d 100644 --- a/bandwidth/model/bxml/verbs/forward.py +++ b/bandwidth/model/bxml/verbs/forward.py @@ -46,13 +46,21 @@ def __init__( 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. """ - self.attributes = { - "to": to, - "from_": from_, - "call_timeout": call_timeout, - "diversion_treatment": diversion_treatment, - "diversion_reason": diversion_reason, - "uui": uui, - } - - super().__init__(tag="Forward", content=None, attributes=self.attributes) + self.to = to + self.from_ = from_ + self.call_timeout = call_timeout + self.diversion_treatment = diversion_treatment + self.diversion_reason = diversion_reason + self.uui = uui + + super().__init__(tag="Forward", content=None) + @property + def _attributes(self): + return { + "to": self.to, + "from_": self.from_, + "call_timeout": self.call_timeout, + "diversion_treatment": self.diversion_treatment, + "diversion_reason": self.diversion_reason, + "uui": self.uui, + } diff --git a/test/unit/bxml/test_forward.py b/test/unit/bxml/test_forward.py index 60772edd..41c9fa33 100644 --- a/test/unit/bxml/test_forward.py +++ b/test/unit/bxml/test_forward.py @@ -9,6 +9,7 @@ import pytest import unittest +from bandwidth.model.bxml.verb import Verb from bandwidth.model.bxml.verbs.forward import Forward class TestForward(unittest.TestCase): @@ -22,6 +23,8 @@ def setUp(self): diversion_reason="away", uui="93d6f3c0be5845960b744fa28015d8ede84bd1a4;encoding=base64,asdf;encoding=jwt" ) + self.test_verb = Verb(tag="test") + def test_to_bxml(self): if os.environ['PYTHON_VERSION'] == '3.7': From 16d457044894944d7908e1442f673f405bb0a112 Mon Sep 17 00:00:00 2001 From: Brian Date: Mon, 3 Oct 2022 15:05:04 -0400 Subject: [PATCH 4/4] Fixed casing --- bandwidth/model/bxml/verbs/forward.py | 6 +++--- test/unit/bxml/test_forward.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bandwidth/model/bxml/verbs/forward.py b/bandwidth/model/bxml/verbs/forward.py index e7f81d8d..7e6ff6f2 100644 --- a/bandwidth/model/bxml/verbs/forward.py +++ b/bandwidth/model/bxml/verbs/forward.py @@ -59,8 +59,8 @@ def _attributes(self): return { "to": self.to, "from_": self.from_, - "call_timeout": self.call_timeout, - "diversion_treatment": self.diversion_treatment, - "diversion_reason": self.diversion_reason, + "callTimeout": self.call_timeout, + "diversionTreatment": self.diversion_treatment, + "diversionReason": self.diversion_reason, "uui": self.uui, } diff --git a/test/unit/bxml/test_forward.py b/test/unit/bxml/test_forward.py index 41c9fa33..b6409e1f 100644 --- a/test/unit/bxml/test_forward.py +++ b/test/unit/bxml/test_forward.py @@ -28,9 +28,9 @@ def setUp(self): def test_to_bxml(self): if os.environ['PYTHON_VERSION'] == '3.7': - expected = '' + expected = '' else: - expected = '' + expected = '' assert(expected == self.forward.to_bxml()) def test_add_verb(self):