From 598d0580aa46559755639e5e9581cf441db4f7b5 Mon Sep 17 00:00:00 2001 From: Paul McDermott Date: Wed, 21 Jan 2015 14:14:48 +0000 Subject: [PATCH 1/6] [FIX] Use requests.utils.default_user_agent() to set the default User-Agent header --- prestapyt/prestapyt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prestapyt/prestapyt.py b/prestapyt/prestapyt.py index 96732d8..91338d1 100644 --- a/prestapyt/prestapyt.py +++ b/prestapyt/prestapyt.py @@ -33,8 +33,8 @@ except ImportError, e: from xml.etree import ElementTree -requests.defaults.defaults['base_headers']['User-Agent'] = 'Prestapyt: Python Prestashop Library' - +#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 From ba72e47dfc8b9047df330e9615c58e463e79839f Mon Sep 17 00:00:00 2001 From: Paul McDermott Date: Wed, 21 Jan 2015 16:40:26 +0000 Subject: [PATCH 2/6] [FIX] Change way session object is set-up [FIX] Remove commented out sections from previous fix [FIX] Change the way session object is set-up --- prestapyt/prestapyt.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/prestapyt/prestapyt.py b/prestapyt/prestapyt.py index 91338d1..cad859b 100644 --- a/prestapyt/prestapyt.py +++ b/prestapyt/prestapyt.py @@ -33,7 +33,6 @@ 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): @@ -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): """ From de85ee0eac2030b4007671fd954661c4547bc153 Mon Sep 17 00:00:00 2001 From: Paul McDermott Date: Wed, 21 Jan 2015 16:46:07 +0000 Subject: [PATCH 3/6] [FIX] 'attrs' element no longer provided in Prestashop response --- prestapyt/prestapyt.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/prestapyt/prestapyt.py b/prestapyt/prestapyt.py index cad859b..0298327 100644 --- a/prestapyt/prestapyt.py +++ b/prestapyt/prestapyt.py @@ -487,11 +487,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).\ @@ -502,9 +502,9 @@ def dive(response, level=1): if not elems: return [] elif isinstance(elems, list): - ids = [int(elem['attrs']['id']) for elem in elems] + ids = [int(elem['id']) for elem in elems] else: - ids = [int(elems['attrs']['id'])] + ids = [int(elems['id'])] return ids def get(self, resource, resource_id=None, options=None): From 9fe736837a24177771f524b9c628a5b230fad0ed Mon Sep 17 00:00:00 2001 From: Paul McDermott Date: Wed, 21 Jan 2015 16:57:12 +0000 Subject: [PATCH 4/6] [FIX] search method can't return just IDs: 'display' option needs to be respected --- prestapyt/prestapyt.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/prestapyt/prestapyt.py b/prestapyt/prestapyt.py index 0298327..68fbeea 100644 --- a/prestapyt/prestapyt.py +++ b/prestapyt/prestapyt.py @@ -502,10 +502,9 @@ def dive(response, level=1): if not elems: return [] elif isinstance(elems, list): - ids = [int(elem['id']) for elem in elems] + return elems else: - ids = [int(elems['id'])] - return ids + return [elems] def get(self, resource, resource_id=None, options=None): """ From ea3ef0d4983bb980c0e9b03a462b959d945a0083 Mon Sep 17 00:00:00 2001 From: Paul McDermott Date: Wed, 21 Jan 2015 16:58:17 +0000 Subject: [PATCH 5/6] [FIX] Change MAX_COMPATIBLE_VERSION to 1.6.11 --- prestapyt/prestapyt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prestapyt/prestapyt.py b/prestapyt/prestapyt.py index 68fbeea..e627850 100644 --- a/prestapyt/prestapyt.py +++ b/prestapyt/prestapyt.py @@ -62,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): """ From d23424b1ac1bb5bc9ad4e40978d0c8ab8cd6d656 Mon Sep 17 00:00:00 2001 From: Paul McDermott Date: Wed, 21 Jan 2015 17:17:50 +0000 Subject: [PATCH 6/6] [FIX] No longer URL encode XML data submitted for POST requests (may not be backwards compatible) --- prestapyt/prestapyt.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/prestapyt/prestapyt.py b/prestapyt/prestapyt.py index e627850..7e8a9cc 100644 --- a/prestapyt/prestapyt.py +++ b/prestapyt/prestapyt.py @@ -326,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)