Representational State Transfer

61
Representation al State Transfer Alexei Skachykhin – Software Engineer iTechArt, 2014

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

Page 1: Representational State Transfer

Representational State Transfer

Alexei Skachykhin – Software EngineeriTechArt, 2014

Page 2: Representational State Transfer

The Internet Of Things Explosion of WWW Smart Electronics Web APIs

Page 3: Representational State Transfer

Painting Style

What painting style is it?

Cubism? Surrealism?

Page 4: Representational State Transfer

REST

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

Page 5: Representational State Transfer

REST

NO SPEC! NO PROTOCOL!

Page 6: Representational State Transfer

Who coined the term?Roy Fielding

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

Page 7: Representational State Transfer

Who else?Mike Amundsen Leonard Richardson

Page 8: Representational State Transfer

Architectural Style

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

architectural properties (benefits)

Page 9: Representational State Transfer

Architectural Style

Feature-driven Architectural

Style

Page 10: Representational State Transfer

Architectural Style

Constraints-driven Architectural Style

Page 11: Representational State Transfer

Properties of REST

Heterogeny Scalability Evolvability

Visibility Reliability Efficiency

Page 12: Representational State Transfer

ConstraintsClient-Server Stateless Cacheable

Layered System

Code on Demand

Uniform Interface

Optional constraint

Page 13: Representational State Transfer

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.

Page 14: Representational State Transfer

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

Benefits

Page 15: Representational State Transfer

Client-Server

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

NOT AT ALL ACTUALLY

Page 16: Representational State Transfer

Client-Server

How does WebRTC fit into REST?IT DOESN`T

Page 17: Representational State Transfer

Stateless

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

to understand the request

Page 18: Representational State Transfer

Stateless

Application State

Resource State

Page 19: Representational State Transfer

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

failures) Visibility

Benefits

Page 20: Representational State Transfer

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

Disadvantages

Page 21: Representational State Transfer

Cacheable

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

explicitly labeled as cacheable or non-cacheable

Page 22: Representational State Transfer

Cacheable Improved efficiency Improved scalability Improved user perceived performance

Benefits

Page 23: Representational State Transfer

Layered system

Allows an architecture to be composed of hierarchical layers

Page 24: Representational State Transfer

Layered system Bound complexity Improved scalability (load balancing)

Benefits

Page 25: Representational State Transfer

Layered system Increased latency User-perceived performance may

degrade

Disadvantages

Page 26: Representational State Transfer

Code on Demand (Optional)

REST allows client functionality to be extended by downloading and executing

code in the form of applets or scripts

Page 27: Representational State Transfer

Code on Demand (Optional) Improved extensibility Simplified clients

Benefits

Page 28: Representational State Transfer

Uniform Interface

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

Page 29: Representational State Transfer

Uniform Interface Improved visibility Overall simplicity Implementation decoupled from

services

Benefits

Page 30: Representational State Transfer

Uniform Interface

BUT!Performance suffers!

Page 31: Representational State Transfer

Uniform InterfaceIdentification of

resourcesManipulation of

resources through representations

Self-descriptive messages

Hypermedia as the engine of application

state

Page 32: Representational State Transfer

Uniform InterfaceResources

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

Page 33: Representational State Transfer

Uniform InterfaceResources

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

Page 34: Representational State Transfer

Uniform InterfaceResources

resource to set map Static semantics Many-to-Many

Page 35: Representational State Transfer

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

Page 36: Representational State Transfer

Uniform InterfaceResource Identification

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

Page 37: Representational State Transfer

Uniform InterfaceResource Identification

Hardcoded URLs or URL templates is a form of

coupling!

Page 38: Representational State Transfer

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

Page 39: Representational State Transfer

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.

Page 40: Representational State Transfer

Uniform InterfaceManipulation of resources through representations

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

Page 41: Representational State Transfer

Uniform InterfaceManipulation of resources through representations

Do I violate REST if I pick HTML as representation

format? NO YOU DON`T

Page 42: Representational State Transfer

Uniform InterfaceHATEOAS

HATEOAS!Is it a Greek got?

Page 43: Representational State Transfer

Uniform InterfaceHATEOAS

Hypermedia as the engine of application state!

Page 44: Representational State Transfer

Uniform InterfaceHATEOAS

You already know about Application State, right?

At least I told you 10 minutes ago :)

Page 45: Representational State Transfer

Uniform InterfaceHATEOAS

Hypermedia connects resources to each other, and describes their

capabilities in machine-readable ways

Page 46: Representational State Transfer

Uniform InterfaceHATEOAS

What to do next?

Page 47: Representational State Transfer

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“         }]     }}

Page 48: Representational State Transfer

Uniform InterfaceHATEOAS

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

ways”

- Roy Fielding

Page 49: Representational State Transfer

Uniform InterfaceHATEOAS

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

hypermedia

Page 50: Representational State Transfer

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?

Page 51: Representational State Transfer

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

Page 52: Representational State Transfer

Uniform InterfaceHypermedia Aware Media Types

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

W3C Spec

Page 53: Representational State Transfer

Uniform InterfaceHATEOAS

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

Page 54: Representational State Transfer

Uniform InterfaceHATEOAS

Hardcoded URLs or URL templates is a form of

coupling!

Page 55: Representational State Transfer

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

Page 56: Representational State Transfer

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

Page 57: Representational State Transfer

Richardson Maturity ModelNot only verbs but all HTTP facilities

Page 58: Representational State Transfer

Richardson Maturity Model

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

mostly)HTTP Verbs, Status Codes, Headers

Page 59: Representational State Transfer

REST Religion

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

Page 60: Representational State Transfer

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

Page 61: Representational State Transfer

Thank you!Forward your questions

to [email protected]