htarevern / salesforce-python-sdk

Salesforce Python SDK
BSD 3-Clause "New" or "Revised" License
34 stars 15 forks source link

login.py: SOAP XML response with British pound symbol causes encoding error #8

Open dalefrancum opened 7 years ago

dalefrancum commented 7 years ago

While using British pounds for currency, during the SOAP API call to authenticate, a response is returned from Salesforce which contains this line:

<currencySymbol>£</currencySymbol>

The login.py code tried to parse the response with this line:

xml_value = xml.dom.minidom.parseString(response.text)

The following error occurred:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 777: ordinal not in range(128)

I have a proposed fix for this, which is to remove the node from the XML response string. I will post the patch shortly.

dalefrancum commented 7 years ago

I don't have rights to push a branch. Here is a diff for my code changes:

--- a/salesforce/login.py
+++ b/salesforce/login.py
@@ -3,6 +3,7 @@ from exception import AuthenticationFailed
 import urllib
 import utils
 import xml.dom.minidom
+import re

 class Authentication(object):
@@ -165,7 +166,10 @@ class LoginWithSoapAPI(Login):
                                       headers,
                                       data=data)

-        xml_value = xml.dom.minidom.parseString(response.text)
+        # Remove currency symbol from XML response
+        response_text  = re.sub('<currencySymbol>.*</currencySymbol>', '', response.text)
+
+        xml_value = xml.dom.minidom.parseString(response_text)
         access_token = utils.get_element_by_name(xml_value, 'sessionId')
         url = urlparse(utils.get_element_by_name(xml_value, 'serverUrl'))
         instance_url = '{0}://{1}'.format(url.scheme, url.netloc)
dalefrancum commented 7 years ago

I forked the repo and created a pull request from my branch into this master: #9