This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Refer verb in python sdk (#17)
- Loading branch information
1 parent
fcff67c
commit 728254e
Showing
7 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from zang.inboundxml import Response, Refer, Sip | ||
|
||
from zang.domain.enums.http_method import HttpMethod | ||
|
||
refer = Refer( | ||
action="https://example.com/actionURL", | ||
method=HttpMethod.POST, | ||
timeout=180, | ||
callbackUrl="https://example.com/callbackURL", | ||
callbackMethod=HttpMethod.POST, | ||
) | ||
|
||
sip = Sip("username@example.com", | ||
username="username", | ||
password="pass",) | ||
|
||
refer.addElement(sip) | ||
|
||
response = Response() | ||
response.addElement(refer) | ||
|
||
print(response.xml) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import unittest | ||
from zang.inboundxml.elements.refer import Refer | ||
from zang.domain.enums.http_method import HttpMethod | ||
from zang.inboundxml.elements.sip import Sip | ||
|
||
|
||
class TestRefer(unittest.TestCase): | ||
|
||
def test_init_with_required_values(self): | ||
expected = '<Refer></Refer>' | ||
assert Refer().xml == expected | ||
|
||
def test_init_with_arguments(self): | ||
text = 'sip:username@example.com' | ||
refer = Refer(address=text) | ||
expected = '<Refer>%s</Refer>' % text | ||
assert refer.xml == expected | ||
|
||
def test_init_add_element(self): | ||
text = 'username@example.com' | ||
sip = Sip(text) | ||
refer = Refer() | ||
refer.addElement(sip) | ||
expected = '<Refer><Sip>%s</Sip></Refer>' % text | ||
assert refer.xml == expected | ||
|
||
def test_init_remove_element_at_index(self): | ||
text = 'Hello from Avaya CPaaS!' | ||
sip = Sip(text) | ||
refer = Refer() | ||
refer.addElement(sip) | ||
expected = '<Refer><Sip>%s</Sip></Refer>' % text | ||
assert refer.xml == expected | ||
refer.removeElementAtIndex(0) | ||
expected = '<Refer></Refer>' | ||
assert refer.xml == expected | ||
|
||
def test_remove_element_at_out_of_range_index(self): | ||
text = 'Hello from Avaya CPaaS!' | ||
sip = Sip(text) | ||
refer = Refer() | ||
refer.addElement(sip) | ||
index = len(refer._content) | ||
self.assertRaises( | ||
IndexError, lambda: refer.removeElementAtIndex(index)) | ||
|
||
def test_init_with_optional_attributes(self): | ||
method = HttpMethod.GET | ||
refer = Refer(method=method) | ||
expected = '<Refer method="%s"></Refer>' % (method.value) | ||
assert refer.xml == expected | ||
|
||
def test_init_with_unsupported_attributes(self): | ||
self.assertRaises(TypeError, lambda: Refer(foo='bar')) | ||
|
||
def test_with_update_attributes(self): | ||
refer = Refer() | ||
timeout = 0 | ||
refer.timeout = 0 | ||
expected = '<Refer timeout="%s"></Refer>' % (timeout) | ||
assert refer.xml == expected | ||
|
||
def test_udefinded_method_with_primitive_type(self): | ||
self.assertRaises(TypeError, lambda: Refer().addElement(0.5)) | ||
|
||
def test_udefinded_method_with_base_node(self): | ||
self.assertRaises(AttributeError, lambda: Refer().url) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
zang.inboundxml.elements.refer | ||
~~~~~~~~~~~~~~~~~~~ | ||
Module containing `Refer` inbound xml element | ||
""" | ||
|
||
from zang.inboundxml.elements.base_node import BaseNode | ||
from zang.inboundxml.elements.sip import Sip | ||
|
||
import sys | ||
if sys.version_info > (3, 0): | ||
str_classes = (str, bytes) | ||
|
||
|
||
class Refer(BaseNode): | ||
|
||
_allowedContentClass = ( | ||
str, | ||
int, | ||
Sip, | ||
) | ||
|
||
def __init__( | ||
self, | ||
address=None, | ||
action=None, | ||
method=None, | ||
timeout=None, | ||
callbackUrl=None, | ||
callbackMethod=None,): | ||
self._value = address | ||
self.action = action | ||
self.method = method | ||
self.timeout = timeout | ||
self.callbackUrl = callbackUrl | ||
self.callbackMethod = callbackMethod | ||
|
||
self._content = [] | ||
|
||
@property | ||
def address(self): | ||
return self._value | ||
|
||
@address.setter | ||
def address(self, value): | ||
if value is None: | ||
raise TypeError | ||
self._value = value | ||
|
||
@property | ||
def elements(self): | ||
return self._content | ||
|
||
def addElement(self, element): | ||
if isinstance(element, type(self)._allowedContentClass): | ||
self._content.append(element) | ||
else: | ||
raise TypeError('Element not allowed for content model') | ||
|
||
def removeElementAtIndex(self, index): | ||
del self._content[index] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters