Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A RESTful singleton resource

Tags:

rest

singleton

I'm coming from the RPC world but currently investigating if using REST is a good idea for my project. As for as I understand from Wikipedia the basic idea of RESTful services is to provide access to collections and their individual elements.

In my case the server would be a measuring instrument. I must be able to start, stop, and pause the measurement routine, and read the data at any time.

Currently, I'm considering the following:

  • POST /measure (start measurement, this continues until stopped by the user)
  • PUT /measure pause=true/false (pause/unpause)
  • DELETE /measure (stop)
  • GET /measure (get measurement data)

However, I'm not sure if this fits the REST model, since I don't really work with collections or elements here.

My question: How would I access a singleton resource and do the start/stop requests to the server break the RESTful stateless constraint?

like image 806
Sundae Avatar asked Dec 08 '11 16:12

Sundae


People also ask

What is singleton in REST API?

There are some resources that can only have one instance. These resources are called singletons and the resource collection name is in the singular form. For example: /system.

What is a singleton resource?

Singleton resources represent a single resource instance, rather than a resource collection. This pattern is useful when you do not require a collection of resources for your API. Let's examine this pattern and how it can be used to simplify an API design.

What is a RESTful resource?

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.

Which HTTP method removes a singleton resource?

DELETE means you remove a resource, and since a resource is identified by a URL, DELETE /measure implies that /measure no longer exists.


1 Answers

Not RESTful

No, your approach isn't RESTful, because if I understand the workflow, you would delete the resource to stop the measurement and then get the resource to read out the final result. But deleting a resource implies that there would be nothing left to GET.

The fact that your resource is a singleton isn't a problem at all. The problem lies in the way you're mapping verbs and state.

Your description is a bit abstract, so let's be a bit more concrete: let's assume that the instrument in question measures the angular velocity of a fly wheel in radians/sec. This instrument has some cost associated with measurement, so the client needs to be able to disable measurement for some periods of time as a cost-saving measure. If this is roughly analogous to your scenario, then the exposition below should be applicable to your scenario.

Verbs

Now, let's review your verbs.

GET returns a representation of a resource. So when you GET /measure, it should return some data that represents the current measurement.

PUT creates or updates a specific, named resource. The resource is named by its URL. So PUT /measure implies that you're updating the state of a resource called /measure, or creating that resource if it doesn't already exist. In your case, the instrument value is read-only: we can't write a radian/sec value to the instrument. But the paused/active state of the instrument is mutable, so PUT /measure should include a body that modifies the state of the instrument. You could use a lot of different representations here, but one simple approach would be a request body like active=true or active=false to indicate what thew instrument's new state should be.

POST is similar to PUT, except that the client does not specify the name of the resource that should be created or updated. In a different API, for example, the client might POST /articles to create a new a article. The server would create a resource and give it a name like /articles/1234 and then it would tell the client about this new name by returning a 201 CREATED HTTP code and adding a Location: /articles/1234 header to tell the client where the new resource is. In your scenario, POST isn't a meaningful verb because you always know what the name of your singleton resource is.

DELETE means you remove a resource, and since a resource is identified by a URL, DELETE /measure implies that /measure no longer exists. A subsequent GET /measure should return either 404 NOT FOUND or 410 GONE. In your case, the client can't actually destroy the instrument, so DELETE isn't meaningful a meaningful verb.

Conclusion

So in sum, a RESTful design for your service would be to have PUT /measure with a request body that tells the instrument whether it should be active or not active (paused) and GET /measure to read the current measurement. If you GET /measure on a paused instrument, you should probably return a 409 CONFLICT HTTP status. Your service shouldn't use POST or DELETE at all.

like image 181
Mark E. Haase Avatar answered Oct 30 '22 09:10

Mark E. Haase