Sunday, October 15, 2006

REST Epiphany

Ever since first hearing about REST, I have have been attracted to its' simplicity. Let's face it who wants to use SOAP and WS-*? I've struggled with REST though. What do you mean no new verbs? OO programming is built on verbs right? What would objects be without verbs (process)?

The I had my epiphany....

There was an article on REST on InfoQ that got me thinking about it once more. The article had a link to an application written in the REST style. The application allowed you to make telephone calls by sending messages using REST. It suddenly clicked. A RESTful request is a message send in an OO sense. OO is all about sending messages, it is the reciever that then interprets the message and selects an associated process depending on the message REPRESENTATION. For human consumption the message normally contains verbs, but as far as the recviever is concerned, the message is just a label (noun) which is used to select a process defined up front.

OO isn't about verbs, its about messages (nouns) which recievers act upon. So REST is kind of like a dynamic OO message send. An Example:


Buying a book at Amazon.

I would need to GET the book details and price info from Amazon, and
determine that indeed this is the book I want and that I like the price.

I would then POST my credit card details and delivery address, along with
any other relevant details back to Amazon..

As long as Amazon and I agree on the REPRESENTATION of these messages then
everything should all work fine.


Know using a keyword message format like in smalltalk I could send to Amazon the message:

Amazon.bookDetailsAndPrice: mybook.

As Mark Baker points out. A message in distributed programming is normally "a document with an operation and address wrapped around it". REST isn't like that. The operation is implicit in the URL and the REPRESENTATION of the document. So the same "message" in REST is:

GET Amazon/bookDetailsAndPrice?book=mybook
Response: A document with the book details an price.

And I would post back my credit card details:

POST Amazon/checkout

my credit card details
mybook
my address

Response: A document showing the success or failure of the transaction.


So you can send any message you like with REST, by transfering documents. It is that simple. So why didn't I get it before?

Paul.

2 comments:

Mark Baker said...

Paul, you might want to clarify "The operation is implicit in the URL" - the explanation is good following that statement, but others might take it the wrong way.

Paul Beckford said...

Hi Mark,

I see wat you mean. Strictly speaking the Operation is GET, POST, PUT etc. The URL is the resource identifier. In Smalltalk the resource in question would be an answer to the message send, and the resource identifier is the method selector used as a key in the receiving objects method dictionary and the parameters passed.

So maybe a better phrase would be: "The URL identifies the resource in question. The resource is generated by a method invocation at the server."