Let’s say there is a SINGLE mobile application which receives various types of notifications (like WhatsApp notifications, Facebook Messenger notifications ,etc). What would be a better REST API structure for this?
/users/[email protected]/notifications //Gives all the notifications
There is a confusion between the below two formats on how .
/users/[email protected]/notifications?category=whatsapp,facebook //Gives all the notifications
vs
/users/[email protected]/notifications/whatsapp //Gives only whatsapp notification
/users/[email protected]/notifications/facebook //Gives only facebook notification
To access an individual notification resource
/users/[email protected]/notifications/facebook/{notification-id}
vs
/users/[email protected]/notifications/{notification-id}
2
Answers
If a resource has a unique ID then it should be directly accessible from that, so in my opinion
Would make most sense. In terms of filtering, this is probably more about preference than anything. Here’s what I think would be the most idiomatic approach
It really depends on how you define a
notification
resource and the relation with itscategory
type (whatsapp
,facebook
…).Non
category
dependentIf the structure of a notification is not dependent on its category, then you want to access it without any category context:
And you can use the category as a filter to a collection of notifications:
category
dependentOtherwise, if a notification is structurally dependent on its category (e.g., if you want to define different actions when you deal with
whatsapp
notifications than when you deal withfacebook
notifications), then you might want to distinguish a notification according to its category:In this case, you could have:
That define 2 different notifications (although it uses the same identifier).
Now requesting a collection of this kind of notifications is a bit different than the previous “non category dependent” case.
If you only want to have
whatsapp
notifications then simply call the category resource does the job:But if you want to search on different categories, then you cannot apply your request to a specific category resource. Indeed, it makes no sense to ask for
facebook
notifications when you deal withwhatsapp
ones:One solution would be to make as many requests as there are categories requested:
But you will have to merge your results later.
Another solution would be to apply your query directly from
But the result will be different than the “non category dependent” case. Indeed, you won’t be able to directly have your list of notifications, but a list of categories to access to your list of notifications.