Skip to content
This repository has been archived by the owner on Feb 10, 2021. It is now read-only.

Commit

Permalink
Merge pull request #38 from Bandwidth/DX-467
Browse files Browse the repository at this point in the history
Changed media function to no longer encode an already encoded url
  • Loading branch information
jmulford-bw authored Jun 12, 2019
2 parents 7b2debd + 09b0ef5 commit 299651e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
24 changes: 20 additions & 4 deletions bandwidth/account/client_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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


Expand Down Expand Up @@ -52,6 +53,21 @@ def __init__(self, user_id=None, api_token=None, api_secret=None, **other_option
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 it
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
headers = kwargs.pop('headers', None)
Expand Down Expand Up @@ -1123,7 +1139,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:
Expand All @@ -1149,7 +1165,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']
Expand All @@ -1165,7 +1181,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):
Expand All @@ -1188,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(
Expand Down
2 changes: 1 addition & 1 deletion bandwidth/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = 'v3.0.3'
__version__ = 'v3.1.0'
17 changes: 17 additions & 0 deletions tests/bandwidth/test_account_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,20 @@ 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))

0 comments on commit 299651e

Please sign in to comment.