-
Notifications
You must be signed in to change notification settings - Fork 282
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
feat: Add support for uploading files as a list #403
feat: Add support for uploading files as a list #403
Conversation
This could also be a candidate for a back-port to the 0.9 version as well. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #403 +/- ##
==========================================
+ Coverage 85.07% 88.86% +3.79%
==========================================
Files 9 9
Lines 469 476 +7
Branches 47 49 +2
==========================================
+ Hits 399 423 +24
+ Misses 65 51 -14
+ Partials 5 2 -3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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 would also add a unit test to make sure files get closed for both cases list or tuple.
Thanks!
files[k].append(val) | ||
else: | ||
files[k] = val | ||
for k in request.files.keys(): |
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.
you also had to fix httpbin?
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.
Yes, as this only returned one item the way it was written. According to what I found out, you need to use the getlist(key) method to get all files for one key.
files_descriptor_to_close = filter( | ||
is_file_descriptor, list(files.values()) + [data] | ||
) | ||
|
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.
at this point I would extract the logic to close file descriptor in a dedicated function and try to cleanup this part
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 refactored the closing of file descriptors into its own static method.
54da020
to
d099ade
Compare
Added verification to the Robot Framework tests to verify that the file descriptors are closed for both use cases. |
Uploading a list of files with the same key is supported by the Requests library. This allows the receiving server to accept a list of files for the same key, i.e. accepting any number of files. Issue: MarketSquare#401
d099ade
to
0fadd9f
Compare
there was already a unit test for check file closing |
@@ -176,7 +192,7 @@ def session_less_get( | |||
| ``json`` | A JSON serializable Python object to send in the body of the request. | | |||
| ``headers`` | Dictionary of HTTP Headers to send with the request. | | |||
| ``cookies`` | Dict or CookieJar object to send with the request. | | |||
| ``files`` | Dictionary of file-like-objects (or ``{'name': file-tuple}``) for multipart encoding upload. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers to add for the file. | | |||
| ``files`` | Dictionary of file-like-objects (or ``{'name': file-tuple}``) for multipart encoding upload. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers to add for the file. List or tuple of ``('key': file-tuple)`` allows uploading multiple files with the same key, resulting in a list of files on the receiving end. | |
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.
the strange thing is that even the official documentation don't mention files as list or tuple..
https://requests.readthedocs.io/en/latest/api/#requests.request
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.
Yes, I know that they don't mention it. But the support is there and I don't know how long it has been there.
Well, I did not dig too deep to find that. |
I rebased on master. I don't think that my changes is what is causing the test to fail. |
no it's not your commit, there are still some tests that use a web version of httpbin, sometimes they throttle the requests... |
Yeah, it is the same test that also fails locally for me. |
@lucagiove Was there anything I still needed to do with this? How do we rerun the builds? |
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.
No I would have added also the unit test but mandatory.
I'll try to have the pipeline running.
But didn't you say that there already are a unit test for the closing of file descriptors? Or was there some other unit test that you wanted to run? |
Yep but not with the specific input of a list. |
Uploading a list of files with the same key is supported by the Requests library. This allows the receiving server to accept a list of files for the same key, i.e. accepting any number of files.
Issue: #401