Understanding REST through an Example

My only real exposition of REST ideas went through Ruby on Rails RESTful . This suited me well for CRUD-based applications that I created with Rails, but therefore my understanding of RESTfulness is somewhat limited.


Let's say we have a finite set of elements, each of which has a unique identifier and a number of properties, such as color, shape and size (which may be undefined for some elements).

Elements can be used by a client for a certain period of time, but each element can be used by only one client at a time. Access to items is regulated by the server. Clients may request temporary use of certain items from the server.

Typically, customers will only be interested in gaining access to a number of elements with certain properties, and not access to certain elements.

When a client requests the use of several elements, the server responds with a list of identifiers matching the request, or with a response stating that the requested elements are currently unavailable or do not exist.

The client can fulfill the following types of requests:

  • Tell me how many elements of the green triangle there are (total / available).
  • Let me use 200 large red items.
  • I ended up with points 21, 23, 23.
  • Add 100 new red square elements.
  • Remove 50 small green elements.
  • Change all the large yellow pentagonal objects so that they are blue.

The above toy example is similar to the resource allocation problem that I had to deal with recently. How should I think ABOUT THIS?

+6
rest
source share
3 answers

The trick to understanding is to think about the problems of focusing on nouns instead of verbs.

In the rest of the world, verbs are all β€œgiven”, and nouns become infinitely flexible. In the world of soap or rock, verbs are infinitely flexible. Limit your thinking to block verbs, and then see what nouns you need to solve your problem within the limitations that you have.

This is exactly what darrel did in the answer above - he composed a new lock noun that will satisfy your limitations and then configure access to them to achieve what you wanted.

Some of your questions were related to search or filter - for those who think about GET, about types of resources, passing query parameters to limit or filter results.

+6
source share

If resource locking is indeed a problem for the domain in your scenario, I would recommend simulating the locking as a resource.

Here are some suggestions on how you might fulfill your requests.

GET /Triangle/Green/Count GET /Triangle/Green/Available POST /Item/Red/Large/Locks?quantity=200 DELETE /Item/21/Lock DELETE /Item/23/Lock DELETE /Item/25/Lock POST /Square/Red?quantity=100 DELETE /Item/Green/Small?quantity=100 POST /Pentagon/Blue?url=/Pentagon/Yellow 

Having said that, the definition of URLs is somewhat irrelevant. Designing the types of your materials with the appropriate links is an important part of RESTful design.

+5
source share

Read this for basic training ... I'm stuck in What is REST ... !!!

A content management system may contain a set of articles. Two sources are implied here. Firstly, there are separate articles. Each source of resources. Theres also a second resource: a collection of articles.

To get a list of all the articles, we can send an HTTP GET request to this collection, say, along the path / articles. To get the contents of a single resource, we need to identify it. The Rails path would have to give its primary key value (i.e. its identifier). Again wed issued a GET request, this time against URL / articles / 1. So far, it all looks pretty familiar. But what happens when we want to add an article to our collection?

In non-RESTful applications, perhaps come up with some kind of action with a verb phrase as the name: articles / add_article / 1. In the REST world, it was not proposed to do this: it was supposed to tell resources what to do using a standard set of verbs. To create a new article in our collection using REST, use the HTTP POST request directed to the / articles path with the message data containing the added article. Yes, the same path we used to get the list of articles: if you issue a GET, it responds with a list, and if you do a POST, it adds a new article to the collection.

Take one more step. We have already seen that you can retrieve the contents of an article, issue a GET request to the path / articles / 1. To update this article, you invoke an HTTP PUT request with the same URL. And to remove it, you can send an HTTP DELETE request using the same URL again.

0
source share

All Articles