- Notifications
You must be signed in to change notification settings - Fork11
Add multipart support to the CreateEtchPacket mutation#41
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| defget_files_for_payload(self)->Tuple[List[AttachableEtchFile],List[Any]]: | ||
| """Scan through and gather files for use in multipart requests. | ||
| During the process, files will be replaced with a dummy `Path` | ||
| instance as `Callable` instances and likely their returns of some | ||
| sort of IO reader is not serializable and not easily dealt with in | ||
| pydantic (the underlying model system for the types used in this | ||
| library). Closer to the actual request, when the multipart payload is | ||
| being constructed, the dummy instances will be replaced by their actual | ||
| file instances. | ||
| :return: Tuple containing a list of uploadable file types, and a list | ||
| of actual file references | ||
| :rtype: Tuple[List, List] | ||
| """ | ||
| contains_uploads=any([isinstance(f,DocumentUpload)forfinself.files]) | ||
| ifnotcontains_uploads: | ||
| returnself.files, [] | ||
| files= [] | ||
| foridx,finenumerate(self.files): | ||
| ifnotisinstance(f,DocumentUpload): | ||
| continue | ||
| ifgetattr(f.Config,"contains_uploads",False): | ||
| attr_name=getattr(f.Config,"contains_uploads") | ||
| upload=getattr(f,attr_name) | ||
| ifnotisinstance(upload,Base64Upload): | ||
| files.append(upload) | ||
| setattr(f,attr_name,Path()) | ||
| returnself.files,files |
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.
Admittedly, this is a little funky, but it's the easiest way I can think of at the moment. Because of the multipart spec, we need to a way to check for files, but the file attribute also has to be nulled/None-ed out when we serialize this down to JSON. Because of this, I'm just using aPath instance as a marker since a multipart file can be a file path or function.
| @@ -0,0 +1,34 @@ | |||
| frompython_anvil.multipart_helpersimportget_extractable_files_from_payload | |||
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.
Adding more tests shortly..
| files=get_multipart_payload(mutation) | ||
| res=self.mutate_multipart(files,**kwargs) |
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.
A few things:
- This looks like it will always call
mutatate_multipart, even if there are no binary uploads, and therfore no reason to usemulti-part. Is that right? Maybe only usemulti-partwhen necessary? - It feels like this decision to use multipart or not, and the few lines hre of what to do in that case are better abstracted away inside the internals of
self.mutatesomehow. That way anything that is uploading binary in a mutation will just have that stuff handled implicitly.
| returnself.file_payloads | ||
| defcreate_payload(self)->CreateEtchPacketPayload: | ||
| defget_files_for_payload(self)->Tuple[List[AttachableEtchFile],List[Any]]: |
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.
This also feels like something that belongs in a more shared, non-CreateEtchPacket specific spot.
This seem seems akin tohttps://www.npmjs.com/package/extract-files
Our node-anvil library has a decent pattern to follow where it figures out if there files to maphere and then makes a multi-part request if necessaryhere.
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 generalextract-files-like functionality is in the function here:https://github.com/anvilco/python-anvil/pull/41/files#diff-615974429dd7f7e34ce2e08e0012206ae6f47d86bcfc9338e6f72546e76436beR14
In this first pass, though, this is mainly to add multipart support oncreateEtchPacket since it's the only one that has underlying model support. Will have a more generic multipart mutation path after this PR.
Adds multipart support for the
createEtchPacketmutation via the spec here:https://github.com/jaydenseric/graphql-multipart-request-spec