rest e resource oriented architectures

77
Gabriele Lana [email protected] REST and Resource Oriented Architectures

Upload: grusp

Post on 25-May-2015

816 views

Category:

Technology


0 download

DESCRIPTION

REST è lo stile architetturale a cui aderisce quello che oggi noi chiamiamo Web. Uno dei problemi storici della programmazione di applicazioni web è l’impendance mismatch fra vari modelli computazionali e di dati (REST, Object Oriented, Relational Model, ecc…). Questa sessione ha lo scopo di evidenziare la problematica con relativi esempi, soluzioni e risultati ottenuti con l’utilizzo di PHP.

TRANSCRIPT

Page 1: REST e Resource Oriented Architectures

Gabriele [email protected]

REST and Resource Oriented

Architectures

Page 2: REST e Resource Oriented Architectures

what isREST?

Page 3: REST e Resource Oriented Architectures

a Protocolan Architecture

a Softwarea Standard

a fancy name for Web Servicesa Buzzword

REST is not

Page 4: REST e Resource Oriented Architectures

RepresentationalStateTransfer

Roy T. Fielding“Architectural Styles and the Design of Network-based Software Architectures”

Ph.D dissertation, 2000

Page 5: REST e Resource Oriented Architectures

a Software Architecture Stylea set of Constraints on Component

Interaction that, when obeyed, cause the resulting Architecture to have

certain properties

Roy T. Fielding http://roy.gbiv.com/untangled/2008/on-software-architecture

REST is

Page 6: REST e Resource Oriented Architectures

?

Page 7: REST e Resource Oriented Architectures

“Roy Fielding will officially disown most of the RESTful authors and software packages

available. Nobody will care, or worse, somebody looking to make a name for themselves will proclaim that “Roy doesn't really understand

REST”, and they'll be right: Roy doesn't understand what they consider to be REST”

http://dotnet.dzone.com/articles/ted-neward-2009-predictions-20

prediction for 2009

Page 8: REST e Resource Oriented Architectures

“... the motivation for developing REST was to create an architectural model for how the Web

should work, such that it could serve as the guiding framework for the Web protocol

standards”

http://www.ics.uci.edu/~fielding/pubs/dissertation/evaluation.htm

why?

Page 9: REST e Resource Oriented Architectures

REST vsSOA vsSOAP vsRPC vsWS-* vs

...

no Silver Bullets

Page 10: REST e Resource Oriented Architectures

but sometimesREST is better :-)

Page 11: REST e Resource Oriented Architectures

HTTPOOP

RDB

MS

web applicationsInformation Flow

Page 12: REST e Resource Oriented Architectures

HTTPOOP

RDB

MSHTTP

HTTP is REST

Page 13: REST e Resource Oriented Architectures

HTTPOOP

RDB

MS

Impedance Mismatch

Page 14: REST e Resource Oriented Architectures

HTTPOOP

RDB

MS

AccidentalComplexity

Page 15: REST e Resource Oriented Architectures

HTTPOOP

RDB

MSHTTP

OOP

RDB

MS

EssentialComplexity

Page 16: REST e Resource Oriented Architectures

RPC

RPC OOP

RDB

MS

HTTP

Page 17: REST e Resource Oriented Architectures

HTTPRDB

MSROA

Resource Oriented Architectures

Page 18: REST e Resource Oriented Architectures

HTTP ROA DATA

maybe not today, but Yes, we Can

Page 19: REST e Resource Oriented Architectures

REST by Example(Chess)

Page 20: REST e Resource Oriented Architectures

how to Design aRESTful service

Step by Step

Leonard Richardson & Sam Ruby “RESTful Web Services”, O’Reilly 2007

Page 21: REST e Resource Oriented Architectures

1. every Resource must be Identified

Page 22: REST e Resource Oriented Architectures

BoardGame

Position

Move

FileRankPlayer

Square

Knight

Queen

Rook

Pawn Bishop

Page 23: REST e Resource Oriented Architectures

every Concept in your Domain can

be a Resource

Page 24: REST e Resource Oriented Architectures

2. design the URIs (Ubiquitous Language)

Page 25: REST e Resource Oriented Architectures

/position/wqd3,wkd1,bqd7,bkd8

Page 26: REST e Resource Oriented Architectures

/position/wqd3,wkd1,bqe6,bkf8/d3-d8

Page 27: REST e Resource Oriented Architectures

/move/wqd3,wkd1,bkf8-wqd8,wkd1,bkf8

Page 28: REST e Resource Oriented Architectures

/position/initial

Page 29: REST e Resource Oriented Architectures

/board/4815162342

