77
What is Hypermedia API?

Hypermedia API

Embed Size (px)

DESCRIPTION

Marat Kamenschykov presentation at Rubyshift 2012.

Citation preview

Page 1: Hypermedia API

What is Hypermedia API?

Page 2: Hypermedia API

client code <=> API code

Page 3: Hypermedia API

API:

1. namespace

2. its functions

3. their params

Page 4: Hypermedia API

Web API:

1. domain (https://graph.facebook.com/)

2. addresses (/users, /groups, etc)

3. params for them (?access_token=...&...)

Page 5: Hypermedia API

The main API problem:

Client-server coupling

Page 6: Hypermedia API

Change in domain, URL, params →clients brake

Page 7: Hypermedia API
Page 8: Hypermedia API
Page 9: Hypermedia API

WWW exists 20 years.

Clients don't brake

Page 10: Hypermedia API

You can upgrade at any time.

Page 11: Hypermedia API
Page 12: Hypermedia API

Hypermedia API =Web API +

Web experience

Page 13: Hypermedia API

Hypermedia API =1 URL

https://api.github.com/https://api.twilio.com/2010-04-01

Page 14: Hypermedia API

Where are the URLs and params?

Page 15: Hypermedia API

MIME type description

Page 16: Hypermedia API

application/vnd.github-issue.text+json

application/json

application/vnd.tekpub.productions+json

application/vnd.spire-io.session+json

Page 17: Hypermedia API

RFC2119:

MUST, SHALL, REQUIRED

SHOULD, RECOMMENDED

MAY, OPTIONAL

MUST NOT, SHALL NOT

SHOULD NOT, NOT RECOMMENDED

Page 18: Hypermedia API

E.g.:

Response representations MUST begin with <root> element as the first element

Page 19: Hypermedia API

Servers SHOULD return a response code of 204 if the HTTP DELETE request was

successful.

Page 20: Hypermedia API

Document MAY contain a single filters object.

Five properties: href(REQUIRED), rel (REQUIRED), name (OPTIONAL), prompt

(OPTIONAL), and a data array (OPTIONAL)

Page 21: Hypermedia API

"filters":[

{"rel":"category", "href":"...", "prompt":"",

"data":[

{"name":"category",

"value":"Microsoft"}]},

{"rel":"category", "href":"...", "prompt":"",

"data":[{"name":"category",

"value":"Ruby"}]}]

Page 22: Hypermedia API

"filters":[

{"rel":"category", "href":"...", "prompt":"",

"data":[

{"name":"category",

"value":"Microsoft"}]},

{"rel":"category", "href":"...", "prompt":"",

"data":[{"name":"category",

"value":"Ruby"}]}]

Page 23: Hypermedia API

Rels are described in MIME

Page 24: Hypermedia API

Best MIME type is XHTML: it supports forms!

Page 25: Hypermedia API
Page 26: Hypermedia API

3 whales:HTTP + MIME + HATEOAS

Page 27: Hypermedia API

HATEOAS: hypermedia as the engine of application state

Page 28: Hypermedia API
Page 29: Hypermedia API

<root>

<id>q0</id>

<link rel = "q1"

uri = "..." />

<link rel = "q2"

uri = "..." />

</entry>

<root>

<id>q1</id>

<link rel = "q0"

uri = "..." />

<link rel = "q3"

uri = "..." />

</entry>

Page 30: Hypermedia API

ticket: {

assigned_to: someone,

links: [

{rel: “unassign”, url: “...”},

{rel: “close”, url: “...”}

]

}

Page 31: Hypermedia API

ticket: {

assigned_to: null,

links: [

{rel: “assign”, url: “...”},

{rel: “remove”, url: “...”}

]

}

Page 32: Hypermedia API

*MIME types are registered in IANA w/ public access

Page 33: Hypermedia API

HTTP:

Page 34: Hypermedia API

HTTP verbs:

GET

HEAD

POST

PUT

PATCH

DELETE

OPTIONS

Page 35: Hypermedia API

HTTP verbs:

GET

HEAD

POST

PUT

PATCH

DELETE

OPTIONS

Page 36: Hypermedia API

Status codes:

100.upto(505).almost_each do |status_code|…end

Page 37: Hypermedia API

100 Continue

201 Created

202 Accepted

206 Partial Content

303 See Other

400 Bad Request

401 Unauthorized

404 Not Found

409 Conflict

412 Precondition Failed

417 Expectation Failed

Page 38: Hypermedia API

100 Continue

201 Created

202 Accepted

206 Partial Content

303 See Other

400 Bad Request

401 Unauthorized

404 Not Found

409 Conflict

412 Precondition Failed

417 Expectation Failed

Page 39: Hypermedia API

100 Continue

201 Created

202 Accepted

206 Partial Content

303 See Other

400 Bad Request

401 Unauthorized

404 Not Found

409 Conflict

412 Precondition Failed

417 Expectation Failed

Page 40: Hypermedia API

100 Continue

201 Created

202 Accepted

206 Partial Content

303 See Other

400 Bad Request

401 Unauthorized

404 Not Found

409 Conflict

412 Precondition Failed

417 Expectation Failed

Page 41: Hypermedia API

100 Continue

201 Created

202 Accepted

206 Partial Content

303 See Other

400 Bad Request

401 Unauthorized

404 Not Found

409 Conflict

412 Precondition Failed

417 Expectation Failed

Page 42: Hypermedia API

100 Continue

201 Created

202 Accepted

206 Partial Content

303 See Other

400 Bad Request

401 Unauthorized

404 Not Found

409 Conflict

412 Precondition Failed

417 Expectation Failed

Page 43: Hypermedia API

Request headers:

OPTIONS /payment/order/1234 HTTP 1.1 Host: starbucks.example.com

Response

200 OK Allow: GET, PUT

Page 44: Hypermedia API

Request headers:

PUT /order/1234 HTTP 1.1 Host: starbucks.example.com Expect: 100-Continue

Response:

100 Continue

Page 45: Hypermedia API

Request:

PUT /order/1234 HTTP 1.1 Host: starbucks.example.com {body}

Response:

200 OK

{body}

Page 46: Hypermedia API

Request:

PUT /order/1234 HTTP 1.1 Host: starbucks.example.com

{body}

Response:

409 Conflict

Page 47: Hypermedia API

Request:

PUT /order/1234 HTTP 1.1 Host: starbucks.example.com Expect: 100-Continue

Response:

417 Expectation Failed

Page 48: Hypermedia API

HTTP Headers:Accept/typeEtagCacheAuthorizationVersionIf-Unmodified-SinceIf-Match

Page 49: Hypermedia API

HTTP Headers:Accept/typeEtagCacheAuthorizationVersionIf-Unmodified-SinceIf-Match

Page 50: Hypermedia API

Media types (revisited):Accept: application/xmlAccept: application/json

Page 51: Hypermedia API

GET https://api.github.com/gists/1

Accept: application/json

200 OK

Content-Type: application/json; charset=utf-8

(response body)

Page 52: Hypermedia API

GET https://api.github.com/gists/1

Accept: application/xml

200 OK

Content-Type: application/xml; charset=utf-8

(response body)

Page 53: Hypermedia API

GET https://api.github.com/gists/1

Accept: application/xml

406 Not Acceptable

Content-Type: application/json

{

"message": "Must ACCEPT application/json: [\"application/xml\"]"

}

Page 54: Hypermedia API

HTTP Headers:Accept/typeEtagCacheAuthorizationVersionIf-Unmodified-SinceIf-Match

Page 55: Hypermedia API

GET https://api.github.com/gists/1

Accept: application/json

200 OK

ETag: "2259b5bea67655550030acf98bad4184"

{body}

GET https://api.github.com/gists/1

Accept: application/json

If-None-Match: "2259b5bea67655550030acf98bad4184"

304 Not Modified

Page 56: Hypermedia API

HTTP Headers:Accept/typeEtagCacheAuthorizationVersionIf-Unmodified-SinceIf-Match

Page 57: Hypermedia API

Authentication:Basic HTTP Authentication

(with SSL or Digesting)

Page 58: Hypermedia API

HTTP Headers:Accept/typeEtagCacheAuthorizationVersionIf-Unmodified-SinceIf-Match

Page 59: Hypermedia API

Accept: application/vnd.example+json

Accept: application/vnd.example+json;version=1.0

Accept: application/vnd.example-v2+json

Start point URI remains: http://api.example.com

Page 60: Hypermedia API

HTTP Headers:Accept/typeEtagCacheAuthorizationVersionIf-Unmodified-SinceIf-Match

Page 61: Hypermedia API

Richardson Maturity Model

Page 62: Hypermedia API

1. “The Swamp of POX.” You’re using HTTP to make RPC calls. HTTP is only really used

as a tunnel.

http://api.example.com?post_id=1&user_id=2

Page 63: Hypermedia API

2. Resources. Rather than making every call to a service endpoint, you have multiple

endpoints.http://api.example.com/posts/edit/1

http://api.example.com/users/show/1

Page 64: Hypermedia API

3. HTTP Verbs. GET http://api.example.com/posts/1

PUT http://api.example.com/posts/1

PATCH http://api.example.com/posts/1

POST http://api.example.com/posts

DELETE http://api.example.com/posts/1

Page 65: Hypermedia API

4. Hypermedia Controls. HATEOAS. You’re 100% REST compliant.

GET https://api.example.com/HTTP 1.1

Accept: application/vnd.example-v1+jsonIf-Match: “...”

Authentication: ...

200 OK{root: {entries: ...},

links: [{rel: “edit”, url: “...”}, {…}]}

Page 66: Hypermedia API

No client-server coupling

1 stable URL

No dependencies on URLs and params

Page 67: Hypermedia API

Client is valid forever (ideally)

Page 68: Hypermedia API

Just as web browsers work

Page 69: Hypermedia API

Wish you all to be at level 4

(if you want)

Page 70: Hypermedia API

Thank you!

@maratkamen

[email protected]

Page 71: Hypermedia API

Sources:

Page 72: Hypermedia API

“Architectural Styles andthe Design of Network-based Software

Architectures”http://www.ics.uci.edu/~fielding/pubs/di

ssertation/top.htm

Page 73: Hypermedia API

http://blog.steveklabnik.com/posts/2012-02-27-hypermedia-api-reading-list

Page 74: Hypermedia API

“Building Hypermedia APIs with HTML5 and Node”

Mike Amundsen

Page 75: Hypermedia API

http://timelessrepo.com/haters-gonna-hateoas

Page 76: Hypermedia API

REST is over!http://blog.steveklabnik.com/posts/2012-02-

23-rest-is-over

Page 77: Hypermedia API

Questions!