Hypermedia api (HATEOAS)

36
Hypermedia API Pavel Mitin 2012 Anadea Inc.

description

 

Transcript of Hypermedia api (HATEOAS)

Page 1: Hypermedia api (HATEOAS)

Hypermedia APIPavel Mitin

2012 Anadea Inc.

Page 2: Hypermedia api (HATEOAS)

Why does it matter

API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API, API..

Page 3: Hypermedia api (HATEOAS)

Problem

Existing APIs are often:● unclear● fragile● not extendable

Page 4: Hypermedia api (HATEOAS)

Possible solution

“WWW is fundamentally a distributed hypermedia application.” Richard Taylor

Page 5: Hypermedia api (HATEOAS)

Possible solution

“WWW is fundamentally a distributed hypermedia application.” Richard Taylor

And the application works!

Page 6: Hypermedia api (HATEOAS)

How WEB works

Browsers know how to:● deal with media types (text/html,

image/png...)● navigate via links and submit forms

Page 7: Hypermedia api (HATEOAS)

How WEB works

Browsers know how to:● deal with media types (text/html,

image/png...)● navigate via links and submit forms

Let’s follow the example!!!

Page 8: Hypermedia api (HATEOAS)

Example -- eBay APIGET .../item/180881974947

{

"name" : "Monty Python and the Holy Grail white rabbit big pointy teeth",

"id" : "180881974947",

...

"links" : [

{ “type: "application/vnd.ebay.item",

"rel": "Add item to watchlist",

"href": "https://.../user/12345678/watchlist/180881974947"},

{ // and a whole lot of other operations ]}

Source: http://www.slideshare.net/josdirksen/rest-from-get-to-hateoas

Page 9: Hypermedia api (HATEOAS)

Media types

● application/atom+xml● image/jpeg● audio/vnd.wave● application/vnd.buyfolio.listing+json

Page 10: Hypermedia api (HATEOAS)

Hypermedia links

● Embed Links○ <img src="..." />

○ <x:include href"..." />

Page 11: Hypermedia api (HATEOAS)

Hypermedia links

● Embed Links○ <img src="..." />

○ <x:include href"..." />

● Outbound Links○ <a href="..." target="_blank">...</a>

Page 12: Hypermedia api (HATEOAS)

Hypermedia links

● Embed Links○ <img src="..." />

○ <x:include href"..." />

● Outbound Links○ <a href="..." target="_blank">...</a>

● Templated Links ○ <form method="get" action="...">...</form>

○ <link href="http://www.example.org/?search={search}" />

Page 13: Hypermedia api (HATEOAS)

Hypermedia links

● Embed Links○ <img src="..." />

○ <x:include href"..." />

● Outbound Links○ <a href="..." target="_blank">...</a>

● Templated Links ○ <form method="get" action="...">...</form>

○ <link href="http://www.example.org/?search={search}" />

● Idempotent Links○ <link rel="edit" href="http://example.org/edit/1"/>

○ (new XMLHttpRequest()).open("DELETE", "/tags/"+id);

Page 14: Hypermedia api (HATEOAS)

Hypermedia links

● Embed Links○ <img src="..." />

○ <x:include href"..." />

● Outbound Links○ <a href="..." target="_blank">...</a>

● Templated Links ○ <form method="get" action="...">...</form>

○ <link href="http://www.example.org/?search={search}" />

● Idempotent Links○ <link rel="edit" href="http://example.org/edit/1"/>

○ (new XMLHttpRequest()).open("DELETE", "/tags/"+id);

● Non-Idempotent Links○ <form method="post" action="...">...</form>

Page 15: Hypermedia api (HATEOAS)

Hypermedia Design Elements

1. Base Format2. State Transfer3. Domain Style4. Application Flow

Page 16: Hypermedia api (HATEOAS)

Base format

1. JSON2. XML3. XHTML/HTML54. Others (YAML, CSV, Markdown, Protocol

Buffers)

Page 17: Hypermedia api (HATEOAS)

State Transfer (client-initiated)

1. None (i.e. read-only)2. Predefined (via external documentation)3. Ad-Hoc (via in-message hypermedia

controls)

Page 18: Hypermedia api (HATEOAS)

Domain Style

1. Specific2. General3. Agnostic

Page 19: Hypermedia api (HATEOAS)

Domain Style

<!-- domain-specific design -->

<story>

<id>...</id>

<estimate>...</estimate>

</story>

Page 20: Hypermedia api (HATEOAS)

Domain Style

/* domain-general design */

{

"collection" : {

"items" : {

{ "key" : "id",

"value" : "12345" }

...

Page 21: Hypermedia api (HATEOAS)

Domain Style

<!-- domain-agnostic design -->

<ul class="order">

<li class="id">...</li>

<ul class="shipping-address">

<li class="street-address">...</li>

...

Page 22: Hypermedia api (HATEOAS)

I haven't mentioned

● Resources● Http verbs● Http status codes

Page 23: Hypermedia api (HATEOAS)

Surfing your API

Source: Building Hypermedia APIs with HTML5 and Node by Mike Amundsen

Page 24: Hypermedia api (HATEOAS)

XHTML as a base format

● Native support for links and forms● Ubiquitous client (browser)● XHTML is XML● HAML :)

Page 25: Hypermedia api (HATEOAS)

Extending vs versioning

Reasons for a new version:● A change that alters the meaning or

functionality of an existing feature or element

● A change that causes an existing element to disappear or become disallowed

● A change that converts an optional element into a required element

Page 26: Hypermedia api (HATEOAS)

Javascript RIA

;)

Page 27: Hypermedia api (HATEOAS)

Criticism

Usually compared to "traditional" REST API

Page 28: Hypermedia api (HATEOAS)

Criticism

● Overcomplicated client● Extra traffic

Page 29: Hypermedia api (HATEOAS)

Rails and HATEOAS

● lack of abstractions for hypermedia controls

Page 30: Hypermedia api (HATEOAS)

Rails and HATEOAS

● lack of abstractions for hypermedia controls● confusing terms:

○ #resource are two different resources

Page 31: Hypermedia api (HATEOAS)

Rails and HATEOAS

● lack of abstractions for hypermedia controls● confusing terms:

○ #resource are three different resources ● OPTIONS verb

Page 32: Hypermedia api (HATEOAS)

Rails and HATEOAS

● lack of abstractions for hypermedia controls● confusing terms:

○ #resource are three different resources ● OPTIONS verb● PATCH verb

Page 33: Hypermedia api (HATEOAS)

Rails and HATEOAS

● lack of abstractions for hypermedia controls● confusing terms:

○ #resource are three different resources ● OPTIONS verb● PATCH verb● PUT verb

○ upsert○ whole resource state

Page 34: Hypermedia api (HATEOAS)

Rails and HATEOAS

● lack of abstractions for hypermedia controls● confusing terms:

○ #resource are three different resources ● OPTIONS verb● PATCH verb● PUT verb

○ upsert○ whole resource state

● custom media types require custom code

Page 35: Hypermedia api (HATEOAS)

Richardson Maturity Model

Source: http://martinfowler.com/articles/richardsonMaturityModel.html

Page 36: Hypermedia api (HATEOAS)

Books