Theiaz
Theiaz

Reputation: 784

REST - Addressability of resources (Composition, Collection)

I'm designing a REST-API and currently I'm stuck with the following question. For example I got the following structure:

/foods/fruits/{fruit}
/foods/meat/{meat}

for example:

/foods/fruits/apple
/foods/fruits/pineapple
/foods/meat/chicken

Each of the endpoints is unique (only one apple, one chicken), but different sorts of fruits can exist (e.g. pineapple). However my service needs to return either

With this design my resource is addressable about 3 URIs (composition, collection, endpoint), but it should be a maxima of 2. It this a restful design?

Upvotes: 0

Views: 530

Answers (2)

Chris Simon
Chris Simon

Reputation: 6505

With this design my ressource is addressable about 3 URIs (composition, collection, endpoint), but it should be a maxima of 2. It this a restful design?

Could you provide a reference for the guideline that it should be a maxima of 2?

But either way - those three URIs are not 3 different URIs for the same resource, they are three different resources:

  1. the food resource
  2. a collection of foods of a particular type
  3. a particular type of food

While the earlier resources can be considered to be composed of the others, it does not mean that the finest grain (the particular type, e.g. apple) is addressable via 3 URIs.

If you are using a hypermedia format such as HAL your composition representation could utilised embedded resources, which clearly illustrates that the contained resources are also available directly via their own endpoint.

Upvotes: 1

Alex Marculescu
Alex Marculescu

Reputation: 5770

Looking at the Richardson Maturity Model, you could have a generic Food resource and endpoint, with properties like type and name, and then your requirements would be accessible like so:

  • GET /api/food?name=apple
  • GET /api/food?type=fruit
  • GET /api/food

Upvotes: 2

Related Questions