-
Notifications
You must be signed in to change notification settings - Fork 27
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
fix: add correct support for compressing file-like objects #174
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
17039a3
test: update unit tests to demostrate the issue
pyrooka 35216f3
fix: add correct support for compressing file-like objects
pyrooka 245b72b
chore: fix typos
pyrooka bdc27d3
chore: move all compression logic to helper class
pyrooka ae5f618
test: simulate urllib3 chunk reading and validate decompressions
pyrooka 1363692
test: cover reading from closed source
pyrooka 8251974
fix: handle reading from closed source
pyrooka 7920e40
chore: change base class
pyrooka de4ef28
Merge branch 'main' into nb/fix-file-compression
pyrooka 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 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 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 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 |
---|---|---|
|
@@ -647,6 +647,34 @@ def test_gzip_compression(): | |
assert prepped['headers'].get('content-encoding') == 'gzip' | ||
|
||
|
||
def test_gzip_compression_file_input(): | ||
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. Had to put the new test cases into a separate function to avoid the |
||
service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) | ||
service.set_enable_gzip_compression(True) | ||
|
||
# Should return file-like object with the compressed data when compression is on | ||
# and the input is a file, opened for reading in binary mode. | ||
raw_data = b'rawdata' | ||
with tempfile.TemporaryFile(mode='w+b') as tmp_file: | ||
tmp_file.write(raw_data) | ||
tmp_file.seek(0) | ||
|
||
prepped = service.prepare_request('GET', url='', data=tmp_file) | ||
assert prepped['data'].read() == gzip.compress(raw_data) | ||
assert prepped['headers'].get('content-encoding') == 'gzip' | ||
|
||
# Should return file-like object with the compressed data when compression is on | ||
# and the input is a file, opened for reading in text mode. | ||
assert service.get_enable_gzip_compression() | ||
text_data = 'textata' | ||
with tempfile.TemporaryFile(mode='w+') as tmp_file: | ||
tmp_file.write(text_data) | ||
tmp_file.seek(0) | ||
|
||
prepped = service.prepare_request('GET', url='', data=tmp_file) | ||
assert prepped['data'].read() == gzip.compress(text_data.encode()) | ||
assert prepped['headers'].get('content-encoding') == 'gzip' | ||
|
||
|
||
def test_gzip_compression_external(): | ||
# Should set gzip compression from external config | ||
file_path = os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-gzip.env') | ||
|
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.
I use this if-else format to avoid
too-many-branches
linter error then I renamed the variable toraw
to do not exceed the max row length...:)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.
I think it would add some value if you were to add some additional function to the GzipStream() ctor so that you could also pass in a non-IOBase-type value for
raw
and it would do the right thing (in that case, perhaps just wrapraw
with an appropriate IOBase-type class that could stream the bytes ofraw
... in other words, beef up GzipStream just a bit so it can handle file-like objects and static strings/buffers).This way, the details of how a particular requestBody is gzip-encoded is hidden inside GzipStream and not exposed up in this BaseService method AND we also get a stream-based zip compression being performed which might help with large JSON requestBodies.
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.
FYI when I was originally investigating this in our SDK I noticed from
gzip.compress()
docsSo there might not be much value in trying to be fully stream-based.
It also says:
but it isn't immediately obvious to me whether that delegation makes any difference to the streaming behaviour, but
mtime
defaults to current time so it won't happen at present anyway (nb.mtime
only avialable from3.8
).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.
In my opinion it would make the code cleaner (to have all compression related steps in the helper class) and although the performance is improved in 3.11, the memory usage could still be a problem when the file is large, right? I think that's Phil's main point.
We still support 3.7 so that's not an option - at least for the
gzip.compress
function. From my understanding themtime
is only used during the decompressing so - I think - the streaming shouldn't affect the result.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.
My understanding is that in 3.11 the entire contents will be in memory in gzip anyway. So my point was that might not be worth making changes only for that reason, but as you say there are other benefits.