8000 Add better documentation for TwiML verbs · stomatocode/twilio-python@09d4217 · GitHub
[go: up one dir, main page]

Skip to content

Commit 09d4217

Browse files
committed
Add better documentation for TwiML verbs
Simplify the TwiML generation code base. kwargs are your friend
1 parent 6c4fc66 commit 09d4217

File tree

2 files changed

+56
-81
lines changed

2 files changed

+56
-81
lines changed

tests/test_twiml.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,3 @@ def testImproperNesting(self):
467467
self.assertRaises(TwimlException, verb.append, twiml.Dial())
468468
self.assertRaises(TwimlException, verb.append, twiml.Conference(""))
469469
self.assertRaises(TwimlException, verb.append, twiml.Sms(""))
470-
471-
if __name__ == '__main__':
472-
unittest.main()

twilio/twiml.py

Lines changed: 56 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,24 @@ class TwimlException(Exception):
1212
class Verb(object):
1313
"""Twilio basic verb object.
1414
"""
15+
GET = "GET"
16+
POST = "POST"
17+
nestables = None
18+
1519
def __init__(self, **kwargs):
1620
self.name = self.__class__.__name__
1721
self.body = None
18-
self.nestables = None
1922
self.verbs = []
2023
self.attrs = {}
2124

25+
if kwargs.get("waitMethod", "GET") not in ["GET", "POST"]:
26+
raise TwimlException("Invalid waitMethod parameter, "
27+
"must be 'GET' or 'POST'")
28+
29+
if kwargs.get("method", "GET") not in ["GET", "POST"]:
30+
raise TwimlException("Invalid method parameter, "
31+
"must be 'GET' or 'POST'")
32+
2233
for k, v in kwargs.items():
2334
if k == "sender":
2435
k = "from"
@@ -70,6 +81,7 @@ def xml(self):
7081
return el
7182

7283
def append(self, verb):
84+
print self.nestables
7385
if not self.nestables or verb.name not in self.nestables:
7486
raise TwimlException("%s is not nestable inside %s" % \
7587
(verb.name, self.name))
@@ -79,22 +91,22 @@ def append(self, verb):
7991

8092
class Response(Verb):
8193
"""Twilio response object."""
94+
nestables = [
95+
'Say',
96+
'Play',
97+
'Gather',
98+
'Record',
99+
'Dial',
100+
'Redirect',
101+
'Pause',
102+
'Hangup',
103+
'Reject',
104+
'Sms',
105+
]
106+
82107
def __init__(self, **kwargs):
83108
"""Version: Twilio API version e.g. 2008-08-01 """
84-
85-
Verb.__init__(self, **kwargs)
86-
self.nestables = [
87-
'Say',
88-
'Play',
89-
'Gather',
90-
'Record',
91-
'Dial',
92-
'Redirect',
93-
'Pause',
94-
'Hangup',
95-
'Reject',
96-
'Sms',
97-
]
109+
super(Response, self).__init__(**kwargs)
98110

99111
def say(self, text, **kwargs):
100112
"""Return a newly created :class:`Say` verb, nested inside this
@@ -203,11 +215,11 @@ class Say(Verb):
203215
FRENCH = 'fr'
204216
GERMAN = 'de'
205217

206-
def __init__(self, text, voice=None, language=None, loop=None, **kwargs):
207-
Verb.__init__(self, voice=voice, language=language, loop=loop,
208-
**kwargs)
218+
def __init__(self, text, **kwargs):
219+
super(Say, self).__init__(**kwargs)
209220
self.body = text
210221

222+
211223
class Play(Verb):
212224
"""Play an audio file at a URL
213225
@@ -218,8 +230,8 @@ class Play(Verb):
218230
Specifying '0' will cause the the :class:`Say` verb to loop
219231
until the call is hung up. Defaults to 1.
220232
"""
221-
def __init__(self, url, loop=None, **kwargs):
222-
Verb.__init__(self, loop=loop, **kwargs)
233+
def __init__(self, url, **kwargs):
234+
super(Play, self).__init__(**kwargs)
223235
self.body = url
224236

225237

@@ -229,8 +241,6 @@ class Pause(Verb):
229241
:param length: specifies how many seconds Twilio will wait silently before
230242
continuing on.
231243
"""
232-
def __init__(self, length=None, **kwargs):
233-
Verb.__init__(self, length=length, **kwargs)
234244

235245

236246
class Redirect(Verb):
@@ -244,26 +254,21 @@ class Redirect(Verb):
244254
GET = 'GET'
245255
POST = 'POST'
246256

247-
def __init__(self, url="", method=None, **kwargs):
248-
Verb.__init__(self, method=method, **kwargs)
249-
if method and method not in [self.GET, self.POST]:
250-
raise TwimlException( \
251-
"Invalid method parameter, must be 'GET' or 'POST'")
257+
def __init__(self, url="", **kwargs):
258+
super(Redirect, self).__init__(**kwargs)
252259
self.body = url
253260

254261

255262
class Hangup(Verb):
256263
"""Hangup the call
257264
"""
258-
def __init__(self, **kwargs):
259-
Verb.__init__(self)
260265

261266

262267
class Reject(Verb):
263268
"""Hangup the call
269+
270+
:param reason: not sure
264271
"""
265-
def __init__(self, reason=None, **kwargs):
266-
Verb.__init__(self, reason=reason, **kwargs)
267272

268273

