Skip to content

Instantly share code, notes, and snippets.

@michaelrice
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save michaelrice/b479bfa98e494b5938ac to your computer and use it in GitHub Desktop.

Select an option

Save michaelrice/b479bfa98e494b5938ac to your computer and use it in GitHub Desktop.
from __future__ import unicode_literals
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
from xml.etree.ElementTree import tostring
def _build_customer_payload(customer):
"""
<customer xmlns="http://www.vmware.com/UM">
<name>Customer Name</name>
<country>US</country>
<postalCode>90210</postalCode>
</customer>
"""
if not 'country' in customer:
raise MissingProperty("Missing required 'country'.")
if not 'name' in customer:
raise MissingProperty("Missing required 'name'")
if not 'postal_code' in customer:
raise MissingProperty("Missing required 'postal_code'")
attribs = {
'xmlns': 'http://www.vmware.com/UM'
}
root = Element('customer', attribs)
name = SubElement(root, 'name')
name.text = customer['name']
country = SubElement(root, 'country')
country.text = customer['country']
postal = SubElement(root, 'postalCode')
postal.text = customer['postal_code']
return tostring(root)
###################
# Method from my test
def test_customer_builder(self):
customer = {
'country': 'US',
'name': '¿Cómo',
'postal_code': '78555'
}
xml = customers._build_customer_payload(customer)
self.assertIsInstance(xml, str)
@michaelrice
Copy link
Copy Markdown
Author

ERROR: test_customer_builder (tests.customer_tests.CustomerTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/errr/programs/python/thunderhead/tests/customer_tests.py", line 139, in test_customer_builder
    xml = customers._build_customer_payload(customer)
  File "/Users/errr/programs/python/thunderhead/thunderhead/builder/customers.py", line 114, in _build_customer_payload
    return tostring(root).decode(encoding='utf-8', errors='ignore')
  File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1126, in tostring
    ElementTree(element).write(file, encoding, method=method)
  File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 820, in write
    serialize(write, self._root, encoding, qnames, namespaces)
  File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 939, in _serialize_xml
    _serialize_xml(write, e, encoding, qnames, None)
  File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 937, in _serialize_xml
    write(_escape_cdata(text, encoding))
  File "/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1073, in _escape_cdata
    return text.encode(encoding, "xmlcharrefreplace")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment