6

In a REST api what is the most efficient way to handle user specific data for items?

For example, let's say there are item resources that can be favourited. A list of items can be accessed by:

https://myservice.com/api/items (Full list)
https://myservice.com/api/items/{id} (Single item)

Which returns

{
  { 'name': 'name 1' },
  { 'name': 'name 2' },
}

Each item can be favourited by a user (https://myservice.com/api/user/{id}) and a list of these favourites would be available at:

https://myservice.com/api/user/{id}/favorites

This whole setup is stateless; however, there may be hundreds of favourites and retrieving a full list may not be needed.

Q: Whilst maintaining a stateless system what is the best way to combine getting an item with the user specific data?

i.e. Is it sensible, or plausible, to get a user-specific list of items:

https://myservice.com/api/items?user={id}
{
  { 'name': 'name 1', 'isFavourite':true },
  { 'name': 'name 2', 'isFavourite':false },
}
Itzalive
  • 370
  • 4
  • 14

1 Answers1

0

Whilst maintaining a stateless system what is the best way to combine getting an item with the user specific data?

Think about how you would do it on a web site. That's how you do it in REST; you just need to make it machine readable.

This whole setup is stateless; however, there may be hundreds of favourites and retrieving a full list may not be needed.

In which case, you can distribute the list across multiple resources, and then use paging to assist the consumer in finding the resource that returns the next/previous page of the list.

Is it sensible, or plausible, to get a user-specific list of items

Yes, of course. The key idea to recognize is that your REST API is part of your integration domain; you pretend to be a site full of "web pages", and translate the manipulation of the web resources into actions/queries in your application.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91