REST 101: Part 2 - A Million APIs 15

Posted by jeff Tuesday, April 03, 2007 14:39:00 GMT

Last time, I convinced you that the world wide web is a bunch of resources, not web pages. This time we will take another step closer to what REST is all about.

Object-Oriented Analysis and Design

If you've been doing object-oriented programming for some time, you probably learned to work like this:

  1. Write a description of your problem domain - what your program is supposed to do.
  2. The nouns are your classes
  3. The verbs are methods on those classes
  4. Have your nouns call methods on other nouns to accomplish a task

Sounds reasonable. That's how I did programming for a long time. This style of noun-verb programming is often called "RPC" style. I don't know what it has to do with "remote"-ness, but just know that "RPC-style" is the "classic" way of building object-oriented software. It's actually a great way to develop software. But that's not how the web was designed to work. Why not?

Imagine it's 1992 (or just sometime before the web as you know it today). And suppose three different companies developed three different software applications: one to buy books, one to buy airline tickets, and one that can provide aerial photographs from any arbitrary distance above the earth. They each followed the classic object-oriented modeling techniques outlined above to develop their apps. And thinking that third-party software might someday want to interface with their software, they each implemented their own API as well.

Now suppose it became your job to write a tool that would front-end all three applications.

Ugh. You'd have to learn three different APIs. And I bet you would have UI controls that would correspond directly to the methods you can call in each API. But it sounds logical, right? I mean, how else could you do it? Besides, it's only three different APIs, and you know of some good books that give you some design patterns you could use to try to reduce the complexity (and look cool to your co-workers).

How about a million APIs?

Let's think about the browser again. When you install it on your computer, it has no foreknowledge of what websites you will want to visit. Yet it can communicate with any website you want. Not only that, you can take actions on those sites: you can buy music, book airline tickets, and view satellite images of your house.

Is it magic? Think for a moment how impossible things would be if each website had their own API. To buy a book on Amazon, the browser would have to know to call the Amazon.Buy() method. And it would need to call the UnitedAirlines.CheckFlights() method when I clicked a button that said "Check Flights" on it. You just can't build a tool that would have to talk to a million different APIs.

That's why the web wasn't designed from an RPC perspective. It was designed from a REST perspective.

Resource-centric Design

According to Wikipedia, REST stands for Representational State Transfer. Well... whatever. All it really means is that instead of having nouns with an arbitrary set of verbs attached to each one, every noun (from now on referred to as a resource) will have the same finite set of verbs. In other words, every resource will expose the same API. The essential methods in this API enable any client tool to:

  1. Obtain a read-only view of a resource
  2. Create a new resource
  3. Update the attributes of a resource
  4. Delete a resource

Wait a minute! Which one of these methods means "buy this book"? Which one means "search for flights from Chicago to New York next Tuesday"? Which one lets me zoom in from "city" level to "street" level on a map? And which one lets a user login to the site?

We'll find out the answer next time. But I think you might know the answer already, if you really can stop thinking about the "buy the book" web page and start thinking about what resource you might be creating. See you next time.


Questions? Suggestions? Leave a comment and tell me.

Comments

Leave a response

  1. Mark Neustadt   April 03, 2007 @ 03:21 PM

    Arg! This is a terrific series but I constantly feel like you're teasing me! You have a great writing style and you're explaining things in a way that makes sense. I just wish your postings were a lot longer! It's exciting!

    Keep up the GREAT work!

    MCN

  2. Dylan Bennett   April 03, 2007 @ 08:57 PM

    "According to Wikipedia, REST stands for Representational State Transfer. Well... whatever."

    FNE. I love it. Personally, I like the pace of the articles. It's just enough info for me to digest within the onslaught of RSS feed reading I do every day.

    FNE <-- http://mboffin.com/post.aspx?id=2181 :)

  3. Frank   April 06, 2007 @ 01:36 PM

    Those articles are a real pleasure to read. This is done with passion and it shows.

    Congrats for the great work!

  4. Cameron Singe   April 20, 2007 @ 07:51 AM

    Great articles, you better finish these articles! - Jokes

  5. James   May 31, 2007 @ 08:12 PM

    'This style of noun-verb programming is often called "RPC" style. I don't know what it has to do with "remote"-ness, but just know that "RPC-style" is the "classic" way of building object-oriented software.'

    Are you sure about that? Are not getting confused with CRC (class, responsibility, and collaboration)?

  6. Perry   August 16, 2007 @ 05:47 PM

    Jeff, I finished reading your series of articles but I came back here to ask a question. Could you explain how the REST approach limits the number of APIs needed to render a resource? It seems that each operation/resource type pair needs a separate controller. I can't see the difference between the OOP and REST approach - could you comment on that one?

    Perry

  7. Raju   September 05, 2007 @ 05:24 AM

    Great work.Keep it up.

  8. Donovan   November 26, 2007 @ 09:31 AM

    Thank you so much Jeff. Your series is just what I need! Can you shed some more light on the resource-based design process? For example, how do go about choosing which resources to model? How do you handle complex, non-trivial resources - I.e, should I model a car with one controller/resource or several (one for chassis, one for engine, one for transmission, one for drive train, etc.) ? Thanks.

  9. Jeff   November 26, 2007 @ 07:20 PM

    Donovan,

    I still start with a traditional design, where I identify all of the models I need. (Ok, actually, I do this iteratively as I write tests, but that's the topic of another post...)

    Then, I generate RESTful controllers only for those things that I want my application to expose to the world, whether that's on web pages or via XML.

    Even though a car has a transmission, you may not need a separate Transmissions resource. One way to approach is it to think of your urls. If you're just using /cars/1, for example, and you simply are going to display the type of transmission used in that car, there's no need for a Transmissions resource.

    But if you wanted to access details about the transmission by going to /transmissions/46, then you would need a controller for that.

  10. Bunter   January 09, 2008 @ 09:55 AM

    Sounds like you are treating every webapplication in the world as one big database browser....

  11. Bunter   January 09, 2008 @ 10:23 AM

    While I wend through all the articles in this series, i don't see you keeping up to you promises. For example, while talking about "learning those horrible 3 different API-s to 3 different systems", you really don't show how REST with a wave of magic solves this problem. Instead of learning three traditional functional/object-oriented API-s you'll have to learn three URL based APIs. And those are as messy as anything else if you go pass simple CRUD examples which are really nice for demos and really simple applications. But if your UI starts to be just a bit more complex (I'll know i'm going to have angry rails mob down to my neck for making these kinds of comments) you'll need to start communicating UI model specifics back to the web server and that usually messes up this nice resource per URL model pretty fast.

  12. Valentine   April 28, 2008 @ 11:32 AM

    Great Article! Keep on doing !!

  13. kino   May 24, 2008 @ 01:06 AM

    Because of our necessary ignorance of the conditions, metaphysics (and it is not at all certain that this is true) depends on our sense perceptions, and our a posteriori knowledge, on the other hand, is the mere result of the power of the transcendental unity of apperception, a blind but indispensable function of the soul.

  14. Ellroy   May 30, 2008 @ 11:16 PM

    Therefore, is it true that the transcendental aesthetic can not take account of our sense perceptions, or is the real question whether our hypothetical judgements exist in our ideas?

  15. Ascanio   June 07, 2008 @ 07:00 PM

    Man, you are a terrific writer. I love your style. Keep it up!

Comment