Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modeling resource relationships with RESTful APIs

Tags:

rest

When designing a RESTful API should resources which are dependent on others be modeled as sub-uris or should they simply reference each other?

E.g. presuming a door is always dependent on a house then

/house/73/door/1

or

/house/73
/door/1044

where house and door include references to each other?

Most RESTful APIs I have found are quite flat so I would value references to any which do have more complex relationship dependencies.

Regards

like image 928
Howard May Avatar asked Sep 24 '10 10:09

Howard May


People also ask

What relationship should a model have with an API interface?

Link objects are used to express structural relationships in the API. So for example, the top-level collections, singleton resources and sub-collections (including actions) are all referenced using link objects. Object links are used to express semantic relationships from the application data model.

What is resource in RESTful API?

Resources are the basic building block of a RESTful service. Examples of a resource from an online book store application include a book, an order from a store, and a collection of users. Resources are addressable by URLs and HTTP methods can perform operations on resources.

How do you interact with RESTful APIs?

Step #1 – Enter the URL of the API in the textbox of the tool. Step #2 – Select the HTTP method used for this API (GET, POST, PATCH, etc). Step #3 – Enter any headers if they are required in the Headers textbox. Step #4 – Pass the request body of the API in a key-value pair.

What is model in REST API?

The JSON representation of the Adult Social Care REST API is modeled as a number of structs in IBM Rational Software Architect. Modeling the REST facade. REST API resource methods are implemented by Cúram modeled facade operations.


1 Answers

In UML terms, if the relationship is that of Aggregation, then you use a flat hierarchy with links between things, whereas if the relationship is that of Composition (i.e., the lifetime of a door is strictly bounded by the lifetime of a house) you use sub-resources.

I'm not suggesting drawing a UML diagram! But it does help to have in mind that distinction. (You could also model the Aggregation case by having sub-resources that are just redirect to the real ones; redirects are RESTful. OTOH, I don't actually like doing that; I prefer to make any relationships explicit and to keep the number of redirects down.)

like image 200
Donal Fellows Avatar answered Sep 29 '22 19:09

Donal Fellows