8000 Support nesting TwiML verbs via `with` by dougblack · Pull Request #366 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

Support nesting TwiML verbs via with #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Jul 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion tests/unit/twiml/test_messaging_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ def test_response(self):
)

def test_response_chain(self):
r = MessagingResponse().message('Hello').redirect(url='example.com')
with MessagingResponse() as r:
r.message('Hello')
r.redirect(url='example.com')

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Message>Hello</Message><Redirect>example.com</Redirect></Response>'
)

def test_nested_verbs(self):
with MessagingResponse() as r:
with r.message('Hello') as m:
m.media('example.com')

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Message>Hello<Media>example.com</Media></Message></Response>'
)


class TestMessage(TwilioTest):

Expand Down
23 changes: 18 additions & 5 deletions tests/unit/twiml/test_voice_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,30 @@ def test_response(self):
)

def test_response_chain(self):
r = VoiceResponse().hangup().leave().sms(
'twilio sms',
to='+11234567890',
from_='+10987654321'
)
with VoiceResponse() as r:
r.hangup()
r.leave()
r.sms(
'twilio sms',
to='+11234567890',
from_='+10987654321'
)

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Hangup /><Leave /><Sms from="+10987654321" to="+11234567890">twilio sms</Sms></Response>'
)

def test_nested_verbs(self):
with VoiceResponse() as r:
with r.gather() as g:
g.say('Hello', voice='man')

assert_equal(
self.strip(r),
'<?xml version="1.0" encoding="UTF-8"?><Response><Gather><Say voice="man">Hello</Say></Gather></Response>'
)


class TestSay(TwilioTest):

Expand Down
13 changes: 12 additions & 1 deletion twilio/twiml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,25 @@ def append(self, verb):
"""
Add a TwiML doc
:param verb: TwiML Document
:return:
:return: self
"""
if not isinstance(verb, TwiML):
raise TwiMLException('Only appending of TwiML is allowed')

self.verbs.append(verb)
return self

def nest(self, verb):
"""
Add a TwiML doc. Unlike `append()`, this returns the created verb.
:param verb: TwiML verb
:return: the TwiML verb
"""
if not isinstance(verb, TwiML):
raise TwiMLException('Only nesting of TwiML is allowed')
self.verbs.append(verb)
return verb

