7

I have two different apps in my django project. One is "comment" and an other one is "files". A comment might save some file attached to it.

The current way of creating a comment with attachments is by making two API calls. First one creates an actual comment and replies with the comment ID which serves as foreign key for the Files. Then for each file, a new request is made with the comment ID.

Please note that file is a generic app, that can be used with other apps too. What is the cleanest way of making this into one API call?

I want to have this as a single API call because I am in a situation where I need to send user an email with all the files as attachment when a comment is made. I know Queueing is the ideal way to do it. But I don't have the liberty to add queing to our stack now. So this was the only way I could think of.

3
  • Why do you want to have this as a single API call?
    – CodeART
    Oct 7, 2013 at 7:16
  • I am in a situation where I need to send user an email with all the files as attachment when a comment is made. I know Queueing is the ideal way to do it. But I dont have the liberty to add queing to our stack now. So this was the only way I could think of.
    – dhilipsiva
    Oct 7, 2013 at 7:27
  • Do you have something like a Core-app? Why not defining an endpoint there, where you import needed packages from both apps and do your all-in-one-api-call there? Jul 16, 2017 at 9:28

1 Answer 1

1

One straight forward way to solve this without having to add queuing is to reverse the order of method calls. Rather than creating comments than attaching the files, you should create the files first, which returns file id, then send the file ids in the create comments request in the attachments fields. Doing it this way also allows a single file to be attached to multiple comments, without reuploading, or for the server to automatically detect duplicate uploads and return the same file id.

Another possible way to do this is to add a status field. When you first create a comment, it should be in a Draft status, which doesn't send emails. When you've finished attaching the files, send another request to transition the comment status to Published, which should send emails with the attachments up to that point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.