REST For the Wicked

Sudo Soldiers
04.23.2014

Hi. I’m Karan.

Sudo Soldiers

Weekly demos

  • 2 pitches every week
  • Complete/incomplete projects + ideas welcome
  • Talk about: Motivation, challenged, help, experience
  • 2 minutes each pitch - hard limit
  • Sign up now!

Today's Demos

Today...

REST

API

  • defines interaction between components
  • software or hardware
  • HTTP - REST, SOAP

REST

Why bother

  • API's break strong ties between components
  • Separation of client from server
  • 1 API - Multiple devices
  • Easier to make clients
  • Expansion - Motivate other developers to leverage your platform

JSON

  • Data exchange format
  • JavaScript Object Notation
  • name: value pairs
  • {
        "firstName": "John",
        "lastName": "Smith",
        "isAlive": true,
        "age": 25,
        "height_cm": 167.64,
        "address": {
            "streetAddress": "21 2nd Street",
            "city": "New York",
            "state": "NY",
            "postalCode": "10021-3100"
        },
        "phoneNumbers": [
            { "type": "home", "number": "212 555-1234" },
            { "type": "fax",  "number": "646 555-4567" }
        ]
    }

HTTP methods

REST is all about simplifying API's. It defines 4 methods (verbs):

  • GET - Read a specific resource
  • PUT - Update a specific resource
  • DELETE - Remove/delete a specific resource
  • POST - Create a new resource.
So instead of API calls such as:
Create – GET: /user/new?name=John
Read – GET: /user/1
Update – GET: /user/1/update?name=Doe
Delete – GET: /user/1/delete
Simply:
Create – POST: /user (with request body)
Read – GET: /user/1
Update – PUT: /user/1 (with request body)
Delete – DELETE: /user/1

Resource vs Representation

Resource

Consider http:/test.com/user/1. Here, user/1 is a resource.



Representation

Set of attributes related to the resource returned by the URI. A `GET http:/test.com/user/1` returns the representation of a user as:

name: John
Age: 21
Representation has a format (XML, JSON, JSONP) and a language (en, de).

80/20

  • Making an API is 80% art and 20% science.
  • Art - Creating a sensible URL hierarchy
  • Science - Creating sensible resource names
  • Follow the standards
  • URL hierarchy should make it obvious - how to use the API
  • Resource names should provide context for service request
  • GET /events - get all events
    GET /events/concerts - get all concerts
    GET /events/concerts/123 - get a specific concert
  • URL's are hierarchical - leverage that
  • Design for clients, not the data
  • Avoid verbs, use nouns
  • Keep URL's short and sensible

HTTP Response Codes

  • 200 OK
  • 201 CREATED
  • 400 BAD REQUEST
  • 401 UNAUTHORIZED
  • 404 NOT FOUND
  • 500 INTERNAL SERVER ERROR
  • 418 - I'M A TEAPOT

Headers

Client to Server

  • Authorization - Enable auth for any method
  • User-Agent - Info about the client
  • Host - Domain of the client

Server to Client

  • Content-Encoding - Compression being used
  • Content-Length - Size of response in bytes

State

REST is Stateless

That means...

  • Do not create session
  • Client must pass authorization in every call
  • Never, never use a session key

Let's make an API

Acronyms API

We have the data and starter code. Let's hack something!

Use Postman to test the API.

Deploying

Heroku

  • PAAS
  • Free
  • Easy deployment
  • git push heroku master
  • Addons for pretty much anything you want

Deploy our API

Assuming you signed up, and logged in using the Heroku Toolkit.
# make a Procfile
$ echo "web: node app.js" > Procfile

# test using foreman
$ foreman start

# (git add, git commit your code)

# create an app
$ heroku create [app name]

# to the moon
git push heroku master

Detailed tutorial

Now what?

That is all.

goel.im

@TheKaranGoel

github.com/karan


Questions?