REST Introduction (PHP London)

40
 

description

An introduction to Representation State Transfer (REST) and how it's philosophies can alter the way you design and build web applications.

Transcript of REST Introduction (PHP London)

Page 1: REST Introduction (PHP London)

   

Page 2: REST Introduction (PHP London)

   

Representational State Transfer

Page 3: REST Introduction (PHP London)

   

RESTful Principles

Page 4: REST Introduction (PHP London)

   

A universal syntax for resource-identification

Page 5: REST Introduction (PHP London)

   

A set of well-defined operations

Page 6: REST Introduction (PHP London)

   

Having a shared set of media-types

Page 7: REST Introduction (PHP London)

   

The use of hypermedia for application state-transitions

Page 8: REST Introduction (PHP London)

   

Statelessprotocol

Page 9: REST Introduction (PHP London)

   

Building a RESTful App

Page 10: REST Introduction (PHP London)

   

RESTfully Delicious

● Get a list of all bookmarks● filter by user and/or tag● limit by number

● Add a bookmark● Edit a bookmark● Delete a bookmark

Page 11: REST Introduction (PHP London)

   

Discover Resources

Resources URLs Methods RepresentationsBookmark /bookmarks/{md5} GET

PUTDELETE

Bookmark list /bookmarks GETPOST

Users bookmarks /users/{user} GETTagged bookmarks /tags/{tag} GET

application/bookmark+xmlapplication/bookmark+xml

application/atom+xmlapplication/bookmark+xmlapplication/atom+xmlapplication/atom+xml

Resources URLs Methods RepresentationsUser list /users GETTag list /tags GETHomepage / GET

application/atom+xmlapplication/atom+xmlapplication/delicious+xml

Page 12: REST Introduction (PHP London)

   

GETting the Homepage

GET /

200 OK

Content­type: application/delicious+xml

<?xml version=”1.0”?>

<delicious users=”/users” bookmarks=”/bookmarks” tags=”/tags”>

<recent start=”1” end=”20” next=”/?start=21&amp;end=40”>

<bookmark url="http://www.example.org/something­intersting" href="/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"/>

...

</recent>

</delicious>

Page 13: REST Introduction (PHP London)

   

GETting Bookmarks

GET /bookmarks

200 OK

Content­type: application/atom+xml

<?xml version="1.0"?>

<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Bookmarks</title>

  <entry>

    <title>Something interesting</title>

    <link href="/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"/>

    <summary>http://example.org/something­intersting</summary>

  </entry>

</feed>

Page 14: REST Introduction (PHP London)

   

GETting A Bookmark

GET /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf

200 OK

Content­type: application/bookmark+xml

<?xml version="1.0"?>

<bookmark>

  <title>Something interesting</title>

  <url>http://example.org/something­intersting</url>

  <user href=”/users/pauljames”>pauljames</user>

  <tags>

    <tag href=”/tags/interesting”>interesting</tag>

  </tags>

</bookmark>

Page 15: REST Introduction (PHP London)

   

Creating and Updating

POST /bookmarks

Content­type: application/bookmark+xml

...

201 Created

Location: /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf

PUT /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf

Content­type: application/bookmark+xml

...

200 OK

Page 16: REST Introduction (PHP London)

   

Benefits of Being RESTful

● Better scaling due to stateless communications ● Better response times due to caching● Better long­term compatibility due to:

● the capability of document types to evolve without breaking backwards­compatibility

● the ability to add support for new content types without reducing support for older content types.

● Do less with more

Page 17: REST Introduction (PHP London)

   

Finally, some PHP

HTTP method$_SERVER['REQUEST_METHOD']

Reading request data$_SERVER['CONTENT_LENGTH']

$_SERVER['CONTENT_TYPE']

POST - $HTTP_RAW_POST_DATA

Other HTTP methods - fopen('php://input', 'r')

Processing request dataSimpleXML, MiniXML, PHP JSON, parse_str, unserialize,

unpack, preg_match, etc.

Page 18: REST Introduction (PHP London)

   

Generating ResponsesHTTP response codes

200 OK, 201 Created, 204 No Content, 304 Not Modified, 401 Unauthorized, 404 Not Found, 405 Method Not Allowed, 411 Length Required, 415 Unsupported Media Type

Generating representationsSmarty, PHPFastTemplate, GD, SimpleXML, printf, etc.

HTTP cachingExpires, Cache-Control, Etag, Last-Modifiedheader('Expires: '.gmdate('D, j M Y H:i:s T', time() + $cachelength));

header('Cache­Control: max­age='.$cachelength.', must­revalidate');

Page 19: REST Introduction (PHP London)

   

Conclusion

● REST is not just about “Web Services”● Set of best practices for building Web apps

● Give everything a URL● Use correct HTTP methods● Use common media types● Link things together with hypertext

Page 20: REST Introduction (PHP London)

   

Further Reading

