Top Banner
Chris Marinos presents: Hypermedia APIs: The Rest of REST
37
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hypermedia APIs: The Rest of REST

Chris Marinos presents:

Hypermedia APIs: The Rest of REST

Page 2: Hypermedia APIs: The Rest of REST

The Agenda

❖ REST overview

❖ Hypermedia

❖ Affordances

❖ Demo

Page 3: Hypermedia APIs: The Rest of REST
Page 4: Hypermedia APIs: The Rest of REST
Page 5: Hypermedia APIs: The Rest of REST

Who has written or consumed a REST API?

Page 6: Hypermedia APIs: The Rest of REST

What is REST?

Page 7: Hypermedia APIs: The Rest of REST

–Chris Marinos

REST APIs are where you, like, follow HTTP and stuff

Page 8: Hypermedia APIs: The Rest of REST

Representational State Transfer (REST) is a

software architecture style consisting of guidelines

and best practices for creating scalable web

services. REST is a coordinated set of constraints

applied to the design of components in a distributed

hypermedia system that can lead to a more

performant and maintainable architecture.

Page 9: Hypermedia APIs: The Rest of REST

Representational State Transfer (REST) is a

software architecture style consisting of guidelines

and best practices for creating scalable web

services. REST is a coordinated set of constraints

applied to the design of components in a distributed

hypermedia system that can lead to a more

performant and maintainable architecture.

Page 10: Hypermedia APIs: The Rest of REST

REST is the architectural style of the internet

Page 11: Hypermedia APIs: The Rest of REST

REST Constraints

❖ Client-Server

❖ Stateless

❖ Cache

❖ Uniform Interface

❖ Layered System

❖ Code-On-Demand (optional)

Page 12: Hypermedia APIs: The Rest of REST

REST Benefits

❖ Client-Server -> reuse across clients

❖ Stateless -> scalability/reliability

❖ Cache -> performance

❖ Uniform Interface -> simplicity of interaction

❖ Layered System -> visibility

❖ Code-On-Demand (optional) -> extensibility of client

Page 13: Hypermedia APIs: The Rest of REST

No HTTP?

Page 14: Hypermedia APIs: The Rest of REST

–Roy Fielding

“…if the engine of application state (and hence the

API) is not being driven by hypertext, then it cannot

be RESTful and cannot be a REST API. Period.”

Page 15: Hypermedia APIs: The Rest of REST

The Agenda

❖ REST overview

❖ Hypermedia

❖ Affordances

❖ Demo

Page 16: Hypermedia APIs: The Rest of REST

HATEOAS

Hypertext as the Engine of Application State

(also a horrible acronym)

Page 17: Hypermedia APIs: The Rest of REST

So what is hypermedia?

Page 18: Hypermedia APIs: The Rest of REST

Richardson Maturity Model

Level 0: Pox

Level 1: Resources

Level 2: HTTP Verbs

Level 3: Hypermedia Controls

REST

Page 19: Hypermedia APIs: The Rest of REST

Level 0: POX

POST /myservice/

{

method: ‘get_customer’

customer_id: 1235

}

POST /myservice/

<?xml version="1.0"?>

<soap:Envelope

xmlns:soap="http://www.w3.org/2001/12/soap-envelope"

soap:encodingStyle="http://www.w3.org/2001/12/soap-

encoding">

<soap:Body xmlns:m="http://www.example.org/stock">

<m:GetCustomer>

<m:CustomerId>1234</m:CustomerId>

</m:GetCustomer>

</soap:Body>

</soap:Envelope>

Page 20: Hypermedia APIs: The Rest of REST

Level 1: Resources

POST /customer/

{ method: ‘get_customer’, customer_id: 1234}

POST /organization/

{ method: ‘get_organization’, organization_id: 1234}

POST /organization/

{

method: ‘create_organization’,

organization_id: 1234,

name: ‘new name’

}

Page 21: Hypermedia APIs: The Rest of REST

Level 2: Verbs

GET /organizations/

————————

{

organization_id: 1234

name: ‘Cool Stuff Inc.’

}

POST /organizations/

{

organization_id: 999

name: ‘My New Organization’

}

GET /organizations/999

————————

{

organization_id: 999

name: ‘My New Organization’

}

Page 22: Hypermedia APIs: The Rest of REST

Level 3: Hypermedia

GET /organizations/

<form name="input" method=“post" action=“/organizations/“>

Name: <input type="text" name=“organization_name">

<input type="submit" value="Submit">

</form>

POST /organizations/

{

organization_id: 1234

name: ‘My RESTful Organization’

}

Page 23: Hypermedia APIs: The Rest of REST

The Agenda

❖ REST overview

❖ Hypermedia

❖ Affordances

❖ Demo

Page 24: Hypermedia APIs: The Rest of REST

“A REST API should be entered with no prior

knowledge beyond the initial URI (bookmark) and set

of standardized media types that are appropriate for

the intended audience (i.e., expected to be

understood by any client that might use the API)”

–Roy Fielding

Page 25: Hypermedia APIs: The Rest of REST

Affordances

Page 26: Hypermedia APIs: The Rest of REST

State transitions over URL structure

Page 27: Hypermedia APIs: The Rest of REST

Hypermedia Benefits

• API discoverability and documentation

• Reuse of state transition logic across clients

• Ease of client development

• More flexibility for servers to evolve

Page 28: Hypermedia APIs: The Rest of REST

Hypermedia Formats

HTML

XHTML

Collection+JSON

Siren

HAL

JSON-LD

NOT JSON or XML

Page 29: Hypermedia APIs: The Rest of REST

Collection+JSON

{

"collection" :

{

"version" : “1.0",

"href" : “https://foo.com/api/“,

"links" : […],

"items" : […],

"queries" : […],

"template" : {…}

}

}

Page 30: Hypermedia APIs: The Rest of REST

Links

"links" : [

{

"href": “https://foo.com/api/organizations/",

"rel": "organizations"

},

{

"href": “https://foo.com/api/customers/",

"rel": "customers"

},

],

Page 31: Hypermedia APIs: The Rest of REST

Items

"items": [

{

"href": “https://foo.com/api/organizations/1/",

"data": [

{

"name": “organization_id”,

"value": “my_org”

}

],

"links": [

{

“href": “https://foo.com/api/organizations/1/customers“,

"rel": "customers"

},

]

}

]

Page 32: Hypermedia APIs: The Rest of REST

Queries

"queries" : [

{"rel" : "search",

"href" : “https://foo.com/api/customers/search”,

"data" : [

{"name" : “last_name", "value" : ""}

]

}

],

Page 33: Hypermedia APIs: The Rest of REST

Template

"template" : {

"data" : [

{"name" : “fist_name”, "value" : "", "prompt" : “First name”},

{"name" : “last_name”, "value" : "", "prompt" : “Last name"},

]

}

Page 34: Hypermedia APIs: The Rest of REST

The Agenda

❖ REST overview

❖ Hypermedia

❖ Affordances

❖ Demo

Page 35: Hypermedia APIs: The Rest of REST

Who’s using this stuff?

Page 36: Hypermedia APIs: The Rest of REST

Who has written or consumed a REST API?

Page 37: Hypermedia APIs: The Rest of REST

Questions?

❖ API Craft Google group/mailing list

❖ RESTful Web APIs (Richardson/Amundsen)

❖ Roy Fielding’s thesis/blog

❖ Hypermedia formats (HAL, Siren, Hydra, CJ, JSON-LD,

UBER, etc.)

❖ API Craft conference in Detroit (http://apicraft.org/)