-
Notifications
You must be signed in to change notification settings - Fork 2
Handler API inspection
Fundamentally all RESTful pages have this logic
-
Sanitizing input. That is the information submitted by the user consistent with the constraints of the system.
-
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.)
-
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:
-
How do we view an object in different ways depending on authentication status etc
-
Writing return is error-prone!
-
Can we split these steps up in different parts
-
Who is responsible for object serialization