Skip to content
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

Unable to use braintree_python on Google App Engine #53

Closed
vrypan opened this issue May 11, 2015 · 6 comments
Closed

Unable to use braintree_python on Google App Engine #53

vrypan opened this issue May 11, 2015 · 6 comments

Comments

@vrypan
Copy link

vrypan commented May 11, 2015

AI responses are gzip-encoded, but the python library (probably the requests library) treats them as ASCII.

File "xtras/braintree/customer.py", line 126, in find
    return Configuration.gateway().customer.find(customer_id)
  File "xtras/braintree/customer_gateway.py", line 36, in find
    response = self.config.http().get("/customers/" + customer_id)
  File "xtras/braintree/util/http.py", line 57, in get
    return self.__http_do("GET", path)
  File "xtras/braintree/util/http.py", line 82, in __http_do
    return XmlUtil.dict_from_xml(response_body)
  File "xtras/braintree/util/xml_util.py", line 11, in dict_from_xml
    return Parser(xml).parse()
  File "xtras/braintree/util/parser.py", line 15, in __init__
    self.doc = minidom.parseString("><".join(re.split(">\s+<", xml)).strip())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/minidom.py", line 1928, in parseString
    return expatbuilder.parseString(string)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 940, in parseString
    return builder.parseString(string)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 0: ordinal not in range(128)

Modifying http_do() in util/http.py to unzip the content, makes the library work again, but this is probably a fragile way to fix this. (unzip() is just a wrapper to unzip raw data, nothing special.)

   def http_do(self, http_verb, path, headers, request_body):
        response = self.__request_function(http_verb)(
            self.environment.base_url + path,
            headers=headers,
            data=request_body,
            verify=self.environment.ssl_certificate,
            timeout=self.config.timeout
        )
        if response.headers['content-encoding']=='gzip':
            return [response.status_code, unzip(response.content)]
        else:
            return [response.status_code, response.text]
@cbetta
Copy link

cbetta commented May 11, 2015

I can confirm this problem this problem has been brought up to me at some events too. Not sure if an issue or documentation.

@agfor
Copy link

agfor commented May 11, 2015

@vrypan I believe this is a problem with a recent version of the requests library (a dependency of our library). Can you let us know what version of requests you're using, and try version 2.6.0 to see if that fixes it?

@agfor
Copy link

agfor commented May 11, 2015

We've tracked this down to a bug in urllib3 (urllib3/urllib3#616), a dependency of requests. It exists in requests version 2.6.1 and higher, so 2.6.0 is currently the latest version that works.

@adrienbrunet
Copy link

I don't know for you guyz, but to avoid the issue, while waiting for a new release of the api, (and urllib3 if I read correctly) I use .encode('utf-8') on all the data I pass before making the transaction. It works but it is NOT a long term solution. I do not really like this....

@agfor
Copy link

agfor commented May 11, 2015

@adrienbrunet The issue is now being tracked at https://github.com/kennethreitz/requests/issues/2595 and they will hopefully update their version of urllib3 soon. It would be helpful if you could post the exact details of your problem, so I can make sure the latest version of urllib3 fixes it.

@agfor
Copy link

agfor commented May 13, 2015

I've updated the appengine example to cover this problem. Basically, don't use requests 2.6.1, 2.6.2, or 2.7.0. Hopefully later versions will fix the issue.

Alternatively, you can add this to your app.yaml:

env_variables:
  GAE_USE_SOCKETS_HTTPLIB : 'anyvalue'

see https://github.com/kennethreitz/requests/issues/2595#issuecomment-101795229 for more info.

@adrienbrunet I don't think your issue is the same. If you're having unicode issues, you can email me at adam@getbraintree.com and I'll try to help you out.

@agfor agfor closed this as completed May 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants