Delicious REST APIs with Django and Tastypie

By @sanketsaurav, co-founder, doselect

Delicious REST APIs with
Django and Tastypie

By Sanket Saurav
Co-founder, doselect

sanket.__doc__

Sanket Saurav

What is REST?

Representational state transfer (REST) is an abstraction of the architecture of the World Wide Web; more precisely, REST is an architectural style consisting of a coordinated set of architectural constraints applied to components, connectors, and data elements, within a distributed hypermedia system.

Wikipedia

What is REST?

Methods

Status Codes

Resources

A resource (R) is a temporary varying membership function M(t), which for time t maps to a set of entities, or values, which are equivalent.

Roy Fielding

Resources

Representations

Representations

GET /api/v1/quote/l8rdp

200 OK
Content-Type: application/json
{
	author: "/api/v1/author/albus-dumbledore"
	created: "Fri, 17 Oct 2014 20:33:19 +0000"
	quote: "Harry is the best hope we have. Trust him."
	resource_uri: "/api/v1/quote/l8rdp"
	uid: "l8rdp"
}

		

The Good Parts

  1. Uses the REST methods and status codes properly
  2. Round trippable data
    • Anything you can GET, you can POST/PUT
  3. Super-flexible serialization
    • JSON, JSONP, XML, YAML, HTML
  4. Fully HATEOAS compatible
  5. Extensibility is a core feature

HATEOAS?

  1. Hypermedia as the Engine of Application State
  2. The user shouldn't have to know anything in advance about the data
  3. Complete explorability and deep-linking of data
  4. RESTful interaction is driven by hypermedia, rather than out-of-band information
  5. Decouples client and server in a way that allows the server functionality to evolve independently

C R U D

List & Detail

The Dehydrate Cycle

  1. Put the data model into a Bundle instance, which is then passed through the various methods.
  2. Run through all fields on the Resource, letting each field perform its own dehydrate method on the bundle.
  3. While processing each field, look for a dehydrate_ method on the Resource. If it’s present, call it with the bundle.
  4. Finally, after all fields are processed, if the dehydrate method is present on the Resource, it is called & given the entire bundle.