269274
class Gather(Verb):
@@ -277,17 +282,10 @@ class Gather(Verb):
277282
"""
278283
GET = 'GET'
279284
POST = 'POST'
285+
nestables = ['Say', 'Play', 'Pause']
280286

281-
def __init__(self, action=None, method=None, numDigits=None, timeout=None,
282-
finishOnKey=None, **kwargs):
283-
284-
Verb.__init__(self, action=action, method=method,
285-
numDigits=numDigits, timeout=timeout, finishOnKey=finishOnKey,
286-
**kwargs)
287-
if method and (method != self.GET and method != self.POST):
288-
raise TwimlException( \
289-
"Invalid method parameter, must be 'GET' or 'POST'")
290-
self.nestables = ['Say', 'Play', 'Pause']
287+
def __init__(self, **kwargs):
288+
super(Gather, self).__init__(**kwargs)
291289

292290
def say(self, text, **kwargs):
293291
return self.append(Say(text, **kwargs))
@@ -314,8 +312,8 @@ class Number(Verb):
314312
:param number: phone number to dial
315313
:param sendDigits: key to press after connecting to the number
316314
"""
317-
def __init__(self, number, sendDigits=None, **kwargs):
318-
Verb.__init__(self, sendDigits=sendDigits, **kwargs)
315+
def __init__(self, number, **kwargs):
316+
super(Number, self).__init__(**kwargs)
319317
self.body = number
320318

321319

@@ -325,7 +323,7 @@ class Client(Verb):
325323
:param name: Client name to connect to
326324
"""
327325
def __init__(self, name, **kwargs):
328-
Verb.__init__(self, **kwargs)
326+
super(Client, self).__init__(**kwargs)
329327
self.body = name
330328

331329

@@ -341,13 +339,8 @@ class Sms(Verb):
341339
GET = 'GET'
342340
POST = 'POST'
343341

344-
def __init__(self, msg, to=None, sender=None, method=None, action=None,
345-
statusCallback=None, **kwargs):
346-
Verb.__init__(self, action=action, method=method, to=to, sender=sender,
347-
statusCallback=statusCallback, **kwargs)
348-
if method and (method != self.GET and method != self.POST):
349-
raise TwimlException( \
350-
"Invalid method parameter, must be GET or POST")
342+
def __init__(self, msg, **kwargs):
343+
super(Sms, self).__init__(**kwargs)
351344
self.body = msg
352345

353346

@@ -365,16 +358,8 @@ class Conference(Verb):
365358
GET = 'GET'
366359
POST = 'POST'
367360

368-
def __init__(self, name, muted=None, beep=None,
369-
startConferenceOnEnter=None, endConferenceOnExit=None, waitUrl=None,
370-
waitMethod=None, maxParticipants=None, **kwargs):
371-
Verb.__init__(self, muted=muted, beep=beep,
372-
startConferenceOnEnter=startConferenceOnEnter,
373-
endConferenceOnExit=endConferenceOnExit, waitUrl=waitUrl,
374-
waitMethod=waitMethod, maxParticipants=maxParticipants, **kwargs)
375-
if waitMethod and (waitMethod != self.GET and waitMethod != self.POST):
376-
raise TwimlException( \
377-
"Invalid waitMethod parameter, must be GET or POST")
361+
def __init__(self, name, **kwargs):
362+
super(Conference, self).__init__(**kwargs)
378363
self.body = name
379364

380365

@@ -383,21 +368,25 @@ class Dial(Verb):
383368
384369
:param action: submit the result of the dial to this URL
385370
:param method: submit to 'action' url using GET or POST
371+
:param time timeout: The number of seconds to waits for the called
372+
party to answer the call
373+
:param bool hangupOnStar: Allow the calling party to hang up on the
374+
called party by pressing the '*' key
375+
:param int timeLimit: The maximum duration of the Call in seconds
376+
:param callerId: The caller ID that will appear to the called party
377+
:param bool record: Record both legs of a call within this <Dial>
386378
"""
387379
GET = 'GET'
388380
POST = 'POST'
381+
nestables = ['Number', 'Conference', 'Client']
389382

390-
def __init__(self, number=None, action=None, method=None, **kwargs):
391-
Verb.__init__(self, action=action, method=method, **kwargs)
392-
self.nestables = ['Number', 'Conference', 'Client']
383+
def __init__(self, number=None, **kwargs):
384+
super(Dial, self).__init__(**kwargs)
393385
if number and len(number.split(',')) > 1:
394386
for n in number.split(','):
395387
self.append(Number(n.strip()))
396388
else:
397389
self.body = number
398-
if method and (method != self.GET and method != self.POST):
399-
raise TwimlException( \
400-
"Invalid method parameter, must be GET or POST")
401390

402391
def client(self, name, **kwargs):
403392
return self.append(Client(name, **kwargs))
@@ -425,14 +414,3 @@ class Record(Verb):
425414
"""
426415
GET = 'GET'
427416
POST = 'POST'
428-
429-
def __init__(self, action=None, method=None, timeout=None,
430-
finishOnKey=None, maxLength=None, transcribe=None,
431-
transcribeCallback=None, playBeep=None, **kwargs):
432-
Verb.__init__(self, action=action, method=method, timeout=timeout,
433-
finishOnKey=finishOnKey, maxLength=maxLength,
434-
transcribe=transcribe, transcribeCallback=transcribeCallback,
435-
playBeep=playBeep, **kwargs)
436-
if method and (method != self.GET and method != self.POST):
437-
raise TwimlException( \
438-
"Invalid method parameter, must be GET or POST")

0 commit comments

Comments
 (0)
0