def xml(self):
"""
Convert to XML
Expand Down
8 changes: 4 additions & 4 deletions twilio/twiml/messaging_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def message(self,
:param kwargs: other attributes
:return: <Response> element
"""
return self.append(Message(
return self.nest(Message(
body=body,
to=to,
from_=from_,
Expand All @@ -51,7 +51,7 @@ def redirect(self, url, method=None, **kwargs):
:param kwargs: other attributes
:return: <Response> element
"""
return self.append(Redirect(
return self.nest(Redirect(
method=method,
url=url,
**kwargs
Expand Down Expand Up @@ -80,7 +80,7 @@ def body(self, body):
:param body: body of message
:return: <Message> element
"""
return self.append(Body(body))
return self.nest(Body(body))

def media(self, url):
"""
Expand All @@ -89,7 +89,7 @@ def media(self, url):
:param url: media URL
:return: <Message> element
"""
return self.append(Media(url))
return self.nest(Media(url))


class Body(TwiML):
Expand Down
46 changes: 23 additions & 23 deletions twilio/twiml/voice_response.py
8000 78A5
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def dial(self,
:param kwargs: additional attributes
:return: <Response> element
"""
return self.append(Dial(
return self.nest(Dial(
number=number,
action=action,
method=method,
Expand All @@ -67,7 +67,7 @@ def echo(self,

:return: <Response> element
"""
return self.append(Echo(
return self.nest(Echo(
**kwargs
))

Expand All @@ -91,7 +91,7 @@ def enqueue(self,
:param kwargs: additional attributes
:return: <Response> element
"""
return self.append(Enqueue(
return self.nest(Enqueue(
name,
action=action,
method=method,
Expand Down Expand Up @@ -133,7 +133,7 @@ def gather(self,
:param kwargs: additional attributes
:return: <Response> element
"""
return self.append(Gather(
return self.nest(Gather(
action=action,
method=method,
timeout=timeout,
Expand All @@ -155,15 +155,15 @@ def hangup(self):

:return: <Response> element
"""
return self.append(Hangup())
return self.nest(Hangup())

def leave(self):
"""
Add a new <Leave> element

:return: <Response> element
"""
return self.append(Leave())
return self.nest(Leave())

def pause(self, length=None):
"""
Expand All @@ -172,7 +172,7 @@ def pause(self, length=None):
:param length: time in seconds to pause
:return: <Response> element
"""
return self.append(Pause(length=length))
return self.nest(Pause(length=length))

def play(self,
url=None,
Expand All @@ -188,7 +188,7 @@ def play(self,
:param kwargs: additional attributes
:return: <Response> element
"""
return self.append(Play(
return self.nest(Play(
url=url,
loop=loop,
digits=digits,
Expand Down Expand Up @@ -225,7 +225,7 @@ def record(self,
:param kwargs: additional attributes
:return: <Response> element
"""
return self.append(Record(
return self.nest(Record(
action=action,
method=method,
timeout=timeout,
Expand All @@ -249,7 +249,7 @@ def redirect(self, url, method=None, **kwargs):
:param kwargs: additional attributes
:return: <Response> element
"""
return self.append(Redirect(url, method=method, **kwargs))
return self.nest(Redirect(url, method=method, **kwargs))

def reject(self, reason=None, **kwargs):
"""
Expand All @@ -259,7 +259,7 @@ def reject(self, reason=None, **kwargs):
:param kwargs: additional attributes
:return: <Response> element
"""
return self.append(Reject(reason=reason, **kwargs))
return self.nest(Reject(reason=reason, **kwargs))

def say(self,
body,
Expand All @@ -277,7 +277,7 @@ def say(self,
:param kwargs: additional attributes
:return: <Response> element
"""
return self.append(Say(
return self.nest(Say(
body,
loop=loop,
language=language,
Expand Down Expand Up @@ -305,7 +305,7 @@ def sms(self,
:param kwargs: additional attributes
:return: <Response> element
"""
return self.append(Sms(
return self.nest(Sms(
body,
to=to,
from_=from_,
Expand Down Expand Up @@ -351,7 +351,7 @@ def client(self,
:param kwargs: additional attributes
:return: <Dial> element
"""
return self.append(Client(
return self.nest(Client(
name,
method=method,
url=url,
Expand Down Expand Up @@ -401,7 +401,7 @@ def conference(self,
:param kwargs: additional attributes
:return: <Dial> element
"""
return self.append(Conference(
return self.nest(Conference(
name,
muted=muted,
start_conference_on_enter=start_conference_on_enter,
Expand Down Expand Up @@ -443,7 +443,7 @@ def number(self,
:param kwargs: additional attributes
:return: <Dial> element
"""
return self.append(Number(
return self.nest(Number(
number,
send_digits=send_digits,
url=url,
Expand Down Expand Up @@ -472,7 +472,7 @@ def queue(self,
:param kwargs: additional attributes
:return: <Dial> element
"""
return self.append(Queue(
return self.nest(Queue(
queue_name,
url=url,
method=method,
Expand All @@ -490,7 +490,7 @@ def sim(self,
:param sid: sim sid
:return: <Dial> element
"""
return self.append(Sim(
return self.nest(Sim(
sid,
**kwargs
))
Expand Down Expand Up @@ -519,7 +519,7 @@ def sip(self,
:param kwargs: additional attributes
:return: <Dial> element
"""
return self.append(Sip(
return self.nest(Sip(
uri,
username=username,
password=password,
Expand Down Expand Up @@ -653,7 +653,7 @@ def task(self, attributes, **kwargs):
:param attributes: Attributes for a task
:return: <Task> element
"""
return self.append(Task(attributes, **kwargs))
return self.nest(Task(attributes, **kwargs))


class Task(TwiML):
Expand Down Expand Up @@ -700,7 +700,7 @@ def say(self,
:param kwargs: additional attributes
:return: <Gather> element
"""
return self.append(Say(
return self.nest(Say(
body,
loop=loop,
language=language,
Expand All @@ -722,7 +722,7 @@ def play(self,
:param kwargs: additional attributes
:return: <Gather> element
"""
return self.append(Play(
return self.nest(Play(
url=url,
loop=loop,
digits=digits,
Expand All @@ -736,7 +736,7 @@ def pause(self, length=None):
:param length: time to pause
:return: <Gather> element
"""
return self.append(Pause(length=length))
return self.nest(Pause(length=length))


class Pause(TwiML):
Expand Down
0