Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding REST through an example

Tags:

rest

My only real exposure to the ideas of REST has been through Ruby on Rails' RESTful routing. This has suited me well for the kind of CRUD-based applications I have built with Rails, but consequently my understanding of RESTfulness is somewhat limited.


Let's say we have a finite collection of Items, each of which has a unique ID, and a number of properties, such as colour, shape, and size (which might be undefined for some Items).

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

Usually, clients will only be interested in getting access to a number of Items with particular properties, rather than getting access to specific Items.

When a client requests use of a number of Items, the server responds with a list of IDs corresponding to the request, or with a response that says that the requested Items are not currently available or do not exist.

A client can make the following kinds of request:

  • Tell me how many green triangle Items there are (in total/available).
  • Give me use of 200 large red Items.
  • I have finished with Items 21, 23, 23.
  • Add 100 new red square Items.
  • Delete 50 small green Items.
  • Modify all big yellow pentagon Items to be blue.

The toy example above is like a resource allocation problem I have had to deal with recently. How should I go about thinking about it RESTfully?

like image 710
grifaton Avatar asked May 01 '10 16:05

grifaton


People also ask

How can I learn more about REST APIs?

Learning about APIs can be challenging and daunting to new developers, but to help you keep growing and progressing in your career, Postman has crafted a new public REST API collection, where you can expand your knowledge and understanding of a variety of REST APIs. At Postman, we strive to make APIs more accessible and easy to work with.

What is the difference between rest and a resource?

A resource can be any information that could be named, such as a document or image, a collection of other resources, a non-virtual object, and more. Meanwhile, REST uses a resource identifier to recognize the specific resource involved in an interaction between components.

What is the meaning of a RESTful API?

REST stands for REpresentational State Transfer. It means when a RESTful API is called, the server will transfer to the client a representation of the state of the requested resource.

What are REST constraints and why are they important?

The REST set of constraints will make your APIs easier to use and also easier to discover, meaning a developer who is just starting to use your APIs will have an easier time learning how to do so. REST stands for RE presentational S tate T ransfer.


1 Answers

The trick to understanding is to think about the problems from a focus on nouns instead of verbs.

In the rest world, the verbs are all "preset" and the nouns become infinitely flexible. In a soap or roc world, the verbs are infinitely flexible. Restrict your thinking to locking down the verbs and then see what nouns you need to solve your problem within the constraints you have.

That is exactly what darrel did in the answer above - he made up a new noun for a lock that would satisfy your constraints and then set up access to them to achieve what you wanted.

Some of your questions were search or filter related - for those think of GET against the types of resources, passing in query parameters to restrict or filter the results.

like image 128
heckj Avatar answered Dec 24 '22 03:12

heckj