Skip to content
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

single input multiple files enabled #891

Closed

Conversation

tegkhanna
Copy link

@tegkhanna tegkhanna commented Mar 31, 2020

Added support for multiple files through a single input!

Fixes #777

@tegkhanna tegkhanna changed the title single input multiple files enabled single input multiple files enabled #777 Mar 31, 2020
@tegkhanna tegkhanna changed the title single input multiple files enabled #777 single input multiple files enabled Fixes #777 Mar 31, 2020
@tegkhanna
Copy link
Author

Requesting review @florimondmanca

@demonno
Copy link

demonno commented Apr 8, 2020

I am having same issue, and saw there is some progress already, based on discussion here #777 problem is already clear and implementation suggestion is also made.

Expecting to get similar API to requests post-multiple-multipart-encoded-files

>>> url = 'https://httpbin.org/post'
>>> multiple_files = [
...     ('images', ('foo.png', open('foo.png', 'rb'), 'image/png')),
...     ('images', ('bar.png', open('bar.png', 'rb'), 'image/png'))]
>>> r = httpx.post(url, files=multiple_files)
>>> r.text

@tegkhanna your suggestion in this Pull Request is different from above example.

Why don't not you follow this suggestion, did it have any issues?

Copy link
Member

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tegkhanna! Thanks for submitting this PR. Overall the code looks great, but not sure it's what we want in terms of API.

As @demonno mentioned, and as suggested by Tom in the issue thread, we should probably go for "list of (fieldname, value) items" (instead of "dict of {fieldname: }" as currently proposed here), because it is is line with what Requests does.

So the line that needs changing is probably for name, value in files.items(): files would now refer to either a dict, or a list of (name, value) items. So probably this…

fileitems = files.items() if isinstance(files, dict) else files
for name, value in fileitems:
    ...

httpx/_content_streams.py Outdated Show resolved Hide resolved
@florimondmanca florimondmanca changed the title single input multiple files enabled Fixes #777 single input multiple files enabled Apr 8, 2020
@florimondmanca
Copy link
Member

Went ahead and moved the Fixes #777 note to the PR description, that way #777 is automatically linked. :-)

@tegkhanna
Copy link
Author

@florimondmanca Hi! I will commit the changes soon :) Actually, I was confused that should I add the list of tuples support as it was different from the core API. Should I add the support for data = list of (fieldname, value) item as well?

Went ahead and moved the Fixes #777 note to the PR description, that way #777 is automatically linked. :-)

Thanks for that. I thought it gets linked by adding to the Fixes keyword to the title!

@florimondmanca
Copy link
Member

florimondmanca commented Apr 8, 2020

@tegkhanna

Should I add the support for data = list of (fieldname, value) item as well?

I don't think so no, at least not here.

Currently data only supports either string-like or a dict:

RequestData = typing.Union[
    dict, str, bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]
]

Not obvious yet that we need anything else, but if we do then in any case it should be a different issue/PR (as this one is about files).

@tegkhanna
Copy link
Author

@florimondmanca One last question, should I then drop the support for multiple files if they are given as part of a dict {fieldName : [list of file objects]} or should I keep it as well?

@florimondmanca
Copy link
Member

Yes, let’s drop it in favor of the list of tuple style. :)

Copy link
Member

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant! One factoring suggestion.

Also, it occurs to me I was mistaken when I wrote #891 (comment): we probably do want to support the "list of (field, value) tuples" style for data too.

Because a/ that's supported by Requests, and b/ this is the URL-encoded data case, and it makes sense to have multiple query params with the same field name. Tom's comment in #777 (comment) was actually exactly about also supporting this style for data, alongside files

This PR is almost ready so it's probably okay to submit the data part as a separate PR. If it turns out they'd overlap too much then let's put everything in here…

httpx/_content_streams.py Outdated Show resolved Hide resolved
@tegkhanna
Copy link
Author

@florimondmanca Sure I will make another PR for the data part! I have added the required change for filetype reusability to this PR :)

Copy link
Member

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@tegkhanna
Copy link
Author

@florimondmanca Should I add the data list changes to this PR only? Because a separate PR will cause conflicts if the changes are based on the master

@florimondmanca
Copy link
Member

Yes I suppose so... sure, you can add those changes here if that helps. :)

@tegkhanna
Copy link
Author

The PR is complete. Files and data both now accepts a list of tuples along with a dict.

@florimondmanca
Copy link
Member

Closing this one in favor of #1032, thank you so much for the time you've put into this, @tegkhanna!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for multiple files per POST field
3 participants