Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions boxsdk/object/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ def get_thumbnail_representation(self, dimensions, extension='png'):
Retrieve a thumbnail image for the file.

:param dimensions:
The width by height size of this representation in pixels (i.e. '92x92')
The width by height size of this representation in pixels (e.g. '92x92')
:type dimensions:
`unicode`
:param extension:
Expand All @@ -785,10 +785,12 @@ def get_thumbnail_representation(self, dimensions, extension='png'):
"""
rep_hints = '[{0}?dimensions={1}]'.format(extension, dimensions)
representation = self.get_representation_info(rep_hints)
url = representation[0]['content']['url_template']
url = url.replace('{+asset_path}', '')
response = self._session.get(url, expect_json_response=False)
return response.content
if len(representation):
url = representation[0]['content']['url_template']
url = url.replace('{+asset_path}', '')
response = self._session.get(url, expect_json_response=False)
return response.content
return b''

@api_call
def copy(self, parent_folder, name=None, file_version=None):
Expand Down
34 changes: 34 additions & 0 deletions test/unit/object/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,3 +868,37 @@ def test_get_thumbnail_representation(
params={'fields': 'representations'})
mock_box_session.get.assert_any_call(content_url, expect_json_response=False)
assert thumb == mock_content_response.content


def test_get_thumbnail_representation_not_found(
test_file,
mock_box_session,
mock_content_response,
):
representation_url = '{0}/files/{1}'.format(API.BASE_API_URL, test_file.object_id)
dimensions = '100x100'
extension = 'jpg'

mock_representations_response = Mock()
mock_representations_response.json.return_value = {
'etag': '1',
'id': test_file.object_id,
'representations': {
'entries': [],
},
'type': 'file'
}

mock_box_session.get.side_effect = [mock_representations_response, mock_content_response]

thumb = test_file.get_thumbnail_representation(
dimensions=dimensions,
extension=extension,
)

mock_box_session.get.assert_any_call(
representation_url,
headers={'X-Rep-Hints': '[{}?dimensions={}]'.format(extension, dimensions)},
params={'fields': 'representations'},
)
assert b'' == thumb