Representational State Transfer

Post on 27-Aug-2014

176 views 4 download

Tags:

description

This presentation aimed to explain what is REST and why it is commonly misunderstood. It focuses on describing REST from scientific point of view, based on Roy`s Fielding dissertation.

Transcript of Representational State Transfer

Representational State Transfer

Alexei Skachykhin – Software EngineeriTechArt, 2014

The Internet Of Things Explosion of WWW Smart Electronics Web APIs

Painting Style

What painting style is it?

Cubism? Surrealism?

REST

Like painting style, REST is a network architectural styleRepresentational State Transfer

REST

NO SPEC! NO PROTOCOL!

Who coined the term?Roy Fielding

Author of HTTP spec Co-founder of Apache HTTP Server Senior Principal scientist at Adobe

Who else?Mike Amundsen Leonard Richardson

Architectural Style

Architectural style is basically set of constraints, andfollowing them leads to gaining certain

architectural properties (benefits)

Architectural Style

Feature-driven Architectural

Style

Architectural Style

Constraints-driven Architectural Style

Properties of REST

Heterogeny Scalability Evolvability

Visibility Reliability Efficiency

ConstraintsClient-Server Stateless Cacheable

Layered System

Code on Demand

Uniform Interface

Optional constraint

REST Derivation

RR CS LS VM U

$ CSS LCS COD

C$SS LC$SS LCODC$SS REST

uniform interface

programmable

layeredseparated

on-demand

replicated

stateless mobile simplevisible

intermediate

processing

cacheable

reliable shared extensible reusable

scalable multi.org.

Client-Server Separation of concerns Independent development Security is scoped to connections

Benefits

Client-Server

Wouldn`t that mean that Client-Side databases violate REST?

NOT AT ALL ACTUALLY

Client-Server

How does WebRTC fit into REST?IT DOESN`T

Stateless

Each request from client to server must contain all of the information necessary

to understand the request

Stateless

Application State

Resource State

Stateless Simplified Scalability (CAP theorem) Improved Reliability (recover from

failures) Visibility

Benefits

Stateless Performance decrease (repetitive data) Risk of inconsistent app behavior

Disadvantages

Cacheable

Cache constraints require that the data within a response to a request be implicitly or

explicitly labeled as cacheable or non-cacheable

Cacheable Improved efficiency Improved scalability Improved user perceived performance

Benefits

Layered system

Allows an architecture to be composed of hierarchical layers

Layered system Bound complexity Improved scalability (load balancing)

Benefits

Layered system Increased latency User-perceived performance may

degrade

Disadvantages

Code on Demand (Optional)

REST allows client functionality to be extended by downloading and executing

code in the form of applets or scripts

Code on Demand (Optional) Improved extensibility Simplified clients

Benefits

Uniform Interface

Generalized interface between components. All of the components communicate through this interface

Uniform Interface Improved visibility Overall simplicity Implementation decoupled from

services

Benefits

Uniform Interface

BUT!Performance suffers!

Uniform InterfaceIdentification of

resourcesManipulation of

resources through representations

Self-descriptive messages

Hypermedia as the engine of application

state

Uniform InterfaceResources

Resource is any concept that might be target of an author`s hypertext reference

Uniform InterfaceResources

DOCUMENT, IMAGE, PERSON, COLLECTION OF FAVOURITE MOVIES, E.T.C.

Uniform InterfaceResources

resource to set map Static semantics Many-to-Many

Uniform InterfaceResources

Don`t use database schema as a basis for Web API for couple of reasons

API clients care about application semantics not database schema, which is different thing

Changes to the database schema become impossible

Uniform InterfaceResource Identification

Not a word about URL format!It doesn`t matter!

Uniform InterfaceResource Identification

Hardcoded URLs or URL templates is a form of

coupling!

Uniform InterfaceResource Identification

URI Templates (RFC 6570)http://example.org/~{username}/{term:1}/{term}{?

q*,lang} { username: "rodneyrehm",      term: "hello world", q: {          a: "mars",          b: "jupiter“     },     lang: "en" }

+

=http://example.org/~rodneyrehm/h/hello%20world?a=mars&b=jupiter&lang=en

Uniform InterfaceManipulation of resources through representations

A representation is a sequence of bytes, plus representation metadata to describe those bytes. Representation captures the state of

resource.

Uniform InterfaceManipulation of resources through representations

Can be any sequence of bytes Resource can have multiple representations Content negotiation is needed

Uniform InterfaceManipulation of resources through representations

Do I violate REST if I pick HTML as representation

format? NO YOU DON`T

Uniform InterfaceHATEOAS

HATEOAS!Is it a Greek got?

Uniform InterfaceHATEOAS

Hypermedia as the engine of application state!

Uniform InterfaceHATEOAS

You already know about Application State, right?

At least I told you 10 minutes ago :)

Uniform InterfaceHATEOAS

Hypermedia connects resources to each other, and describes their

capabilities in machine-readable ways

Uniform InterfaceHATEOAS

What to do next?

Uniform InterfaceHATEOAS

What to do next?

{     "appnews": {         "appid": 440,         "newsitems": [{             "gid": "3189986872441392984",             "title": "Team Fortress 2 Update Released",             "url": "http://store.steampowered.com/news/13634/",             "is_external_url": false,             "author": "Valve",             "feedlabel": "Product Update",             "date": 1402535460,             "feedname": "steam_updates“         }]     }}

Uniform InterfaceHATEOAS

“Hypermedia connects resources to each other, and describes their capabilities in machine-readable

ways”

- Roy Fielding

Uniform InterfaceHATEOAS

Most of the “RESTful” Web APIs are not RESTful, because of lack of

hypermedia

Uniform InterfaceHATEOAS

Decoupling between client and server Resilience to API changes Enhanced usability (no hardcode and doc issues)

Why hypermedia is important for Web APIs?

Uniform InterfaceHypermedia Aware Media Types

Siren – generic hypermedia format

{     "class": ["owner", "vip"],     "properties": {         "name": "Kevin“     },     "entities": [       {           "rel": ["https://rels.x.io/dog"],           "href": https://api.x.io/dogs/1       }     ],     "actions": [       {           "name": "adopt",           "method": "POST",           "href": "https://api.x.io/owners/1/dogs",           "fields": [{ "name": "dog-name", "type": "text" }]       }     ],     "links": [       { "rel": ["self"], "href": "https://api.x.io/owners/1" }     ] }

LinkLink Relation

Uniform InterfaceHypermedia Aware Media Types

SIREN, HAL, COLLECTION+JSON, JSON-LD, HYDRA, OData, AtomPub, HTML

W3C Spec

Uniform InterfaceHATEOAS

Not a word about URL format!It doesn`t matter!

Uniform InterfaceHATEOAS

Hardcoded URLs or URL templates is a form of

coupling!

REST Flavor“REST is software design on the scale of decades:

every detail is intended to promote software longevity and independent evolution. Many of the

constraints are directly opposed to short-term efficiency”

- Roy Fielding

HTTP only?REST is not bound to HTTPOther implementations are: CoAP, Waka

Richardson Maturity ModelNot only verbs but all HTTP facilities

Richardson Maturity Model

Don`t reinvent the wheel, stick with capabilities of application protocol of your choice (HTTP

mostly)HTTP Verbs, Status Codes, Headers

REST Religion

REST is not a religion! Treat it as a reference network architecture of the Web

LinksRoy`s Fielding dissertation: http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

API Academy:http://www.apiacademy.co/

Parse.com https://parse.com/

Hypertext Driven API: http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

Thank you!Forward your questions

to alexei.skachykhin@live.com