diff --git a/prestapyt/prestapyt.py b/prestapyt/prestapyt.py index 96732d8..7e8a9cc 100644 --- a/prestapyt/prestapyt.py +++ b/prestapyt/prestapyt.py @@ -33,8 +33,7 @@ except ImportError, e: from xml.etree import ElementTree -requests.defaults.defaults['base_headers']['User-Agent'] = 'Prestapyt: Python Prestashop Library' - +requests.utils.default_user_agent('Prestapyt: Python Prestashop Library') class PrestaShopWebServiceError(Exception): """Generic PrestaShop WebServices error class @@ -63,7 +62,7 @@ class PrestaShopWebService(object): """ MIN_COMPATIBLE_VERSION = '1.4.0.17' - MAX_COMPATIBLE_VERSION = '1.5.4.0' + MAX_COMPATIBLE_VERSION = '1.6.11.0' def __init__(self, api_url, api_key, debug=False, headers=None, client_args=None): """ @@ -99,13 +98,18 @@ def __init__(self, api_url, api_key, debug=False, headers=None, client_args=None # optional arguments self.debug = debug - client_args.update({'auth' : (api_key, '')}) # use header you coders you want, otherwise, use a default self.headers = {} if headers is None else headers # init http client in the init for re-use the same connection for all call - self.client = requests.session(**client_args) + self.client = requests.Session() + self.client.auth=(api_key, '') + if 'timeout' in client_args: + self.client.timeout=client_args['timeout'] + if 'ca_certs' in client_args: + self.client.cert=client_args['ca_certs'] + def _parse_error(self, xml_content): """ @@ -322,7 +326,16 @@ def add_with_url(self, url, content, img_filename=None): pretty_body = content print "Execute url: %s / method: POST\nbody: %s" % (url, pretty_body) - r = self._execute(url, 'POST', data=urllib.urlencode({'xml': content.encode('utf-8')}), add_headers=headers) + # Needed to remove the URL Encoding as the POST method was no longer working with URL Encoded data + # I do not think this is correct, as the payload for a POST should be ASCII data. + # See http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data/4073451#4073451 + # for an explanation was to why POST data should be URL encoded for application/x-www-form-urlencoded + # submissions + # I presume this was working previously, and can only speculate that there has been some sort of change + # in the Prestashop API handling of POST methods + # This version works for Prestashop 1.6.11 + + r = self._execute(url, 'POST', data=content.encode('utf-8'), add_headers=headers) else: img_binary = base64.decodestring(content) img_file = StringIO(img_binary) @@ -483,11 +496,11 @@ def dive(response, level=1): # returned response looks like : # for many resources : - # {'addresses': {'address': [{'attrs': {'id': '1'}, 'value': ''}, - # {'attrs': {'id': '2'}, 'value': ''}, - # {'attrs': {'id': '3'}, 'value': ''}]}} + # {'addresses': {'address': [{'id': '1', 'value': ''}, + # {'id': '2', 'value': ''}, + # {'id': '3', 'value': ''}]}} # for one resource : - # {'addresses': {'address': {'attrs': {'id': '1'}, 'value': ''}}} + # {'addresses': {'address': {'id': '1', 'value': ''}}} # for zero resource : # {'addresses': ''} response = super(PrestaShopWebServiceDict, self).\ @@ -498,10 +511,9 @@ def dive(response, level=1): if not elems: return [] elif isinstance(elems, list): - ids = [int(elem['attrs']['id']) for elem in elems] + return elems else: - ids = [int(elems['attrs']['id'])] - return ids + return [elems] def get(self, resource, resource_id=None, options=None): """