简体   繁体   中英

Addressing collection items in multiple ways in REST API design

We have a resource collection in our API design that can be addressed in multiple ways. For example say these are books.

Name ISBN ISSN Color
Foo 1 2 blue
Bar 4 5 black
Baz 7 8 brown

The books can be addressed uniquely via ISBN, ISSN and the name and color are non-unique indexs as well. How can we model a REST API that allows PATCH operations on the books collection/items that can be specified in multiple ways?

For example we may wish the clients to address the books in any of the following ways

  1. by book name
  2. by book color
  3. by book ISBN
  4. by book name and color

How can I do this without creating multiple endpoints?

I would make a distinction between identifiers and filters. ISBN and ISSN are identifiers, because they are unique. Everything non-unique is a filter. Identifiers deserve their own endpoints. That's what ReST URLs do: uniquely identify resources.

/books/isbn/{id}
/books/issn/{id}

Both of these are guaranteed to return a single book on success. The remaining fields can be combined into a third endpoint.

/books?name=foo&color=blue&author=you

This one returns a collection.

In terms of PATCHing, all three of these can call the same service method to do that. The third endpoint would iterate over its results to invoke the PATCHing logic repeatedly.

This does mean the number of endpoints grows if you add primary keys. I think that is still ReSTful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM