10000 Next gen twiml by jingming · Pull Request #316 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

Next gen twiml #316

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
merged 8 commits into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Move TwiML to unique types
  • Loading branch information
jingming committed Mar 2, 2017
commit 0af4776a961a5b0fef8052082fb1f4027cff172e
76 changes: 76 additions & 0 deletions twilio/twiml/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import xml.etree.ElementTree as ET


def lower_camel(string):
result = "".join([x.title() for x in string.split('_')])
if not result:
return result

return result[0].lower() + result[1:]


class TwiML(object):
"""
Twilio basic verb object.
"""
MAP = {
'from_': 'from'
}

def __init__(self, **kwargs):
self.name = self.__class__.__name__
self.body = None
self.verbs = []
self.attrs = {}

for k, v in kwargs.items():
if v is not None:
self.attrs[lower_camel(self.MAP.get(k, k))] = v

def __str__(self):
return self.to_xml()

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
return False

def to_xml(self, xml_declaration=True):
"""
Return the contents of this verb as an XML string

:param bool xml_declaration: Include the XML declaration. Defaults to
True
"""
xml = ET.tostring(self.xml()).decode('utf-8')

if xml_declaration:
return '<?xml version="1.0" encoding="UTF-8"?>' + xml
else:
return xml

def xml(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or prefix with _ if not public

el = ET.Element(self.name)

keys = self.attrs.keys()
keys = sorted(keys)
for a in keys:
value = self.attrs[a]

if isinstance(value, bool):
el.set(a, str(value).lower())
else:
el.set(a, str(value))

if self.body:
el.text = self.body

for verb in self.verbs:
el.append(verb.xml())

return el

def append(self, verb):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if its an instance of TwiML?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, it would help prevent mistakes.

self.verbs.append(verb)
return verb
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nicer to return self here.

response.append(Dial()).append(Sms())

64 changes: 64 additions & 0 deletions twilio/twiml/messaging_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from twilio.twiml import TwiML


class MessagingResponse(TwiML):

def __init__(self):
super(MessagingResponse, self).__init__()
self.name = 'Response'

def message(self,
body,
to=None,
from_=None,
method=None,
action=None,
status_callback=None,
**kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...ew one line per param?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine and valid.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the other style where you keep them inline until the length limit then wrap to the next line. but not a big deal.

return self.append(Message(
body=body,
to=to,
from_=from_,
method=method,
action=action,
status_callback=status_callback,
**kwargs
))

def redirect(self, method=None, url=None, **kwargs):
return self.append(Redirect(
method=method,
url=url,
**kwargs
))


class Message(TwiML):

def __init__(self, body=None, **kwargs):
super(Message, self).__init__(**kwargs)
if body:
self.body = body

def body(self, body):
return self.append(Body(body))

def media(self, url):
return self.append(Media(url))


class Body(TwiML):
def __init__(self, body):
super(Body, self).__init__()
self.body = body


class Media(TwiML):
def __init__(self, url):
super(Media, self).__init__()
self.body = url


class Redirect(TwiML):
pass

Loading
0