Skip to content

Handler API inspection

William Hogman edited this page Mar 7, 2013 · 7 revisions

Fundamentally all RESTful pages have this logic

  1. Sanitizing input. That is the information submitted by the user consistent with the constraints of the system.

  2. Fetching or Modifying state. Changing the system based on the submitted information. This operation yields a result in the form of either an object or a a status (Something was created, I didn't find that etc.)

  3. Creating output, based on what the user requested, JSON, XML, HTML or whatever. We should give them information based on the result obtained in step 2.

Example in psuedo GO

// isBad FetchFromDb etc are usersupplied
func SomeHandler(Request* r, Response* res) {

   // Sanitize input
   foo := r.Header.get("foo")
   if err := isBad(foo); err != nil {
       res.BadRequest(err);
       return; // halt the request
   }
   obj, err := FetchFromTheDb(foo)
   // omitted error handling
   res.Ok(obj);

}

This has a few problems though:

  1. How do we view an object in different ways depending on authentication status etc

  2. Writing return is error-prone!

  3. Can we split these steps up in different parts

  4. Who is responsible for object serialization

Clone this wiki locally