/position/wqd3,wkd1,bqe6,bkf8

/position/wqd8,wkd1,bqe6,bkf8

move = d3-d8

Page 30: REST e Resource Oriented Architectures

/player/50298

Page 31: REST e Resource Oriented Architectures

3. for each Resourceexpose a subset of

the Uniform Interface

Page 32: REST e Resource Oriented Architectures

GET (Read)POST (Create)PUT (Update)

DELETE (Delete)

Uniform Interface

Page 33: REST e Resource Oriented Architectures

GET /player/50298 HTTP/1.1

Retrieve informations on identified Resource

Idempotent, should not changethe Resource State

Page 34: REST e Resource Oriented Architectures

POST /position/wqd3,wkd1,bke8 HTTP/1.1\nmove = d3-d8

POST to let Resources process informations

HTTP/1.1 302 FoundLocation: /position/wqd8,wkd1,bke8

Page 35: REST e Resource Oriented Architectures

POST /game HTTP/1.1\nwhite = /player/50298 &black = /player/39650

POST to Createnew Resources

HTTP/1.1 201 CreatedLocation: /game/42

Page 36: REST e Resource Oriented Architectures

PUT /board/4815162342 HTTP/1.1\nposition = /position/wqd3,wkd2,bke8

PUT to UpdateResource Informations

HTTP/1.1 205 Reset Content

Page 37: REST e Resource Oriented Architectures

PUT /position/wqd3,wkd1,bke8 HTTP/1.1\nposition = /position/wqd3,wkd2,bke8

a Method can benot supported by a Resource

but should not be ignored

HTTP/1.1 405 Method Not Allowed

Page 38: REST e Resource Oriented Architectures

DELETE /board/4815162342 HTTP/1.1

DELETE tologically Remove

Resources

HTTP/1.1 204 No Content

Page 39: REST e Resource Oriented Architectures

4. for each Resourcedesign Representations

from and to Client

Page 40: REST e Resource Oriented Architectures

GET /position/wqd3,wkd2,bke8 HTTP/1.1Accept: text/plain

client/server Content Negotiation(MIME properties)

HTTP/1.1 200 OKContent-Type: text/plain;format=fen\n3k4/8/3q4/8/8/8/8/5K2

Page 41: REST e Resource Oriented Architectures

GET /position/wqd3,wkd2,bke8 HTTP/1.1Accept: image/png;q=0.8, text/plain;q=0.2

HTTP/1.1 200 OKContent-Type: image/png\nPNG...

client/server Content Negotiation(Preferences)

Page 42: REST e Resource Oriented Architectures

GET /position/wqd3,wkd2,bke8.png HTTP/1.1Accept: image/png

HTTP/1.1 200 OKContent-Type: image/png\nPNG...

client Explicit Request ofContent-Type

Page 43: REST e Resource Oriented Architectures

GET /position/wqd3,wkd2,bke8.gif HTTP/1.1Accept: image/gif

HTTP/1.1 406 Not Acceptable

or more friendlyHTTP/1.1 300 Multiple ChoicesContent-Type: application/xml \n<choices><content type=”image/png” uri=”...”>

Page 44: REST e Resource Oriented Architectures

GET /game/42 HTTP/1.1Accept: application/vnd.dharma.chess

<game> <player role=”white”>/player/1001</player> <player role=”black”>/player/1002</player> <moves> <position>/position/initial</position> </moves></game>

custom Content-Type

Page 45: REST e Resource Oriented Architectures

wait...what about

plain oldWeb Applications?

Page 46: REST e Resource Oriented Architectures

GET /game/42 HTTP/1.1Accept: application/xhtml+xml

<html> ... <a rel=”owner” href=”/player/1001”>...</a> ... <ul class=”moves”> <li><image src=”/position/initial.png”/></li> </ul></html>

Semantic Web is not a dream

Page 47: REST e Resource Oriented Architectures

Tim Berners-Leeon Linked Data

“... almost 20 years ago I wanted to reframe the way we use

informations ... I invented the Word Wide Web ... now is time

for a new reframing...”

Tim Berners-Lee @ http://linkeddata.org

Page 48: REST e Resource Oriented Architectures

expected behavior of the web

1. Use URIs as names for things2. Use HTTP URIs so that people

can look up those names3. When someone looks up a URI,

provide useful information4. Include links to other URIs, so

that they can discover more things

Page 49: REST e Resource Oriented Architectures

POST /game/42 HTTP/1.1Content-Type: application/vnd.dharma.chess

Authorization: Basic dXN1cm5hbWU6cG...\n<game> <player>...</player> <moves> <position>/position/initial</position>

<position color=”white”> /position/wra1,wna2,...,wpd4,... </position> </moves></game>

... from and to the Client

Page 50: REST e Resource Oriented Architectures

5. designtypical flowsanderrorconditions

Page 51: REST e Resource Oriented Architectures

POST /game/42 HTTP/1.1Content-Type: application/vnd.dharma.chessAuthorization: Basic dXN1...\n<game> <player>...</player> <moves> <position>/position/initial</position>

<position color=”white”> /position/wra1,wna2,...,wpd4,... </position> </moves></game>

white Player make first move

Page 52: REST e Resource Oriented Architectures

HTTP/1.1 201 CreatedLocation: /game/42/position/2

Response to the white Player

GET /game/42/position/2

HTTP/1.1 301 Moved PermanentlyLocation: /position/wra1,wna2,...,wpd4,...

Page 53: REST e Resource Oriented Architectures

GET /game/42 HTTP/1.1Authorization: Basic dXN1...\n<game> <player>...</player> <moves>...</player></game>

GET /game/42 HTTP/1.1Authorization: Basic aYR5...\n<game> <player>...</player> <moves>...</player></game>

both Players must GETthe game Representation

Page 54: REST e Resource Oriented Architectures

POST /game/42 HTTP/1.1Content-Type: application/vnd.dharma.chessAuthorization: Basic aYR5...\n<game> <player>...</player> <moves> <position>/position/initial</position> <position>/position/wra1,wna2,...,wpd4,...</position>

<position color=”black”> /position/wra1,wna2,...,bpd5,... </position> </moves></game>

black Player can make next move

Page 55: REST e Resource Oriented Architectures

POST /game/42 HTTP/1.1Content-Type: application/vnd.dharma.chessAuthorization: Basic aYR5...\n...

what if black Player try to resend?

HTTP/1.1 409 Conflict

the game Representation isout of date

Page 56: REST e Resource Oriented Architectures

POST /game/42 HTTP/1.1Content-Type: application/vnd.dharma.chessAuthorization: Basic aYR5...\n...

what if black Player try to move again?

HTTP/1.1 403 Forbidden

the black Player should waitfor the white Player to move

Page 57: REST e Resource Oriented Architectures

Business Rules could be easilyexpressed through the Resource's

Behavior

POST /position/wqd3,wkd1,bke8 HTTP/1.1\nmove = d3-e8

what if we try to make a wrong move?

HTTP/1.1 404 Not Found

Page 58: REST e Resource Oriented Architectures

wait...what about

complex UserInterfaces?

Page 59: REST e Resource Oriented Architectures

what about Gmail

Page 60: REST e Resource Oriented Architectures

HTTP

?

JS

JS

what about Gmail

Page 61: REST e Resource Oriented Architectures

what about Gmail

Page 62: REST e Resource Oriented Architectures

FAQ: cool URIsare RESTful?

OpenSocial/people/{guid}/@all

Collection of all people connected to user {guid}http://www.opensocial.org/Technical-Resources/opensocial-spec-v081/restful-protocol.html

Page 63: REST e Resource Oriented Architectures

FAQ: REST isstateless?

Statelessness says thatall possible states of the server

are also Resources(Resource State)

Page 64: REST e Resource Oriented Architectures

FAQ: REST isstateless?

Statelessness says thateach message contains

all the informations necessaryto understand the Request

(Application State)

Page 65: REST e Resource Oriented Architectures

SERVER

R1

R2

R3R4

CLIENT

Application

State

Resource

State

Representational State Transfer

Page 66: REST e Resource Oriented Architectures

your application can be Discoverable

Page 67: REST e Resource Oriented Architectures

your application can be Testable

Page 68: REST e Resource Oriented Architectures

Testability

HTTP

View Model

Control

Page 69: REST e Resource Oriented Architectures

too much code untested

HTTP

View Model

Control

Testability

Page 70: REST e Resource Oriented Architectures

too fragile and unreliableTestability

HTTP

View Model

Control

Page 71: REST e Resource Oriented Architectures

Testability

HTTP

Board

Game

Player

Position

Move

Js

Page 72: REST e Resource Oriented Architectures

HTTP

Board

Game

Player

Position

Move

Js

what about the view?Testability

Page 73: REST e Resource Oriented Architectures

your application can be Composable

Page 74: REST e Resource Oriented Architectures

your application can be Scalable

Page 75: REST e Resource Oriented Architectures

...so what Really means (for me) being RESTful?

Page 76: REST e Resource Oriented Architectures

now the Webcan talk about

chess

Page 77: REST e Resource Oriented Architectures