Restful

Architecture


Joey Brown
brownjn12@gmail.com
@mtownjbrown

REST

Representational State Transfer

REST is an architectural style used represent and map resources in a networked application

  • Not a protocol
  • Not a standard
  • Not concerned with implementation, just transport layer

problems

  • Applications need to communicate over a network
  • Scalable, maintainable
    • Consistent, uniform
  • Lots of clients
  • Simple (!= easy)

REST

  • Resource = source
  • Representation = serialized
    [{id: 56, blob: xxxxx, date_created: 3/5/1999},{id: 57, blob: xxxxx, date_created: 3/5/1999},{id: 58, blob: xxxxx, date_created: 3/5/1999}] 
    <album>  <photo>    <id>56</id>    <blob>xxxxx</blob>    <date_created>3/5/1999</date_created>  </photo>  <photo>    <id>57</id>    <blob>xxxxx</blob>    <date_created>3/5/1999</date_created>
    </photo> <photo> <id>58</id> <blob>xxxxx</blob> <date_created>3/5/1999</date_created>
    </photo></album>

    URI

    • Standard
    • Consistent
    • Collections

    http://facebook.com/photos
    http://facebook.com/photos/1545

    Resource Interaction


    POST

    GET

    PUT

    PATCH

    DELETE

    POST


    facebook.com/photo

    {blob: xxxxx, date_created: 6/19/2004, tags: [mempy, webworkers, jug]}

    {id: 5000, blob: xxxxx, date_created: 6/19/2004, tags: [mempy, webworkers, jug]}

    GET

    facebook.com/photos/5000

     {id: 5000, blob: xxxxx, date_created: 6/19/2004, tags: [mempy, webworkers, jug]}

    PATCH

    facebook.com/photos/5000

     {id: 5000, tags: [ruby, .net, php]}

     {id: 5000, blob: xxxxx, date_created: 6/19/2004, tags: [ruby, .net, php]}

    PUT

    facebook.com/photos/5000

     {id: 5000, tags: [ruby, .net, php]}

     {id: 5000, blob: undefined, date_created: undefined, tags: [ruby, .net, php]}

    DELETE

    facebook.com/photos/5000

    Recap

    • Representations of resources
    • Location of resources
    • Interactions with resources

    DEMO

    Using tool, show creation, get, update, and deletion of event

    mem-web-demo id :
    13205302

    events url:
    http://localhost:8001/app/api/events

    HATEOAS

    [{id: 56, blob: xxxxx, date_created: 3/5/1999,   links: {self: http://facebook.com/photos/56}},{id: 57, blob: xxxxx, date_created: 3/5/1999},   links: {self: http://facebook.com/photos/57},{id: 58, blob: xxxxx, date_created: 3/5/1999},   links: {self: http://facebook.com/photos/56}]

    {id: 56, blob: xxxxx, date_created: 3/5/1999,    links: {self: http://facebook.com/photos/56,            parent: http://facebook.com/photos,            next: http://facebook.com/photos/57,            previous: http://facebook.com/photos/55}}

    rest

    ...ish

    REST vs soap

    SOAP = RPC
    Stateful:
    client:http://hackernews.com/posts/nextPost
    server:   previousPost++   nextPost = previousPost   return nextPost
    Stateless:
    client:http://hackernews.com/posts/56
    server:   return getPost(56)

    Our own restish app

    Things to consider:
    Hackable URLS
    CORS

    JSONP


    <script type="application/javascript" src="http://server2.example.com/Users/1234?jsonp=parseResponse"> </script>

     parseResponse({"Name": "Foo", "Id": 1234, "Rank": 7});

    demo meetup app


    AngularJS
    \/
    Django
    \/
    ???

    $resource

    XMLHttpRequest
    \/
    $http
    \/
    $resource

    $RESOURCE

     app.provider('EventResource', function() {
        this.$get = ['$resource', function($resource) {
            var Event = $resource(
                '/app/api/events/:id/', {}, {
                    update: { method: 'PUT' }
                }
            )
            return Event;
        }]
    })
    var eventResource = new EventResource();...eventResource.$save();
    ...EventResource.update({id: 49}, eventResource);...eventResource.delete();

    $resource

    check out resource code

    meetup restish api

    Events

    GET: /2/events
    POST: /2/event
    GET: /2/event/:id
    POST: /2/event/:id
    DELETE: /2/event/:id

    POST: /2/events
    GET: /2/events{/:id}
    PUT/PATCH: /2/events/:id
    DELETE: /2/events/:id

    demo meetup app

    Put urls in browser bar

    resources



    rest

    By joeybrown

    rest

    Presentation for Web Workers Presentation

    • 647