User data for elements in REST api

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

For example, suppose there are item resources that can be saved. The list of items can be accessed:

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

What returns

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

Each item can be saved by the user ( https://myservice.com/api/user/{id} ), and a list of these favorites will be available at:

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

This whole installation is not stateless; however, there may be hundreds of favorites, and a complete list may not be necessary.

Q: By supporting a stand-alone system, what is the best way to combine receiving an item with specific user data?

i.e. Reasonable or believable to get a list of user-specific items:

 https://myservice.com/api/items?user={id} { { 'name': 'name 1', 'isFavourite':true }, { 'name': 'name 2', 'isFavourite':false }, } 
+5
source share
1 answer

At the same time, supporting a stateless system, what is the best way to combine the receipt of an element with specific user data?

Think about how you do it on a website. This is how you do it in REST; you just need to make it machine readable.

This whole installation is not stateless; however, there may be hundreds of favorites, and a complete list may not be necessary.

In this case, you can distribute the list across several resources, and then use paging to help the consumer find the returned resource next / previous page of the list.

Whether it is reasonable or plausible to get a list of items specific to the user.

Oh sure. The main idea that should be recognized 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 web resources into actions / requests in your application.

0
source

Source: https://habr.com/ru/post/1266214/


All Articles