From a7f60cba6110f4da256a033f929edd667d4386c6 Mon Sep 17 00:00:00 2001 From: jmulford-bandwidth Date: Mon, 10 Jun 2019 14:35:56 -0400 Subject: [PATCH 1/5] Made quote() not encoded an already encoded string --- bandwidth/account/client_module.py | 23 ++++++++++++++++++++--- tests/bandwidth/test_account_client.py | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/bandwidth/account/client_module.py b/bandwidth/account/client_module.py index 1fc58d8..98ecb2d 100644 --- a/bandwidth/account/client_module.py +++ b/bandwidth/account/client_module.py @@ -11,9 +11,11 @@ from .api_exception_module import BandwidthAccountAPIException quote = urllib.parse.quote if six.PY3 else urllib.quote +unquote = urllib.parse.unquote if six.PY3 else urllib.unquote lazy_map = map if six.PY3 else itertools.imap + class Client: """ @@ -51,6 +53,21 @@ def __init__(self, user_id=None, api_token=None, api_secret=None, **other_option 'api_endpoint', 'https://api.catapult.inetwork.com') self.api_version = other_options.get('api_version', 'v1') self.auth = (api_token, api_secret) + + def _encode_if_not_encoded(self, str_): + """ + Takes a string and encodes it if it is not already encoded, otherwise does nothing to id + + params: + str_ (string): + :type str_: str + :param str_: string to check encoding + """ + unparsed_str = unquote(str_) + if unparsed_str == str_: + return quote(str_) + else: + return str_ def _request(self, method, url, *args, **kwargs): user_agent = 'PythonSDK_' + version @@ -1123,7 +1140,7 @@ def upload_media_file(self, media_name, content=None, content_type='application/ if file_path is not None and content is None: content = open(file_path, 'rb') is_file_path = True - path = '/users/%s/media/%s' % (self.user_id, quote(media_name)) + path = '/users/%s/media/%s' % (self.user_id, self._encode_if_not_encoded(media_name)) try: return self._make_request('put', path, data=content, headers={'content-type': content_type}) finally: @@ -1149,7 +1166,7 @@ def download_media_file(self, media_name): with io.open(media['media_name'], 'wb') as file: file.write(stream.read()) """ - path = '/users/%s/media/%s' % (self.user_id, quote(media_name)) + path = '/users/%s/media/%s' % (self.user_id, self._encode_if_not_encoded(media_name)) response = self._request('get', path, stream=True) response.raise_for_status() return response.raw, response.headers['content-type'] @@ -1165,7 +1182,7 @@ def delete_media_file(self, media_name): api.delete_media_file('file1.txt') """ - path = '/users/%s/media/%s' % (self.user_id, quote(media_name)) + path = '/users/%s/media/%s' % (self.user_id, self._encode_if_not_encoded(media_name)) self._make_request('delete', path) def get_number_info(self, number): diff --git a/tests/bandwidth/test_account_client.py b/tests/bandwidth/test_account_client.py index dfb9e3a..ae827d6 100644 --- a/tests/bandwidth/test_account_client.py +++ b/tests/bandwidth/test_account_client.py @@ -143,3 +143,21 @@ def test_make_request_with_location_header(self): headers=headers, auth=('apiToken', 'apiSecret')) self.assertIs(estimated_response, response) self.assertEqual('id', id) + + def test_encode_if_not_encoded_encoded_string(self): + """ + encode_if_not_encoded() should return the same string if it is already encoded + """ + client = Client('userId', 'apiToken', 'apiSecret') + encoded_string = "Home%281%29-m-m4dh5ym.jpg" + self.assertEqual(encoded_string, client._encode_if_not_encoded(encoded_string)) + + def test_encoded_if_not_encoded_non_encoded_string(self): + """ + encoded_if_not_encoded() should encode the string if not already encoded + """ + client = Client('userId', 'apiToken', 'apiSecret') + encoded_string = "Home%281%29-m-m4dh5ym.jpg" + non_encoded_string = "Home(1)-m-m4dh5ym.jpg" + self.assertEqual(encoded_string, client._encode_if_not_encoded(non_encoded_string)) + From f2171c08a60819be44fab99df55d95a1fe0ce439 Mon Sep 17 00:00:00 2001 From: jmulford-bandwidth Date: Mon, 10 Jun 2019 14:40:14 -0400 Subject: [PATCH 2/5] Fixed code style issues, added missing encoding --- bandwidth/account/client_module.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/bandwidth/account/client_module.py b/bandwidth/account/client_module.py index 98ecb2d..929eb11 100644 --- a/bandwidth/account/client_module.py +++ b/bandwidth/account/client_module.py @@ -15,7 +15,6 @@ lazy_map = map if six.PY3 else itertools.imap - class Client: """ @@ -53,7 +52,7 @@ def __init__(self, user_id=None, api_token=None, api_secret=None, **other_option 'api_endpoint', 'https://api.catapult.inetwork.com') self.api_version = other_options.get('api_version', 'v1') self.auth = (api_token, api_secret) - + def _encode_if_not_encoded(self, str_): """ Takes a string and encodes it if it is not already encoded, otherwise does nothing to id @@ -1205,7 +1204,7 @@ def get_number_info(self, number): ## 'updated' : '2017-02-10T09:11:50Z'} """ - path = '/phoneNumbers/numberInfo/%s' % quote(number) + path = '/phoneNumbers/numberInfo/%s' % self._encode_if_not_encoded(number) return self._make_request('get', path)[0] def list_phone_numbers( From 201f0be280cdc51fefe280bdbe68ad5eb70be8eb Mon Sep 17 00:00:00 2001 From: jmulford-bandwidth Date: Mon, 10 Jun 2019 14:47:17 -0400 Subject: [PATCH 3/5] Fixed another code style error --- tests/bandwidth/test_account_client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/bandwidth/test_account_client.py b/tests/bandwidth/test_account_client.py index ae827d6..90c38d2 100644 --- a/tests/bandwidth/test_account_client.py +++ b/tests/bandwidth/test_account_client.py @@ -160,4 +160,3 @@ def test_encoded_if_not_encoded_non_encoded_string(self): encoded_string = "Home%281%29-m-m4dh5ym.jpg" non_encoded_string = "Home(1)-m-m4dh5ym.jpg" self.assertEqual(encoded_string, client._encode_if_not_encoded(non_encoded_string)) - From 9cb7f844c61ae6aa6d4c87167a9075fe757a0ea4 Mon Sep 17 00:00:00 2001 From: jmulford-bandwidth Date: Wed, 12 Jun 2019 09:51:43 -0400 Subject: [PATCH 4/5] Updated version --- bandwidth/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bandwidth/version.py b/bandwidth/version.py index fb43b44..dc895ba 100644 --- a/bandwidth/version.py +++ b/bandwidth/version.py @@ -1 +1 @@ -__version__ = 'v3.0.3' +__version__ = 'v3.1.0' From 09b0ef591b2b68dea0ff87fe8e9b46a8261511e1 Mon Sep 17 00:00:00 2001 From: jmulford-bandwidth Date: Wed, 12 Jun 2019 10:42:59 -0400 Subject: [PATCH 5/5] Fixed typo in code comment --- bandwidth/account/client_module.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bandwidth/account/client_module.py b/bandwidth/account/client_module.py index 929eb11..e85ebf8 100644 --- a/bandwidth/account/client_module.py +++ b/bandwidth/account/client_module.py @@ -55,7 +55,7 @@ def __init__(self, user_id=None, api_token=None, api_secret=None, **other_option def _encode_if_not_encoded(self, str_): """ - Takes a string and encodes it if it is not already encoded, otherwise does nothing to id + Takes a string and encodes it if it is not already encoded, otherwise does nothing to it params: str_ (string):