How to make API endpoints for stepwise continuous upload use case?

Viewed 105

I have a good experience with Android Frontend REST API calls, but I am very new to Backend and learning Django Framework so I need help for designing the below API endpoints.

For better understanding I have uploaded a video over here https://youtu.be/z87Hz1uHrYY.

This is the solution which I was thinking of doing,

    ) HTTP-Method: POST
      EndPoint URL:  /recipe/ 
      Request Params: {"name":"Pizza"} "image": pizza.png 
      Response Params: {"id":"123xyz"} // unique id
    
    ) HTTP-Method PATCH 
      EndPoint URL: /recipe/123xyz/ 
      Request Params: {"serving":2, "difficulty": "m", "prep_time": 80} 
      Response Params:   {"id":"123xyz", "serving":2, "difficulty": "m", "prep_time": 80} 
    
    ) HTTP-Method: PATCH 
      EndPoint URL: /recipe/123xyz/ingredients/ 
      Request Params: [{"ingredient":”rice”, “amount”: “1/2”, “unit”: “g”},{"ingredient":”water”, “amount”: “1/2”, “unit”: “ml”}] 
      Response Params: {"id":"123xyz", "serving":2, "difficulty": "m", "prep_time": 80, “ingredients”:  [{"ingredient":”rice”, “amount”: “1/2”, “unit”: “g”, “index”:1},{"ingredient":”water”, “amount”: “1/2”, “unit”: “ml”, “index”:2}] } 
    
    ) HTTP-Method: PATCH 
      EndPoint URL: /recipe/123xyz/steps/ 
      Request Params: [{"description":”abc”, “image”: “s3//step1.png”, "index": 1},{"description":”xyz”, “video”: “s3//step2.mp4”, "index": 2}] 
      Response Params: {"id":"123xyz", "serving":2, "difficulty": "m", "prep_time": 80, “ingredients”:[{"ingredient":”rice”, “amount”: “1/2”, “unit”: “g”, “index”:1},{"ingredient":”water”, “amount”: “1/2”, “unit”: “ml”, “index”:2}], 
"steps":[{"description":”abc”, “image”: “s3//step1.png”, "index": 1},{"description":”xyz”, “video”: “s3//step2.mp4”, "index": 2}]}

These are the API breakdowns that I can think of with the questions below.

) How to handle reordering of ingredients and steps?

) When to upload image or video, first upload file and get the url of s3 and then make /recipe/123xyz/steps/ api call. Or upload file along with /recipe/123xyz/steps/ API call?

Feel free to correct me if I am wrong and suggest a better API design approach for this use case.

1 Answers

Let's try!

How to handle reordering of ingredients and steps?

You need to have an order_key field in your Ingredient model. Feel free to make it as PositiveSmallIntegerField

When you what to reorder your ingredients in a needed step you need to send a request to the API backend with something like this:

POST /steps/{step_id}/ingredients/reorder/

{
    "ingrediends": {
        "id_1": 1,
        "id_2": 2
    }
}

Handle this payload in your view and reorder your queryset by the order_key field when fetching the data.

When to upload image or video...

Here you can have a lot of strategies. One of them:

  1. You need to know the domain of your CDN (CloudFront + S3 Bucket). For example mycdn.aws.com. Send it with system info to the Android client in the initial calls, or successful auth, other initial steps. So your app is aware of the CDN domain.

  2. Upload your file to the backend. And you will know the final URL if you know where it will be saved. For example:

  • a file name is uuid.avi
  • the CDN domain is mycdn.aws.com,
  • target bucket is video-files

so you even don't need to wait for something from the backend - just call https://mycdn.aws.com/video-files/uuid.avi after uploading.

It's better to upload something as a separate API call to the files' own endpoint. You will be able to manage such requests easily (to show a user error message if the file is wrong or the endpoint isn't working, etc.)

Also uploading files to the backend instead of directly from the app client will be more preferred because you don't need to have the app release if something changed. Something will be changed 100% You need to split the responsibility - the app is taking images and videos, backend knows where to save all these staffs.

Related