8000 Allow adding TwiML children with generic tag names (#425) · BioComputing/twilio-python@394dd1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 394dd1f

Browse files
authored
Allow adding TwiML children with generic tag names (twilio#425)
* adds add_child method * Add tests to voice_response * address code review comments and fix typo * Puts breaks between tags on test_voice_response
1 parent e4e6ae3 commit 394dd1f

File tree

5 files changed

+69
-18
lines changed

5 files changed

+69
-18
lines changed

tests/unit/twiml/test_messaging_response.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ def test_nested_verbs(self):
4242
'<?xml version="1.0" encoding="UTF-8"?><Response><Message>Hello<Media>example.com</Media></Message></Response>'
4343
)
4444

45+
def test_child_node(self):
46+
with MessagingResponse() as r:
47+
with r.add_child('message', tag='global') as mod:
48+
mod.add_child('bold', 'Hello')
49+
50+
assert_equal(
51+
self.strip(r),
52+
'<?xml version="1.0" encoding="UTF-8"?><Response><message tag="global"><bold>Hello</bold></message></Response>')
53+
4554

4655
class TestMessage(TwilioTest):
4756

tests/unit/twiml/test_voice_response.py

Lines changed: 48 additions & 16 deletions
1E0A
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def test_response(self):
2626

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

3233
def test_response_chain(self):
@@ -41,7 +42,8 @@ def test_response_chain(self):
4142

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

4749
def test_nested_verbs(self):
@@ -124,7 +126,8 @@ def test_say_all(self):
124126

125127
assert_equal(
126128
self.strip(r),
127-
'<?xml version="1.0" encoding="UTF-8"?><Response><Say language="fr" loop="3" voice="man">Hello Monkey</Say></Response>'
129+
'<?xml version="1.0" encoding="UTF-8"?><Response><Say language="fr" loop="3" voice="man">'
130+
'Hello Monkey</Say></Response>'
128131
)
129132

130133

@@ -200,7 +203,8 @@ def test_record_max_length_finish_timeout(self):
200203

201204
assert_equal(
202205
self.strip(r),
203-
'<?xml version="1.0" encoding="UTF-8"?><Response><Record finishOnKey="#" maxLength="30" timeout="4" /></Response>'
206+
'<?xml version="1.0" encoding="UTF-8"?><Response><Record finishOnKey="#" maxLength="30" timeout="4" '
207+
'/></Response>'
204208
)
205209

206210
def test_record_transcribe(self):
@@ -240,7 +244,8 @@ def test_redirect_method_params(self):
240244

241245
assert_equal(
242246
self.strip(r),
243-
'<?xml version="1.0" encoding="UTF-8"?><Response><Redirect method="POST">example.com?id=34&amp;action=hey</Redirect></Response>'
247+
'<?xml version="1.0" encoding="UTF-8"?><Response>'
248+
'<Redirect method="POST">example.com?id=34&amp;action=hey</Redirect></Response>'
244249
)
245250

246251

@@ -312,7 +317,9 @@ def test_to_from_action(self):
312317

313318
assert_equal(
314319
self.strip(r),
315-
'<?xml version="1.0" encoding="UTF-8"?><Response><Sms from="3453453456" statusCallback="example.com?id=34&amp;action=hey" to="1231231234">Hello, World</Sms></Response>'
320+
'<?xml version="1.0" encoding="UTF-8"?><Response>'
321+
'<Sms from="3453453456" statusCallback="example.com?id=34&amp;action=hey" to="1231231234">'
322+
'Hello, World</Sms></Response>'
316323
)
317324

318325
def test_action_method(self):
@@ -322,7 +329,8 @@ def test_action_method(self):
322329

323330
assert_equal(
324331
self.strip(r),
325-
'<?xml version="1.0" encoding="UTF-8"?><Response><Sms action="example.com?id=34&amp;action=hey" method="POST">Hello</Sms></Response>'
332+
'<?xml version="1.0" encoding="UTF-8"?><Response>'
333+
'<Sms action="example.com?id=34&amp;action=hey" method="POST">Hello</Sms></Response>'
326334
)
327335

328336

@@ -343,7 +351,9 @@ def test_conference(self):
343351

344352
assert_equal(
345353
self.strip(r),
346-
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Conference beep="false" endConferenceOnExit="true" startConferenceOnEnter="true" waitUrl="">TestConferenceAttributes</Conference></Dial></Response>'
354+
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial>'
355+
'<Conference beep="false" endConferenceOnExit="true" startConferenceOnEnter="true" waitUrl="">'
356+
'TestConferenceAttributes</Conference></Dial></Response>'
347357
)
348358

349359
def test_muted_conference(self):
@@ -362,7 +372,9 @@ def test_muted_conference(self):
362372

363373
assert_equal(
364374
self.strip(r),
365-
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Conference beep="false" endConferenceOnExit="true" muted="true" startConferenceOnEnter="true" waitUrl="">TestConferenceMutedAttribute</Conference></Dial></Response>'
375+
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial>'
376+
'<Conference beep="false" endConferenceOnExit="true" muted="true" startConferenceOnEnter="true" waitUrl="">'
377+
'TestConferenceMutedAttribute</Conference></Dial></Response>'
366378
)
367379

368380

@@ -377,7 +389,8 @@ def test_queue(self):
377389

378390
assert_equal(
379391
self.strip(r),
380-
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Queue method="GET" url="">TestQueueAttribute</Queue></Dial></Response>'
392+
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial>'
393+
'<Queue method="GET" url="">TestQueueAttribute</Queue></Dial></Response>'
381394
)
382395

383396

@@ -407,7 +420,9 @@ def test_enqueue(self):
407420

408421
assert_equal(
409422
self.strip(r),
410-
'<?xml version="1.0" encoding="UTF-8"?><Response><Enqueue action="act" method="GET" waitUrl="wait" waitUrlMethod="POST">TestEnqueueAttribute</Enqueue></Response>'
423+
'<?xml version="1.0" encoding="UTF-8"?><Response>'
424+
'<Enqueue action="act" method="GET" waitUrl="wait" waitUrlMethod="POST">TestEnqueueAttribute</Enqueue>'
425+
'</Response>'
411426
)
412427

413428
def test_task_string(self):
@@ -419,7 +434,8 @@ def test_task_string(self):
419434

420435
assert_equal(
421436
self.strip(r),
422-
'<?xml version="1.0" encoding="UTF-8"?><Response><Enqueue workflowSid="123123123"><Task>{"account_sid": "AC123123123"}</Task></Enqueue></Response>'
437+
'<?xml version="1.0" encoding="UTF-8"?><Response><Enqueue workflowSid="123123123">'
438+
'<Task>{"account_sid": "AC123123123"}</Task></Enqueue></Response>'
423439
)
424440

425441
def test_task_dict(self):
@@ -430,7 +446,8 @@ def test_task_dict(self):
430446
r.append(e)
431447

432448
assert_equal(
433-
'<?xml version="1.0" encoding="UTF-8"?><Response><Enqueue workflowSid="123123123"><Task>{"account_sid": "AC123123123"}</Task></Enqueue></Response>',
449+
'<?xml version="1.0" encoding="UTF-8"?><Response><Enqueue workflowSid="123123123">'
450+
'<Task>{"account_sid": "AC123123123"}</Task></Enqueue></Response>',
434451
self.strip(r)
435452
)
436453

@@ -482,7 +499,8 @@ def test_sip_username_password(self):
482499

483500
assert_equal(
484501
self.strip(r),
485-
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Sip password="bar" username="foo">foo@example.com</Sip></Dial></Response>'
502+
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial>'
503+
'<Sip password="bar" username="foo">foo@example.com</Sip></Dial></Response>'
486504
)
487505

488506
def test_add_number(self):
@@ -508,7 +526,9 @@ def test_add_number_status_callback_event(self):
508526

509527
assert_equal(
510528
self.strip(r),
511-
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial><Number statusCallback="http://example.com" statusCallbackEvent="initiated completed">1231231234</Number></Dial></Response>'
529+
'<?xml version="1.0" encoding="UTF-8"?><Response><Dial>'
530+
'<Number statusCallback="http://example.com" statusCallbackEvent="initiated completed">1231231234</Number>'
531+
'</Dial></Response>'
512532
)
513533

514534
def test_add_conference(self):
@@ -600,7 +620,8 @@ def test_nested_say_play_pause(self):
600620

601621
assert_equal(
602622
self.strip(r),
603-
'<?xml version="1.0" encoding="UTF-8"?><Response><Gather><Say>Hey</Say><Play>hey.mp3</Play><Pause /></Gather></Response>'
623+
'<?xml version="1.0" encoding="UTF-8"?><Response><Gather><Say>Hey</Say><Play>hey.mp3</Play>'
624+
'<Pause /></Gather></Response>'
604625
)
605626

606627

@@ -624,3 +645,14 @@ def text_mixed(self):
624645
self.strip(r),
625646
'<?xml version="1.0" encoding="UTF-8"?><Response>before<Say>Content</Say>after</Response>'
626647
)
648+
649+
def test_add_child(self):
650+
with VoiceResponse() as r:
651+
with r.add_child('alexa', omnipresent='true') as alexa:
652+
alexa.add_child('purchase', 'Kindle')
653+
654+
assert_equal(
655+
self.strip(r),
656+
'<?xml version="1.0" encoding="UTF-8"?><Response><alexa omnipresent="true">'
657+
'<purchase>Kindle</purchase></alexa></Response>'
658+
)

twilio/twiml/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,13 @@ def xml(self):
120120
el.append(last_child)
121121

122122
return el
123+
124+
def add_child(self, name, value=None, **kwargs):
125+
return self.nest(GenericNode(name, value, **kwargs))
126+
127+
128+
class GenericNode(TwiML):
129+
def __init__(self, name, value, **kwargs):
130+
super(GenericNode, self).__init__(**kwargs)
131+
self.name = name
132+
self.value = value

twilio/twiml/messaging_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import json
1010
from twilio.twiml import (
1111
TwiML,
12-
format_language,
12+
format_language
1313
)
1414

1515

twilio/twiml/voice_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import json
1010
from twilio.twiml import (
1111
TwiML,
12-
format_language,
12+
format_language
1313
)
1414

1515

0 commit comments

Comments
 (0)
0