● Roger L. Costello (http://www.xfront.com/REST­Web­Services.html)

● Paul Prescod (http://www.prescod.net/rest/)● REST Wiki (http://rest.blueoxen.net/)● Atom Publishing Protocol 

(http://atomenabled.org/developers/protocol/)● http://del.icio.us/tag/rest

Page 21: REST Introduction (PHP London)

   

 

    1

IntroductionQuestions

Overloaded term

POX over HTTP?Other HTTP methods?Tidy URLs?Buzzword?

http://flickr.com/photos/estherase/128983854/

Page 22: REST Introduction (PHP London)

   

 

    2

Representational State Transfer

Architectural styleDistilled from the Web by Roy FieldingHTTP 1.1 was designed to conform to RESTDefines how the Web worksDescribes a set of rules for building applications on the 

Web that exhibit certain desirable propertiesREST is not HTTP, but HTTP is RESTful

“it’s the way the Web already works, just formalized a bit  and with some do’s and don’ts.”

Web service is a Web page that’s meant to be consumed by an autonomous program as opposed to a Web browser

http://flickr.com/photos/practicalowl/392894653/

Page 23: REST Introduction (PHP London)

   

 

    3

RESTful Principles

5 principles

● A universal syntax for resource­identification ­ URLs● A set of well­defined operations● Having a shared set of media­types● The use of hypermedia for application state­transitions● Stateless protocol – all state on the client

http://flickr.com/photos/thowi/113223967/

Page 24: REST Introduction (PHP London)

   

 

    4

A universal syntax for resource-identification

● Uniform resource locators (URLs)● Every resource (thing of interest) has a URL● URLs are unique and allow us to dereference a 

resource● Nouns● Trillions of nouns for all the concepts in all the heads 

and files of all the people in the world

http://flickr.com/photos/joeholmes/258136938/

Page 25: REST Introduction (PHP London)

   

 

    5

A set of well-defined operations

● HTTP methods ­ verbs● GET – fetch● POST ­ append/process● PUT ­ create/update● DELETE ­ delete● Uniform interface, GET always gets, PUT always 

creates● Using different verbs for different nouns would make 

widespread communication impossible● There are no applications you can think of which 

cannot be made to fit

http://flickr.com/photos/joygant/971783023/

Page 26: REST Introduction (PHP London)

   

 

    6

Having a shared set of media-types

● What's not machine­processable about the current Web isn't the protocol, it's the content

● Information conveyed via documents● A standard set of document formats (HTML, RDF, 

JPEG, PNG, etc.)● Representation of a resource● Resources are just concepts, representations are how 

we interact with them

http://flickr.com/photos/thefrankfurtschool/1305454450/

Page 27: REST Introduction (PHP London)

   

 

    7

The use of hypermedia for application state-transitions

● Hypertext provides links between resources● Clients change state (navigate the Web) via information 

from a previous state● URLs are opaque to clients, they never construct URLs● Because the links mirror the structure of how a user 

makes progress through an application● A Web­based application is a dynamically changing 

graph of state representations (pages) and potential transitions (links) between states

● If not, it may be accessible from the Web, but it’s not really part of the Web

http://flickr.com/photos/pgoyette/100769956/

Page 28: REST Introduction (PHP London)

   

 

    8

Statelessprotocol

● Application state is the information necessary to understand the context of an interaction – auth details, etc.

● Resource state – S in REST, avoid unnamed state● All requests must include all application state● Session state is application state – if you want  a 

session you need a smarter client than a browser – shopping cart

● Prevents partial failures● Load­balancing● Service interruptions

http://flickr.com/photos/davenyc/23033147/

Page 29: REST Introduction (PHP London)

   

 

    9

Building a RESTful App

● Discover first class objects● Our resources● Assign URLs and URL­spaces● Define representations● Input and output formats● Define HTTP methods

Time for an Example

http://flickr.com/photos/hugovk/2037935886/

Page 30: REST Introduction (PHP London)

   

 

    10

RESTfully Delicious

● Get a list of all bookmarks● filter by user and/or tag● limit by number

● Add a bookmark● Edit a bookmark● Delete a bookmark

Get a list of all bookmarksfilter by user and/or taglimit by number

Add a bookmarkEdit a bookmarkDelete a bookmark

http://flickr.com/photos/sharynmorrow/124428600/

Page 31: REST Introduction (PHP London)

   

 

    11

Discover Resources

Resources URLs Methods RepresentationsBookmark /bookmarks/{md5} GET

PUTDELETE

Bookmark list /bookmarks GETPOST

Users bookmarks /users/{user} GETTagged bookmarks /tags/{tag} GET

application/bookmark+xmlapplication/bookmark+xml

application/atom+xmlapplication/bookmark+xmlapplication/atom+xmlapplication/atom+xml

Resources URLs Methods RepresentationsUser list /users GETTag list /tags GETHomepage / GET

application/atom+xmlapplication/atom+xmlapplication/delicious+xml

http://flickr.com/photos/sharynmorrow/124428600/

Page 32: REST Introduction (PHP London)

   

 

    12

GETting the Homepage

GET /

200 OK

Content­type: application/delicious+xml

<?xml version=”1.0”?>

<delicious users=”/users” bookmarks=”/bookmarks” tags=”/tags”>

<recent start=”1” end=”20” next=”/?start=21&amp;end=40”>

<bookmark url="http://www.example.org/something­intersting" href="/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"/>

...

</recent>

</delicious>

http://flickr.com/photos/sharynmorrow/124428600/

Page 33: REST Introduction (PHP London)

   

 

    13

GETting Bookmarks

GET /bookmarks

200 OK

Content­type: application/atom+xml

<?xml version="1.0"?>

<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Bookmarks</title>

  <entry>

    <title>Something interesting</title>

    <link href="/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"/>

    <summary>http://example.org/something­intersting</summary>

  </entry>

</feed>

http://flickr.com/photos/sharynmorrow/124428600/

Page 34: REST Introduction (PHP London)

   

 

    14

GETting A Bookmark

GET /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf

200 OK

Content­type: application/bookmark+xml

<?xml version="1.0"?>

<bookmark>

  <title>Something interesting</title>

  <url>http://example.org/something­intersting</url>

  <user href=”/users/pauljames”>pauljames</user>

  <tags>

    <tag href=”/tags/interesting”>interesting</tag>

  </tags>

</bookmark>

http://flickr.com/photos/sharynmorrow/124428600/

Page 35: REST Introduction (PHP London)

   

 

    15

Creating and Updating

POST /bookmarks

Content­type: application/bookmark+xml

...

201 Created

Location: /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf

PUT /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf

Content­type: application/bookmark+xml

...

200 OK

http://flickr.com/photos/sharynmorrow/124428600/

Page 36: REST Introduction (PHP London)

   

 

    16

Benefits of Being RESTful

● Better scaling due to stateless communications ● Better response times due to caching● Better long­term compatibility due to:

● the capability of document types to evolve without breaking backwards­compatibility

● the ability to add support for new content types without reducing support for older content types.

● Do less with more

Better scaling due to stateless communications Better response times due to cachingBetter long­term compatibility due to:

the capability of document types to evolve without breaking backwards­compatibility

the ability to add support for new content types without reducing support for older content types.

Lower learning curve for consumerLower support overhead for producer

http://flickr.com/photos/ari/1387533615/

Page 37: REST Introduction (PHP London)

   

 

    17

Finally, some PHP

HTTP method$_SERVER['REQUEST_METHOD']

Reading request data$_SERVER['CONTENT_LENGTH']

$_SERVER['CONTENT_TYPE']

POST - $HTTP_RAW_POST_DATA

Other HTTP methods - fopen('php://input', 'r')

Processing request dataSimpleXML, MiniXML, PHP JSON, parse_str, unserialize,

unpack, preg_match, etc.

http://flickr.com/photos/nez/378348754/

Page 38: REST Introduction (PHP London)

   

 

    18

Generating ResponsesHTTP response codes

200 OK, 201 Created, 204 No Content, 304 Not Modified, 401 Unauthorized, 404 Not Found, 405 Method Not Allowed, 411 Length Required, 415 Unsupported Media Type

Generating representationsSmarty, PHPFastTemplate, GD, SimpleXML, printf, etc.

HTTP cachingExpires, Cache-Control, Etag, Last-Modifiedheader('Expires: '.gmdate('D, j M Y H:i:s T', time() + $cachelength));

header('Cache­Control: max­age='.$cachelength.', must­revalidate');

http://flickr.com/photos/nez/378348754/

Page 39: REST Introduction (PHP London)

   

 

    19

Conclusion

● REST is not just about “Web Services”● Set of best practices for building Web apps

● Give everything a URL● Use correct HTTP methods● Use common media types● Link things together with hypertext

REST is not just about “Web Services”Set of best practices for building Web apps

Give everything a URLUse correct HTTP methodsUse common media typesLink things together with hypertext

REST is an architectural styleIt defines 4 core principles

A universal syntax for resource-identification

A set of well-defined operationsHaving a shared set of media-typesThe use of hypermedia for application

state-transitionsStateless client/server interaction

It helps us write well behaved appsIt's not just about Web APIs but the

whole Web, “there are no Web Services”

http://flickr.com/photos/naked_dave1/469220787/

Page 40: REST Introduction (PHP London)

   

 

    20

Further Reading

● Roger L. Costello (http://www.xfront.com/REST­Web­Services.html)

● Paul Prescod (http://www.prescod.net/rest/)● REST Wiki (http://rest.blueoxen.net/)● Atom Publishing Protocol 

(http://atomenabled.org/developers/protocol/)● http://del.icio.us/tag/rest

http://flickr.com/photos/dhammza/91435718/