Download - Demystifying the REST API

Transcript
Page 1: Demystifying the REST API

Demystifying the REST API

Samantha Quiñones (@ieatkillerbees)

Page 2: Demystifying the REST API

@ieatkillerbees http://samanthaquinones.com

Page 3: Demystifying the REST API
Page 4: Demystifying the REST API

Engineer A person who designs, builds, or

maintains things.

Page 5: Demystifying the REST API

Pattern A model or design used to guide the

building of things.

Page 6: Demystifying the REST API

REST (Representational State Transfer)

An architectural pattern for distributed hypermedia systems.

Page 7: Demystifying the REST API

REST[ful] API A resource-oriented API built to conform

with the REST architectural pattern.

Page 8: Demystifying the REST API

Hypermedia A non-linear medium of many disparate types of information, including text, images, video, audio

and hypertext.

Page 9: Demystifying the REST API

I’m not here to tell you…

How to Build an API

Page 10: Demystifying the REST API

-Roy Fielding, REST Discussion Mailing List

“[T]he goal of my dissertation is to teach people how to think about the problem in terms of trade-offs, not

in terms of rigid repetition…”

Page 11: Demystifying the REST API

Nothing is more easy than to reduce this mass to one quarter of its bulk. You know that curious cellular matter which constitutes the elementary tissues of vegetable? This substance is found quite pure in many bodies, especially in cotton, which is nothing more than the down of the seeds of the cotton plant. Now cotton, combined with cold nitric acid, become transformed into a substance eminently insoluble, combustible, and explosive. It was first discovered in 1832, by Braconnot, a French chemist, who called it xyloidine. In 1838 another Frenchman, Pelouze, investigated its different properties, and finally, in 1846, Schonbein, professor of chemistry at Bale, proposed its employment for purposes of war. This powder, now called pyroxyle, or fulminating cotton, is prepared with great facility by simply plunging cotton for fifteen minutes in nitric acid, then washing it in water, then drying it, and it is ready for use.

Page 12: Demystifying the REST API

Nothing is more easy than to reduce this mass to one quarter of its bulk. You know that curious cellular matter which constitutes the elementary tissues of vegetable? This substance is found quite pure in many bodies, especially in cotton, which is nothing more than the down of the seeds of the cotton plant. Now cotton, combined with cold nitric acid, become transformed into a substance eminently insoluble, combustible, and explosive. It was first discovered in 1832, by Braconnot, a French chemist, who called it xyloidine. In 1838 another Frenchman, Pelouze, investigated its different properties, and finally, in 1846, Schonbein, professor of chemistry at Bale, proposed its employment for purposes of war. This powder, now called pyroxyle, or fulminating cotton, is prepared with great facility by simply plunging cotton for fifteen minutes in nitric acid, then washing it in water, then drying it, and it is ready for use.

Page 13: Demystifying the REST API

NASA/JPL-Caltech

Page 14: Demystifying the REST API

The REST Architectural Style

Page 15: Demystifying the REST API

© Henry Story, 2007 (CC BY-NC 2.0)

Page 16: Demystifying the REST API

Architectural Styles and the Design of Network-based Software Architectures

Roy T. Fielding (http://www.ics.uci.edu/~fielding/pubs/dissertation/faq.htm)

Page 17: Demystifying the REST API

MySQL

PHP 5.6

NGINX

Symfony 2 RabbitMQ

Page 18: Demystifying the REST API

WWW

Null Style

Page 19: Demystifying the REST API

Client-Server Architecture

ServerClient

Page 20: Demystifying the REST API

Statelessness

ServerClient

Client

Client

Page 21: Demystifying the REST API

Cache

ServerClient

Client

Client

Page 22: Demystifying the REST API

Uniform Interface

Server

Client

Client

Client

ServerRRSH

RRSH

Page 23: Demystifying the REST API

Layered System

Server

Client

Client

Client

ServerRRSH

RRSH

Gatew

ay

Proxy

Page 24: Demystifying the REST API

Code On Demand

Server

Client

Client

Client

ServerRRSH

RRSH

Gatew

ay

Proxy

Page 25: Demystifying the REST API

The Uniform Interface

Page 26: Demystifying the REST API

Resource A thing, or a collection of things that…

May be concrete, or abstract… May be static, or vary over time… Has a name that is unique…

Uniform Interface

Page 27: Demystifying the REST API

Uniform Interface

Page 28: Demystifying the REST API

Resource Identifier A unique name which is…

A conceptual mapping to a resource… Or a collection of resources…

Uniform Interface

Page 29: Demystifying the REST API

http://galaxies.tembies.com/groups/local/galaxies/ngc4258.json

“The most recent 10 tweets containing the hashtag ‘#MWPHP’”

Uniform Interface

Page 30: Demystifying the REST API

Representation A sequence of bytes that…

Contains the state of a resource… Consists of data… Metadata about the data… Metadata about the metadata…

Uniform Interface

Page 31: Demystifying the REST API

{ "name": "Messier 106", "ngc_id": 4258, "type": "SAB(s)bc", "size": 135000 }

Uniform Interface

Page 32: Demystifying the REST API

Content-Type: application/json

{ "name": "Messier 106", "ngc_id": 4258, "type": "SAB(s)bc", "size": 135000 }

Uniform Interface

Page 33: Demystifying the REST API

Content-Type: application/json Cache-Control: max-age=3600 public

{ "name": "Messier 106", "ngc_id": 4258, "type": "SAB(s)bc", "size": 135000 }

Uniform Interface

Page 34: Demystifying the REST API

Self-Contained Messages Contains all information necessary to…

Process the request… Return an appropriate representation…

Uniform Interface

Page 35: Demystifying the REST API

Hypermedia As The Engine Of Application State

Page 36: Demystifying the REST API

<galaxy> <ngc_id>4258</ngc_id> <name/>Messier 106</name> <type>SAB(s)bc</type> <size>135000</size> <link rel="group" href="/groups/local" /> <link rel="self" href="/groups/local/galaxies/ngc4258" /> </galaxy>

Page 37: Demystifying the REST API

Hypermedia Formats

HTML XML

CSS Atom

Page 38: Demystifying the REST API

JSON is not a Hypermedia Format

Page 39: Demystifying the REST API

JSON Hypermedia

Collection+JSON JSON-LD

JSON:API UBER

JSON+HAL

Page 40: Demystifying the REST API

Hypertext Application Language A proposed (2012) standard that…

Expresses relationships between resources… Supports JSON (JSON+HAL)… Supports XML (XML+HAL)…

Page 41: Demystifying the REST API

{ "name": "Messier 106", "ngc_id": 4258, "type": "SAB(s)bc", "size": 135000, "_links": { "self": { "href": "/groups/local/galaxies/ngc4258" } }, "group": { "href": "/groups/local" } } } }

Page 42: Demystifying the REST API

HTTP (Hyper Text Transport Protocol)

Page 43: Demystifying the REST API

Version 0.9 (1991)

Version 1.0 (1996)

Version 1.1 (1999)

Page 44: Demystifying the REST API

GET

POST

OPTIONS

HEAD

DELETE

TRACE

PUT

PATCH

CONNECT

Page 45: Demystifying the REST API

IdempotentUnsafeSafe

GET POST

OPTIONS

HEAD

DELETE

TRACE

PUT PATCH

CONNECT

Page 46: Demystifying the REST API

Idempotence

A property of an operation that can be applied multiple time without changing the result, such that ƒ(ƒ(𝑥)) = ƒ(𝑥)

Page 47: Demystifying the REST API

GET

Request the representation of a resource with a given identifier

SafeIdempotent

Page 48: Demystifying the REST API

GET /groups/local/galaxies/ncg4258 HTTP/1.1 Host: galaxies.tembies.com Content-Type: application/json; charset=UTF-8

Page 49: Demystifying the REST API

HTTP/1.1 200 OK Content-Length: 207 Content-Type: application/json; charset=UTF-8

{ "name": "Messier 106", "ngc_id": 4258, "type": "SAB(s)bc", "size": 135000, "_links": { "self": { "href": "/groups/local/galaxies/ngc4258" } }, "group": { "href": "/groups/local" } } } }

Page 50: Demystifying the REST API

HEAD

Request the representation metadata and control data for a resource with a given identifier

SafeIdempotent

Page 51: Demystifying the REST API

GET /groups/local/galaxies/ncg4258 HTTP/1.1 Host: galaxies.tembies.com Content-Type: application/json; charset=UTF-8

Page 52: Demystifying the REST API

HTTP/1.1 200 OK Content-Length: 207 Content-Type: application/json; charset=UTF-8

Page 53: Demystifying the REST API

TRACE

Request the server respond by echoing the request.

SafeIdempotent

Page 54: Demystifying the REST API

TRACE /groups/local/galaxies/ncg4258 HTTP/1.1 Host: galaxies.tembies.com Content-Type: application/json; charset=UTF-8

Page 55: Demystifying the REST API

HTTP/1.1 200 OK Content-Type: message/http Content-Length: 39

TRACE /groups/local/galaxies/ncg4258 HTTP/1.1 Host: galaxies.tembies.com Content-Type: application/json; charset=UTF-8

Page 56: Demystifying the REST API

OPTIONS

Request the server respond with information about what methods are allowed for a resource.

SafeIdempotent

Page 57: Demystifying the REST API

OPTIONS /groups/local/galaxies/ncg4258 HTTP/1.1 Host: galaxies.tembies.com Content-Type: application/json; charset=UTF-8

Page 58: Demystifying the REST API

HTTP/1.1 200 OK Content-Type: message/http Content-Length: 39 Allow: GET,HEAD,DELETE,POST,OPTIONS,TRACE

Page 59: Demystifying the REST API

DELETE

Request the server delete the resource.

UnsafeIdempotent

Page 60: Demystifying the REST API

DELETE /groups/local/galaxies/ncg4258 HTTP/1.1 Host: galaxies.tembies.com Content-Type: application/json; charset=UTF-8

Page 61: Demystifying the REST API

HTTP/1.1 204 No Content

Page 62: Demystifying the REST API

PUT

Request the server store a provided representation of a resource at the specified name, replacing the existing resource if it exists.

UnsafeIdempotent

Page 63: Demystifying the REST API

PUT /groups/local/galaxies/ncg4258 HTTP/1.1 Host: galaxies.tembies.com Content-Type: application/json; charset=UTF-8

{ "name": "Messier 106", "ngc_id": 4258, "type": "SAB(s)bc", "size": 135000, “example_type": "contrived", "_links": { "self": { "href": "/groups/local/galaxies/ngc4258" } }, "group": { "href": "/groups/local" } } } }

Page 64: Demystifying the REST API

HTTP/1.1 204 No Content

Page 65: Demystifying the REST API

POST

Request the server store a provided representation of a resource as a new member of the collection identified by the resource name

UnsafeNot Idempotent

Page 66: Demystifying the REST API

POST /groups/local/galaxies HTTP/1.1 Host: galaxies.tembies.com Content-Type: application/json; charset=UTF-8

{ "name": "Messier 51a", "ngc_id": 5194, "type": “SA(s)bc pec", "size": 60000, }

Page 67: Demystifying the REST API

HTTP/1.1 201 Created Location: /groups/local/galaxies/ngc4258 { "_links": {

"self": { "href": "/groups/local/galaxies/ngc4258" } } } }

Page 68: Demystifying the REST API

PUT vs POST

or How Yelling Loudly Can Make a

Difference

Page 69: Demystifying the REST API

PUT

Create or replace… a Resource or collection

Can be repeated… safely

POST

Append to a collection

Can not be repeated… safely

Page 70: Demystifying the REST API

Idempotency

ServerClient

PUTPUTPUT

Page 71: Demystifying the REST API

Idempotency

ServerClient

POSTPOSTPOST

Page 72: Demystifying the REST API

Antipattern

A common solution to an engineering problem that is, at best ineffective, and at worst, potentially destructive.

Page 73: Demystifying the REST API

Overloading GET (or POST)

GET /things/42?action=delete GET /widgets/42/delete GET /widgets/delete?id=42

Antipattern

Page 74: Demystifying the REST API

Abusing Status Codes

HTTP/1.1 200 OK { "success": false, "code": "ID10T", "message": "Tweet @jakeasmith for lols" }

Antipattern

Page 75: Demystifying the REST API

Abusing Status Codes

Antipattern

1xx - Informational

2xx - Success

3xx - Redirection

4xx - Client Error

5xx - Server Error

Page 76: Demystifying the REST API

Relying on Session

REST APIs are Stateless

Antipattern

Page 77: Demystifying the REST API

Not Supporting Caching

Learn to use cache headers

Antipattern

Page 78: Demystifying the REST API

Your Humble Speaker’s Loud and Oft-Repeated Advice

Page 79: Demystifying the REST API

Sanitize Assumptions

Patterns like REST make assumptions safer.

The further you are from the consumers of your API, the closer you should adhere to

the pattern.

Advice

Page 80: Demystifying the REST API

Avoid Side-Effects

GET is a safe method. GET should never, never, cause side effects.

Advice

Page 81: Demystifying the REST API

Document your APIs

REST APIs are self-documenting.

That’s a feature, not an excuse.

Advice

Page 82: Demystifying the REST API

Understand When REST Works

Resource (noun) oriented CRUD

Open consumption Open formats

Advice

Page 83: Demystifying the REST API

And When It Doesn’t

Action (verb) oriented RPC

Closed consumption Closed formats

Advice

Page 84: Demystifying the REST API

After the Conference

Page 85: Demystifying the REST API

Give Me Some Feedback!

https://joind.in/13067

Page 86: Demystifying the REST API

Read the Dissertation

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

Page 87: Demystifying the REST API

Read the RFC

https://www.ietf.org/rfc/rfc2616.txt

Page 88: Demystifying the REST API

Learn about Caching

http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/

Page 89: Demystifying the REST API

Check Out Some Documentation Frameworks

RAML - http://raml.org API Blueprint - http://apiblueprint.org Swagger - http://swagger.io

Page 90: Demystifying the REST API

Consider Some Further Reading

Build APIs You Won't Hate by Phil Sturgeon RESTful Web Services by Leonard Richardson & Sam Ruby

Page 91: Demystifying the REST API

Build an Awesome API

Easy to maintain Meets your needs Solves a problem

Page 92: Demystifying the REST API

Tell Your Community how you built it

at a user group or a conference

Page 93: Demystifying the REST API

Other Resources• RFC 2616 “Hypertext Transfer Protocol” (http://bit.ly/LkW6OW)

• RFC 5789 “PATCH Method for HTTP” (http://bit.ly/1cFpvtq)

• JSON HAL Proposal (http://bit.ly/1kJLvgw)

• RFC 6902 “JSON PATCH” (http://bit.ly/LkWyww)

• Fielding’s Dissertation (http://bit.ly/1eTY8AI)

• REST Cookbook (http://restcookbook.com)

• HATEOAS for PHP: http://hateoas-php.org/

• Well-supported, Symfony-ready HATEOAS library

• REST APIs with Symfony2: The Right Way (http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/)