-
Notifications
You must be signed in to change notification settings - Fork 219
Stream uploads from disk. #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # coding: utf-8 | ||
|
|
||
| from __future__ import unicode_literals | ||
|
|
||
| from requests_toolbelt.multipart.encoder import MultipartEncoder | ||
|
|
||
| from boxsdk.util.ordered_dict import OrderedDict | ||
|
|
||
|
|
||
| class MultipartStream(MultipartEncoder): | ||
| """ | ||
| Subclass of the requests_toolbelt's :class:`MultipartEncoder` that ensures that data | ||
| is encoded before files. This allows a server to process information in the data before | ||
| receiving the file bytes. | ||
| """ | ||
| def __init__(self, data, files): | ||
| fields = OrderedDict() | ||
| for k in data: | ||
| fields[k] = data[k] | ||
| for k in files: | ||
| fields[k] = files[k] | ||
| super(MultipartStream, self).__init__(fields) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| cryptography>=0.9.2 | ||
| pyjwt>=1.3.0 | ||
| requests>=2.4.3 | ||
| requests-toolbelt>=0.4.0 | ||
| six >= 1.4.0 | ||
| . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,8 +119,8 @@ def test_box_session_seeks_file_after_retry(box_session, server_error_response, | |
| assert box_response.status_code == 200 | ||
| assert box_response.json() == generic_successful_response.json() | ||
| assert box_response.ok == generic_successful_response.ok | ||
| mock_file_1.tell.assert_called_once_with() | ||
| mock_file_2.tell.assert_called_once_with() | ||
| mock_file_1.tell.assert_called_with() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| mock_file_2.tell.assert_called_with() | ||
| mock_file_1.seek.assert_called_with(0) | ||
| assert mock_file_1.seek.call_count == 2 | ||
| assert mock_file_1.seek.has_calls(call(0) * 2) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # coding: utf-8 | ||
|
|
||
| from __future__ import unicode_literals, absolute_import | ||
|
|
||
| import pytest | ||
|
|
||
| from boxsdk.util.multipart_stream import MultipartStream | ||
|
|
||
|
|
||
| @pytest.fixture(params=({}, {'data_1': b'data_1_value', 'data_2': b'data_2_value'})) | ||
| def multipart_stream_data(request): | ||
| return request.param | ||
|
|
||
|
|
||
| @pytest.fixture(params=({}, {'file_1': b'file_1_value', 'file_2': b'file_2_value'})) | ||
| def multipart_stream_files(request): | ||
| return request.param | ||
|
|
||
|
|
||
| def test_multipart_stream_orders_data_before_files(multipart_stream_data, multipart_stream_files): | ||
| # pylint:disable=redefined-outer-name | ||
| if not multipart_stream_data and not multipart_stream_files: | ||
| pytest.xfail('Encoder does not support empty fields.') | ||
| stream = MultipartStream(multipart_stream_data, multipart_stream_files) | ||
| encoded_stream = stream.to_string() | ||
| data_indices = [encoded_stream.find(value) for value in multipart_stream_data.values()] | ||
| file_indices = [encoded_stream.find(value) for value in multipart_stream_files.values()] | ||
| assert -1 not in data_indices | ||
| assert -1 not in file_indices | ||
| assert all((all((data_index < f for f in file_indices)) for data_index in data_indices)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # coding: utf-8 | ||
|
|
||
| from __future__ import unicode_literals, absolute_import |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # coding: utf-8 | ||
|
|
||
| from __future__ import unicode_literals, absolute_import | ||
|
|
||
|
|
||
| from mock import mock_open | ||
|
|
||
|
|
||
| def streamable_mock_open(mock=None, read_data=b''): | ||
| mock = mock_open(mock, read_data) | ||
| handle = mock.return_value | ||
| handle.position = 0 | ||
|
|
||
| def tell(): | ||
| return handle.position | ||
|
|
||
| def read(size=-1): | ||
| if size == -1: | ||
| handle.position = len(read_data) | ||
| return read_data | ||
| else: | ||
| data = read_data[handle.position:handle.position + size] | ||
| handle.position += size | ||
| return data | ||
|
|
||
| handle.tell.side_effect = tell | ||
| handle.len = len(read_data) | ||
| handle.read.side_effect = read | ||
| del handle.getvalue | ||
| return mock |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange that this test worked before...