SlideShare a Scribd company logo
1 of 49
Download to read offline
HYPERMEDIA
APIS
Paulo Gandra de
Sousa
pag@isep.ipp.pt
2
ISEP/IPP
Building Hypermedia APIs with HTML5 and
Node
 Mike Amundsen
 ISBN: 978-1-4493-0657-1
RESTful web APIs
 Leonard Richardson, Mike Amundsen
 ISBN: 1449358063
HYPERMEDIA API
3
“Programming for distributed
hypermedia environments
usually means that message
transfers must carry more than
just data; they must carry
additional information including
metadata and higher-level
application flow control
options.”
Mike Amudsen 4
ISEP/IPP
RICHARDSON MATURITY
MODEL
http://martinfowler.com/articles/richardsonMaturityMod
THE HYPERMEDIA
CONSTRAINT
Hipermedia as the
engine of application
state *
* HATEOAS
HYPERMEDIA IS THE ENGINE
“hypermedia payloads carry more
information than just the data
stored on the server. Hypermedia
payloads carry two types of vital
metadata: metadata about the data
itself and metadata about the
possible options for modifying the
state of the application at that
moment.”
Mike Amudsen
7
ISEP/IPP
HYPERMEDIA API
Focus on
the media type and its hypermedia
capabilities
not on
the API calls
9
ISEP/IPP
H-FACTORS
Links
 represent opportunities for a
client to advance the state of
the application by activating a
link.
Controls
 provide support for additional
metadata when executing link
operations.
Not all media types
support all h-factors
11
ISEP/IPP
LINK H-FACTORS
H-Factor Example
LE Embed Links <img src=“…”>
LO Outbound
Links
<a href=“…”>
LT Templated
Links
<form method="get" action=“…">
<input type="text" name="search" value="" />
<input type="submit" />
</form>
LI Idempotent
Links
<link rel="edit" href=“…"/> (ATOM)
LN Non-
Idempotent
Links
<form method="post" action=“…">
<textarea name="comment"></textarea>
<input type="submit" />
</form>
12
ISEP/IPP
CONTROL H-FACTORS
H-Factor Example
CR Read Controls Accept-Language: (http header)
CU Update
Controls
<form method="post“ action=“…"
enctype="text/plain">
<textarea name="comment"></textarea>
<input type="submit" />
</form>
CM Method
Controls
<form method="post“ action=“…">
<textarea name="comment"></textarea>
<input type="submit" />
</form>
CL Link
Annotation
Controls
<link rel="stylesheet" href="..." />
13
ISEP/IPP
HYPERMEDIA MEDIA
TYPE DESIGN
14
MEDIA TYPE
Describes the message structure and semantic
Use standard media types as much as possible
 http://www.iana.org/assignments/media-types/media-types.xhtml
Not all media types are hypermedia enabled, e.g.,
image/jpeg
15
ISEP/IPP
EXAMPLE DATA (CSV)
darrel,admin,active
mike,manager,suspended
...
whartung,user,pending
16
ISEP/IPP
EXAMPLE DATA WITH
METADATA
<ul class="users">
<li class="user">
<a href="..." rel="user">darrel</a>
<span class="role">admin</span>
<span class="status">active</span>
</li>
<li class="user">
<a href="..." rel="user">mike</a>
<span class="role">manager</span>
<span class="suspended">suspended</span>
</li>
...
</ul>
17
ISEP/IPP
EXAMPLE APPLICATION
METADATA
<ul class="users">
<li class="navigation"> <a href="..." rel="next-page">next-page</a>
</li>
<li class="navigation"> <a href="..." rel="last-page">last-page</a>
</li>
<li class="user"> ... </li>
...
</ul>
<!-- defined filters -->
<ul class="queries">
<li class="query"><a href="..." rel="admins">admins</a></li>
<li class="query"><a href="..." rel="pending">pending</a></li>
<li class="query"><a href="..." rel="suspended">suspended</a></li>
</ul>
<!-- user search -->
<form name="user-search" action="...">
<input name="search-name" value="" />
<input type="submit" />
</form>
18
ISEP/IPP
HYPERMEDIA TYPE DESIGN
ELEMENTS
Base Format
(client-initiated) State Transfer
Domain Style
Application Flow
19
ISEP/IPP
BASE FORMAT
Plain text
CSV
JSON, XML,
HTML, Atom/AtomPub
…
20
ISEP/IPP
(CLIENT INITIATED) STATE
TRANSFER
None / read-only
 State is only transmitted from the server to the client
Predefined
 State transmitted from the client to the server follows predefined
payloads defined by the server and known by the client
Ad-hoc
 The server tells the client what state to trasmit. E.g., HTML <FORM>
21
ISEP/IPP
DOMAIN STYLE
Specific
 The payload has domain specific terms that are understood by client
and server, e.g., <shipping address>
General
 The payload uses some general blocks that can be reused in diferent
use cases, e.g., <adress type=“shipping”>
Agnostic
 The payload uses domain agnostic terms and (well known) decorators
to add semantic, e.g., <link rel=“edit”>
22
SPECIFIC DOMAIN STYLE
EXAMPLE
<!-- domain-specific design -->
<order>
<id>...</id>
<shipping-address>...</shipping-address>
<billing-address>...</billing-address>
...
</order>
23
ISEP/IPP
GENERAL DOMAIN STYLE
EXAMPLE
/* domain-general design */
{
"order":
{
"id" : "...",
"address" :
{"type" : "shipping", "street-address" : "..."},
"address" :
{"type" : "billing", "street-address" : "..."}
}
}
24
ISEP/IPP
AGNOSTIC DOMAIN STYLE
EXAMPLE
<!-- domain-agnostic design -->
<ul class="order">
<li class="id">...</li>
<ul class="shipping-address">
<li class="street-address">...</li>
...
</ul>
<ul class="billing-address">
<li class="street-address">...</li>
...
</ul>
...
</ul>
25
ISEP/IPP
APPLICATION FLOW
None
 The hypermedia type contains no identifiers for application flow.
Intrinsic
 The application flow identifiers are defined within the hypermedia
type itself. E.g., Atom and AtomPub define a small set of link
relations to indicate application flow (“edit”, “edit-media”, and “self”).
Applied
 The application flow identifiers are not part of the hypermedia type,
but the type designs contain allowances (usually element decorators
or attributes) for applying external values to indicate application flow.
E.g., HTML attributes: profile, id, name, rel, and class.
26
NO APPLICATION FLOW
EXAMPLE
Media type: text/uri-list
# urn:isbn:0-201-08372-8
http://www.huh.org/books/foo.html
http://www.huh.org/books/foo.pdf
ftp://ftp.foo.org/books/foo.txt
27
ISEP/IPP
INTRINSIC APPLICATION
FLOW EXAMPLE
<!-- AtomPub -->
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom">
<title>Atom-Powered Robots Run Amok</title>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<author><name>John Doe</name></author>
<content>Some text.</content>
<link rel="edit" href="http://example.org/edit/first-
post.atom"/>
</entry>
28
ISEP/IPP
APPLIED APPLICATION FLOW
EXAMPLE
<html>
<head>
<title>Payment Options</title>
<meta name="profile" content="http://www.example.com/profiles/payment.html" />
</head>
<body>
<h1>Payment Options</h1>
<p> <a href="..." rel="cancel-order">Cancel Order</a> </p>
<form href="..." name="credit-card" method="post">
<input name="card-number" value="" />
...
<input type="submit" />
</form>
<form href="..." name="purchase-order" method="post">
<input name="po-number" value="" />
...
<input type="submit" />
</form>
<form href="..." name="bank-draft" method="post">
<input name="routing-number" value="" />
...
<input type="submit" />
</form>
...
</html>
29
ISEP/IPP
AN EXAMPLE Message server
30
SIMPLE MESSAGE SERVICE
A service that allows users to post messages.
Post a new message
Update a message
Search messages by text, user, and date
Get all the messages of one user
Register a new user
Update the user data
Upload the user photo
31
ISEP/IPP
Message
User
1
0..n
MESSAGES RESOURCES
Resource The message collection
URL /message
GET
POST
PUT
DELET
E
return all messages
create new entry, returns 201
not allowed
not allowed
Resource A message (Item in the collection)
URL /message/:id
GET
POST
PUT
DELET
return specific message or 404
update existing entry or 404
overwrite existing or create new given the id
deletes the message 32
ISEP/IPP
USERS RESOURCES
Resource The user collection
URL /user
GET
POST
PUT
DELET
E
return all users
create new entry, returns 201
not allowed
not allowed
Resource An user
URL /user/:id
GET
POST
PUT
DELET
return specific user or 404
updates existing entry or 404
overwrite existing or create new given the id
deletes the user 33
ISEP/IPP
NO HYPERMEDIA
REPRESENTATIONS
Message
{
id: "a1",
text: "Sample one",
sender: "Mary",
createdOn: now,
updatedOn: now
};
User
{
id: "Mary",
name:"Mary",
email: "mary@contoso.com",
roles:[],
createdOn: now
};
34
ISEP/IPP
Missing:
• Explicit id as URI
• Explicit links to other
resources
• Application flow
HYPERMEDIA MISSING FOR
MESSAGES
Message collection
Paging links
 First page
 Next page
 Previous page
Message Item
Resource links
 User
 Message collection
Actions
 Edit the message
 Delete the message
35
ISEP/IPP
HYPERMEDIA MISSING FOR
USERS
User collection
Paging links
 First page
 Next page
 Previous page
User Item
Resource links
 User’s messages
 User’s photo
 User collection
Actions
 Edit the user
 Cancel user account
36
ISEP/IPP
SOME DESIGN DECISIONS
Base format
 JSON
State transfer
 Predefined
Domain style
 Specific
Application flow
 Intrinsic
37
ISEP/IPP
LINK RELATIONS
Don’t reinvent the weel
IANA
 http://www.iana.org/assignments/link-relations/link-relations.xhtml
Microformats
 http://microformats.org/wiki/existing-rel-values
38
ISEP/IPP
COMMON LINK RELATIONS
rel Description Definitio
n
collecti
on
The target IRI points to a resource which
represents the collection resource for the
context IRI.
[RFC657
3]
edit Refers to a resource that can be used to
edit the link's context.
[RFC502
3]
edit-
form
The target IRI points to a resource where a
submission form for editing associated
resource can be obtained.
[RFC686
1]
next Indicates that the link's context is a part of
a series, and that the next in the series is
the link target.
[HTML5]
payme Indicates a resource where payment is [RFC598 39
ISEP/IPP
Source: http://www.iana.org/assignments/link-relations/link-relations.xht
Message collection
{
MESSAGE_KEY : {
MESSAGE_REPRESENTATION
},
…
links: [
{ rel: "self",
href: URI
},
{ rel: "start",
href: URI
},
{ rel: "prev",
href: URI
},
{ rel: "next",
href: URI
}
]
}
Message Item
{
text: "Sample one",
createdOn: now,
updatedOn: now,
links: [
{ rel: "self",
href:
SERVER_ROOT+"/message/a1",
},
{ rel: "edit",
href:
SERVER_ROOT+"/message/a1",
},
{ rel: "collection",
href: SERVER_ROOT+"/message"
},
{ rel: "psidi:user",
href: SERVER_ROOT+"/user/Mary",
title: "Mary Smith"
}
],
};
User collection
{
USER_KEY : {
USER_REPRESENTATION },
…
links: [
{ rel: "self",
href: URI
},
{ rel: "start",
href: URI
},
{ rel: "prev",
href: URI
},
{ rel: "next",
href: URI
}
]
}
UserItem
{
name:"Mary Smith",
email:"mary@contoso.com",
roles:[],
createdOn: now,
links: [
{ rel: "self",
href: SERVER_ADDR+"/user/Mary",
},
{ rel: "edit",
href: SERVER_ADDR+"/user/Mary",
},
{ rel: "collection",
href: SERVER_ADDR+"/user"
},
{ rel: "psidi:user-messages",
href: SERVER_ADDR+"/message?user=Mary",
},
{ rel: "psidi:user-photo",
href: SERVER_ADDR+"/user/Mary/photo",
},
],
};
{
"a11": {
"id": "a11",
"text": "Sample one",
"senderId": "Mary",
"createdOn": "2014-12-17T18:35:00.149Z",
"updatedOn": "2014-12-17T18:35:00.149Z",
"links": [
{
"rel": "self",
"href": "http://localhost:3001/message/a11"
},
{
"rel": "edit",
"href": "http://localhost:3001/message/a11"
},
{
"rel": "collection",
42
ISEP/IPP
GET
http://localhost:3001/messag
e
43
ISEP/IPP
GET
http://localhost:3001/message/a1
1
44
ISEP/IPP
GET http://localhost:3001/user
45
ISEP/IPP
GET
http://localhost:3001/user/Mary
REPRESENTATION VS
RESOURCE
Resource (e.g., table
record)
{
id: PRIMARY_KEY
text: "Sample one",
senderId: FOREIGN_KEY TO
“users”
createdOn: now,
updatedOn: now,
};
Resource
Representation
{
text: "Sample one",
createdOn: now,
updatedOn: now,
links: [
{ rel: "self",
href: SERVER_ROOT+"/message/a1", // URI
},
{ rel: "edit",
href: SERVER_ROOT+"/message/a1", // URI
},
{ rel: "collection",
href: SERVER_ROOT+"/message“ // URI
},
{ rel: "psidi:user",
href: SERVER_ROOT+"/user/Mary", // URI
title: "Mary Smith“
}
],
};
46
CONTINUE THE ROAD TO
HYPERMEDIA
Several things are missing:
Reuse generic collection media type, e.g.,
Collection+JSON, OData
Reuse existing media types and microformats, e.g.,
hCard
“New item” template
Search templates, e.g., OpenSearch
Photo subresource: information + media
47
ISEP/IPP
CLOSINGS
48
THE HYPERMEDIA
CONSTRAINT
“The application is therefore an
engine that moves from one state to
the next by examining and
choosing from among the
alternative state transitions in the
current set of representations.”
Roy Fielding
49
ISEP/IPP
SUGGESTED READINGS
Fielding, R. “REST APIs must be hypertext-driven”.
http://roy.gbiv.com/untangled/2008/rest-apis-must-
be-hypertext-driven
Atom/AtomPub.
http://bitworking.org/projects/atom/rfc5023.html
Collection+JSON media type.
http://amundsen.com/media-types/collection/
OData. http://www.odata.org/
OpenSearch. http://www.opensearch.org/Home
HTTP PATCH. http://tools.ietf.org/html/rfc5789
50
ISEP/IPP
PROGRAMAÇÃO DE
SISTEMAS
DISTRIBUIDOS
Paulo Gandra
de Sousa
pag@isep.ipp.
pt

More Related Content

What's hot

Restful webservice
Restful webserviceRestful webservice
Restful webserviceDong Ngoc
 
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRISThe glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRISGeert Pante
 
Design Beautiful REST + JSON APIs
Design Beautiful REST + JSON APIsDesign Beautiful REST + JSON APIs
Design Beautiful REST + JSON APIsStormpath
 
Rest presentation
Rest  presentationRest  presentation
Rest presentationsrividhyau
 
RESTful Architecture
RESTful ArchitectureRESTful Architecture
RESTful ArchitectureKabir Baidya
 
REST API Recommendations
REST API RecommendationsREST API Recommendations
REST API RecommendationsJeelani Shaik
 
RESTful API Design Fundamentals
RESTful API Design FundamentalsRESTful API Design Fundamentals
RESTful API Design FundamentalsHüseyin BABAL
 
Designing REST services with Spring MVC
Designing REST services with Spring MVCDesigning REST services with Spring MVC
Designing REST services with Spring MVCSerhii Kartashov
 
Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web APIBrad Genereaux
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVCIndicThreads
 
Are you getting Sleepy. REST in SharePoint Apps
Are you getting Sleepy. REST in SharePoint AppsAre you getting Sleepy. REST in SharePoint Apps
Are you getting Sleepy. REST in SharePoint AppsLiam Cleary [MVP]
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiTiago Knoch
 
Soap and restful webservice
Soap and restful webserviceSoap and restful webservice
Soap and restful webserviceDong Ngoc
 
RESTful http_patterns_antipatterns
RESTful http_patterns_antipatternsRESTful http_patterns_antipatterns
RESTful http_patterns_antipatternsJan Algermissen
 

What's hot (20)

Restful webservice
Restful webserviceRestful webservice
Restful webservice
 
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRISThe glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
 
Design Beautiful REST + JSON APIs
Design Beautiful REST + JSON APIsDesign Beautiful REST + JSON APIs
Design Beautiful REST + JSON APIs
 
Rest presentation
Rest  presentationRest  presentation
Rest presentation
 
RESTful Architecture
RESTful ArchitectureRESTful Architecture
RESTful Architecture
 
REST API Recommendations
REST API RecommendationsREST API Recommendations
REST API Recommendations
 
RESTful API Design Fundamentals
RESTful API Design FundamentalsRESTful API Design Fundamentals
RESTful API Design Fundamentals
 
Designing REST services with Spring MVC
Designing REST services with Spring MVCDesigning REST services with Spring MVC
Designing REST services with Spring MVC
 
Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web API
 
Rest and Rails
Rest and RailsRest and Rails
Rest and Rails
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVC
 
Restful webservices
Restful webservicesRestful webservices
Restful webservices
 
REST Presentation
REST PresentationREST Presentation
REST Presentation
 
Are you getting Sleepy. REST in SharePoint Apps
Are you getting Sleepy. REST in SharePoint AppsAre you getting Sleepy. REST in SharePoint Apps
Are you getting Sleepy. REST in SharePoint Apps
 
Rest in Rails
Rest in RailsRest in Rails
Rest in Rails
 
ASP.NET Mvc 4 web api
ASP.NET Mvc 4 web apiASP.NET Mvc 4 web api
ASP.NET Mvc 4 web api
 
What is an API?
What is an API?What is an API?
What is an API?
 
Implementation advantages of rest
Implementation advantages of restImplementation advantages of rest
Implementation advantages of rest
 
Soap and restful webservice
Soap and restful webserviceSoap and restful webservice
Soap and restful webservice
 
RESTful http_patterns_antipatterns
RESTful http_patterns_antipatternsRESTful http_patterns_antipatterns
RESTful http_patterns_antipatterns
 

Viewers also liked

Design Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID codeDesign Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID codePaulo Gandra de Sousa
 
Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Paulo Gandra de Sousa
 
An Authentication and Authorization Architecture for a Microservices World
An Authentication and Authorization Architecture for a Microservices WorldAn Authentication and Authorization Architecture for a Microservices World
An Authentication and Authorization Architecture for a Microservices WorldVMware Tanzu
 
Stateless authentication for microservices
Stateless authentication for microservicesStateless authentication for microservices
Stateless authentication for microservicesAlvaro Sanchez-Mariscal
 
Software Architecture Document Final
Software Architecture Document FinalSoftware Architecture Document Final
Software Architecture Document FinalAli Ahmed
 
Rethink Moldova 2010 2013 Final Edit 110310
Rethink Moldova 2010 2013 Final Edit 110310Rethink Moldova 2010 2013 Final Edit 110310
Rethink Moldova 2010 2013 Final Edit 110310Alexander Minza
 

Viewers also liked (20)

Design Patterns: Back to Basics
Design Patterns: Back to BasicsDesign Patterns: Back to Basics
Design Patterns: Back to Basics
 
Modern web architectural patterns
Modern web architectural patternsModern web architectural patterns
Modern web architectural patterns
 
RESTful services Design Lab
RESTful services Design LabRESTful services Design Lab
RESTful services Design Lab
 
Decoupled Communication
Decoupled CommunicationDecoupled Communication
Decoupled Communication
 
Revision control with Mercurial
Revision control with MercurialRevision control with Mercurial
Revision control with Mercurial
 
PoEAA by Example
PoEAA by ExamplePoEAA by Example
PoEAA by Example
 
Design Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID codeDesign Patterns: From STUPID to SOLID code
Design Patterns: From STUPID to SOLID code
 
Enterprise Integration Patterns
Enterprise Integration PatternsEnterprise Integration Patterns
Enterprise Integration Patterns
 
Software Product Lines
Software Product LinesSoftware Product Lines
Software Product Lines
 
Patterns for distributed systems
Patterns for distributed systemsPatterns for distributed systems
Patterns for distributed systems
 
OO design principles and patterns
OO design principles and patternsOO design principles and patterns
OO design principles and patterns
 
REST beyond CRUD
REST beyond CRUDREST beyond CRUD
REST beyond CRUD
 
Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)
 
Documenting Software Architectures
Documenting Software ArchitecturesDocumenting Software Architectures
Documenting Software Architectures
 
An Authentication and Authorization Architecture for a Microservices World
An Authentication and Authorization Architecture for a Microservices WorldAn Authentication and Authorization Architecture for a Microservices World
An Authentication and Authorization Architecture for a Microservices World
 
Stateless authentication for microservices
Stateless authentication for microservicesStateless authentication for microservices
Stateless authentication for microservices
 
Software Architecture Document Final
Software Architecture Document FinalSoftware Architecture Document Final
Software Architecture Document Final
 
Marta
MartaMarta
Marta
 
Rethink Moldova 2010 2013 Final Edit 110310
Rethink Moldova 2010 2013 Final Edit 110310Rethink Moldova 2010 2013 Final Edit 110310
Rethink Moldova 2010 2013 Final Edit 110310
 
Estrella_master
Estrella_masterEstrella_master
Estrella_master
 

Similar to Hypermedia APIs

Approaches to machine actionable links
Approaches to machine actionable linksApproaches to machine actionable links
Approaches to machine actionable linksStephen Richard
 
APIs, Web Services, and Mashups: What they are and how they can be used
APIs, Web Services, and Mashups: What they are and how they can be usedAPIs, Web Services, and Mashups: What they are and how they can be used
APIs, Web Services, and Mashups: What they are and how they can be usedsnackeru
 
There’s an API for that! Why and how to build on the IBM Connections PLATFORM
There’s an API for that! Why and how to build on the IBM Connections PLATFORMThere’s an API for that! Why and how to build on the IBM Connections PLATFORM
There’s an API for that! Why and how to build on the IBM Connections PLATFORMMikkel Flindt Heisterberg
 
One Web (API?) – Alexandre Bertails - Ippevent 10 juin 2014
One Web (API?) – Alexandre Bertails - Ippevent 10 juin 2014One Web (API?) – Alexandre Bertails - Ippevent 10 juin 2014
One Web (API?) – Alexandre Bertails - Ippevent 10 juin 2014Ippon
 
Hypermedia APIs and HATEOAS
Hypermedia APIs and HATEOASHypermedia APIs and HATEOAS
Hypermedia APIs and HATEOASVladimir Tsukur
 
An Introduction to Working With the Activity Stream
An Introduction to Working With the Activity StreamAn Introduction to Working With the Activity Stream
An Introduction to Working With the Activity StreamMikkel Flindt Heisterberg
 
Mikkel Heisterberg - An introduction to developing for the Activity Stream
Mikkel Heisterberg - An introduction to developing for the Activity StreamMikkel Heisterberg - An introduction to developing for the Activity Stream
Mikkel Heisterberg - An introduction to developing for the Activity StreamLetsConnect
 
Hypermedia for Machine APIs
Hypermedia for Machine APIsHypermedia for Machine APIs
Hypermedia for Machine APIsMichael Koster
 
David Gómez G. - Hypermedia APIs for headless platforms and Data Integration ...
David Gómez G. - Hypermedia APIs for headless platforms and Data Integration ...David Gómez G. - Hypermedia APIs for headless platforms and Data Integration ...
David Gómez G. - Hypermedia APIs for headless platforms and Data Integration ...Codemotion
 
Cdm mil-18 - hypermedia ap is for headless platforms and data integration
Cdm mil-18 - hypermedia ap is for headless platforms and data integrationCdm mil-18 - hypermedia ap is for headless platforms and data integration
Cdm mil-18 - hypermedia ap is for headless platforms and data integrationDavid Gómez García
 
Hypermedia APIs and HATEOAS / Wix Engineering
Hypermedia APIs and HATEOAS / Wix EngineeringHypermedia APIs and HATEOAS / Wix Engineering
Hypermedia APIs and HATEOAS / Wix EngineeringVladimir Tsukur
 
REST teori og praksis; REST in theory and practice
REST teori og praksis; REST in theory and practiceREST teori og praksis; REST in theory and practice
REST teori og praksis; REST in theory and practicehamnis
 
Lies you have been told about REST
Lies you have been told about RESTLies you have been told about REST
Lies you have been told about RESTdarrelmiller71
 
Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...
 Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg... Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...
Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...Brian Mann
 
A Framework for Self-descriptive RESTful Services
A Framework for Self-descriptive RESTful ServicesA Framework for Self-descriptive RESTful Services
A Framework for Self-descriptive RESTful Servicesruyalarcon
 
4163A - What is Web 2.0.ppt
4163A - What is Web 2.0.ppt4163A - What is Web 2.0.ppt
4163A - What is Web 2.0.pptMatthew Perrins
 
Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Sumy PHP User Grpoup
 

Similar to Hypermedia APIs (20)

Approaches to machine actionable links
Approaches to machine actionable linksApproaches to machine actionable links
Approaches to machine actionable links
 
Introduction to Hydra
Introduction to HydraIntroduction to Hydra
Introduction to Hydra
 
APIs, Web Services, and Mashups: What they are and how they can be used
APIs, Web Services, and Mashups: What they are and how they can be usedAPIs, Web Services, and Mashups: What they are and how they can be used
APIs, Web Services, and Mashups: What they are and how they can be used
 
There’s an API for that! Why and how to build on the IBM Connections PLATFORM
There’s an API for that! Why and how to build on the IBM Connections PLATFORMThere’s an API for that! Why and how to build on the IBM Connections PLATFORM
There’s an API for that! Why and how to build on the IBM Connections PLATFORM
 
One Web (API?) – Alexandre Bertails - Ippevent 10 juin 2014
One Web (API?) – Alexandre Bertails - Ippevent 10 juin 2014One Web (API?) – Alexandre Bertails - Ippevent 10 juin 2014
One Web (API?) – Alexandre Bertails - Ippevent 10 juin 2014
 
Hypermedia APIs and HATEOAS
Hypermedia APIs and HATEOASHypermedia APIs and HATEOAS
Hypermedia APIs and HATEOAS
 
An Introduction to Working With the Activity Stream
An Introduction to Working With the Activity StreamAn Introduction to Working With the Activity Stream
An Introduction to Working With the Activity Stream
 
Mikkel Heisterberg - An introduction to developing for the Activity Stream
Mikkel Heisterberg - An introduction to developing for the Activity StreamMikkel Heisterberg - An introduction to developing for the Activity Stream
Mikkel Heisterberg - An introduction to developing for the Activity Stream
 
Hypermedia for Machine APIs
Hypermedia for Machine APIsHypermedia for Machine APIs
Hypermedia for Machine APIs
 
David Gómez G. - Hypermedia APIs for headless platforms and Data Integration ...
David Gómez G. - Hypermedia APIs for headless platforms and Data Integration ...David Gómez G. - Hypermedia APIs for headless platforms and Data Integration ...
David Gómez G. - Hypermedia APIs for headless platforms and Data Integration ...
 
Cdm mil-18 - hypermedia ap is for headless platforms and data integration
Cdm mil-18 - hypermedia ap is for headless platforms and data integrationCdm mil-18 - hypermedia ap is for headless platforms and data integration
Cdm mil-18 - hypermedia ap is for headless platforms and data integration
 
Hypermedia APIs and HATEOAS / Wix Engineering
Hypermedia APIs and HATEOAS / Wix EngineeringHypermedia APIs and HATEOAS / Wix Engineering
Hypermedia APIs and HATEOAS / Wix Engineering
 
REST teori og praksis; REST in theory and practice
REST teori og praksis; REST in theory and practiceREST teori og praksis; REST in theory and practice
REST teori og praksis; REST in theory and practice
 
Lies you have been told about REST
Lies you have been told about RESTLies you have been told about REST
Lies you have been told about REST
 
Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...
 Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg... Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...
Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...
 
A Framework for Self-descriptive RESTful Services
A Framework for Self-descriptive RESTful ServicesA Framework for Self-descriptive RESTful Services
A Framework for Self-descriptive RESTful Services
 
4163A - What is Web 2.0.ppt
4163A - What is Web 2.0.ppt4163A - What is Web 2.0.ppt
4163A - What is Web 2.0.ppt
 
Drupal
DrupalDrupal
Drupal
 
Rest introduction
Rest introductionRest introduction
Rest introduction
 
Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2Service approach for development Rest API in Symfony2
Service approach for development Rest API in Symfony2
 

More from Paulo Gandra de Sousa

More from Paulo Gandra de Sousa (8)

Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
 
Minds-on DDD
Minds-on DDDMinds-on DDD
Minds-on DDD
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservices
 
Principles of Service Orientation
Principles of Service OrientationPrinciples of Service Orientation
Principles of Service Orientation
 
Lição prova professor coordenador
Lição prova professor coordenadorLição prova professor coordenador
Lição prova professor coordenador
 
Communication
CommunicationCommunication
Communication
 
models of distributed computing
models of distributed computingmodels of distributed computing
models of distributed computing
 
Distributed Systems
Distributed SystemsDistributed Systems
Distributed Systems
 

Hypermedia APIs

  • 2. 2 ISEP/IPP Building Hypermedia APIs with HTML5 and Node  Mike Amundsen  ISBN: 978-1-4493-0657-1 RESTful web APIs  Leonard Richardson, Mike Amundsen  ISBN: 1449358063
  • 4. “Programming for distributed hypermedia environments usually means that message transfers must carry more than just data; they must carry additional information including metadata and higher-level application flow control options.” Mike Amudsen 4 ISEP/IPP
  • 6. THE HYPERMEDIA CONSTRAINT Hipermedia as the engine of application state * * HATEOAS
  • 7. HYPERMEDIA IS THE ENGINE “hypermedia payloads carry more information than just the data stored on the server. Hypermedia payloads carry two types of vital metadata: metadata about the data itself and metadata about the possible options for modifying the state of the application at that moment.” Mike Amudsen 7 ISEP/IPP
  • 8. HYPERMEDIA API Focus on the media type and its hypermedia capabilities not on the API calls 9 ISEP/IPP
  • 9. H-FACTORS Links  represent opportunities for a client to advance the state of the application by activating a link. Controls  provide support for additional metadata when executing link operations. Not all media types support all h-factors 11 ISEP/IPP
  • 10. LINK H-FACTORS H-Factor Example LE Embed Links <img src=“…”> LO Outbound Links <a href=“…”> LT Templated Links <form method="get" action=“…"> <input type="text" name="search" value="" /> <input type="submit" /> </form> LI Idempotent Links <link rel="edit" href=“…"/> (ATOM) LN Non- Idempotent Links <form method="post" action=“…"> <textarea name="comment"></textarea> <input type="submit" /> </form> 12 ISEP/IPP
  • 11. CONTROL H-FACTORS H-Factor Example CR Read Controls Accept-Language: (http header) CU Update Controls <form method="post“ action=“…" enctype="text/plain"> <textarea name="comment"></textarea> <input type="submit" /> </form> CM Method Controls <form method="post“ action=“…"> <textarea name="comment"></textarea> <input type="submit" /> </form> CL Link Annotation Controls <link rel="stylesheet" href="..." /> 13 ISEP/IPP
  • 13. MEDIA TYPE Describes the message structure and semantic Use standard media types as much as possible  http://www.iana.org/assignments/media-types/media-types.xhtml Not all media types are hypermedia enabled, e.g., image/jpeg 15 ISEP/IPP
  • 15. EXAMPLE DATA WITH METADATA <ul class="users"> <li class="user"> <a href="..." rel="user">darrel</a> <span class="role">admin</span> <span class="status">active</span> </li> <li class="user"> <a href="..." rel="user">mike</a> <span class="role">manager</span> <span class="suspended">suspended</span> </li> ... </ul> 17 ISEP/IPP
  • 16. EXAMPLE APPLICATION METADATA <ul class="users"> <li class="navigation"> <a href="..." rel="next-page">next-page</a> </li> <li class="navigation"> <a href="..." rel="last-page">last-page</a> </li> <li class="user"> ... </li> ... </ul> <!-- defined filters --> <ul class="queries"> <li class="query"><a href="..." rel="admins">admins</a></li> <li class="query"><a href="..." rel="pending">pending</a></li> <li class="query"><a href="..." rel="suspended">suspended</a></li> </ul> <!-- user search --> <form name="user-search" action="..."> <input name="search-name" value="" /> <input type="submit" /> </form> 18 ISEP/IPP
  • 17. HYPERMEDIA TYPE DESIGN ELEMENTS Base Format (client-initiated) State Transfer Domain Style Application Flow 19 ISEP/IPP
  • 18. BASE FORMAT Plain text CSV JSON, XML, HTML, Atom/AtomPub … 20 ISEP/IPP
  • 19. (CLIENT INITIATED) STATE TRANSFER None / read-only  State is only transmitted from the server to the client Predefined  State transmitted from the client to the server follows predefined payloads defined by the server and known by the client Ad-hoc  The server tells the client what state to trasmit. E.g., HTML <FORM> 21 ISEP/IPP
  • 20. DOMAIN STYLE Specific  The payload has domain specific terms that are understood by client and server, e.g., <shipping address> General  The payload uses some general blocks that can be reused in diferent use cases, e.g., <adress type=“shipping”> Agnostic  The payload uses domain agnostic terms and (well known) decorators to add semantic, e.g., <link rel=“edit”> 22
  • 21. SPECIFIC DOMAIN STYLE EXAMPLE <!-- domain-specific design --> <order> <id>...</id> <shipping-address>...</shipping-address> <billing-address>...</billing-address> ... </order> 23 ISEP/IPP
  • 22. GENERAL DOMAIN STYLE EXAMPLE /* domain-general design */ { "order": { "id" : "...", "address" : {"type" : "shipping", "street-address" : "..."}, "address" : {"type" : "billing", "street-address" : "..."} } } 24 ISEP/IPP
  • 23. AGNOSTIC DOMAIN STYLE EXAMPLE <!-- domain-agnostic design --> <ul class="order"> <li class="id">...</li> <ul class="shipping-address"> <li class="street-address">...</li> ... </ul> <ul class="billing-address"> <li class="street-address">...</li> ... </ul> ... </ul> 25 ISEP/IPP
  • 24. APPLICATION FLOW None  The hypermedia type contains no identifiers for application flow. Intrinsic  The application flow identifiers are defined within the hypermedia type itself. E.g., Atom and AtomPub define a small set of link relations to indicate application flow (“edit”, “edit-media”, and “self”). Applied  The application flow identifiers are not part of the hypermedia type, but the type designs contain allowances (usually element decorators or attributes) for applying external values to indicate application flow. E.g., HTML attributes: profile, id, name, rel, and class. 26
  • 25. NO APPLICATION FLOW EXAMPLE Media type: text/uri-list # urn:isbn:0-201-08372-8 http://www.huh.org/books/foo.html http://www.huh.org/books/foo.pdf ftp://ftp.foo.org/books/foo.txt 27 ISEP/IPP
  • 26. INTRINSIC APPLICATION FLOW EXAMPLE <!-- AtomPub --> <?xml version="1.0"?> <entry xmlns="http://www.w3.org/2005/Atom"> <title>Atom-Powered Robots Run Amok</title> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>2003-12-13T18:30:02Z</updated> <author><name>John Doe</name></author> <content>Some text.</content> <link rel="edit" href="http://example.org/edit/first- post.atom"/> </entry> 28 ISEP/IPP
  • 27. APPLIED APPLICATION FLOW EXAMPLE <html> <head> <title>Payment Options</title> <meta name="profile" content="http://www.example.com/profiles/payment.html" /> </head> <body> <h1>Payment Options</h1> <p> <a href="..." rel="cancel-order">Cancel Order</a> </p> <form href="..." name="credit-card" method="post"> <input name="card-number" value="" /> ... <input type="submit" /> </form> <form href="..." name="purchase-order" method="post"> <input name="po-number" value="" /> ... <input type="submit" /> </form> <form href="..." name="bank-draft" method="post"> <input name="routing-number" value="" /> ... <input type="submit" /> </form> ... </html> 29 ISEP/IPP
  • 28. AN EXAMPLE Message server 30
  • 29. SIMPLE MESSAGE SERVICE A service that allows users to post messages. Post a new message Update a message Search messages by text, user, and date Get all the messages of one user Register a new user Update the user data Upload the user photo 31 ISEP/IPP Message User 1 0..n
  • 30. MESSAGES RESOURCES Resource The message collection URL /message GET POST PUT DELET E return all messages create new entry, returns 201 not allowed not allowed Resource A message (Item in the collection) URL /message/:id GET POST PUT DELET return specific message or 404 update existing entry or 404 overwrite existing or create new given the id deletes the message 32 ISEP/IPP
  • 31. USERS RESOURCES Resource The user collection URL /user GET POST PUT DELET E return all users create new entry, returns 201 not allowed not allowed Resource An user URL /user/:id GET POST PUT DELET return specific user or 404 updates existing entry or 404 overwrite existing or create new given the id deletes the user 33 ISEP/IPP
  • 32. NO HYPERMEDIA REPRESENTATIONS Message { id: "a1", text: "Sample one", sender: "Mary", createdOn: now, updatedOn: now }; User { id: "Mary", name:"Mary", email: "mary@contoso.com", roles:[], createdOn: now }; 34 ISEP/IPP Missing: • Explicit id as URI • Explicit links to other resources • Application flow
  • 33. HYPERMEDIA MISSING FOR MESSAGES Message collection Paging links  First page  Next page  Previous page Message Item Resource links  User  Message collection Actions  Edit the message  Delete the message 35 ISEP/IPP
  • 34. HYPERMEDIA MISSING FOR USERS User collection Paging links  First page  Next page  Previous page User Item Resource links  User’s messages  User’s photo  User collection Actions  Edit the user  Cancel user account 36 ISEP/IPP
  • 35. SOME DESIGN DECISIONS Base format  JSON State transfer  Predefined Domain style  Specific Application flow  Intrinsic 37 ISEP/IPP
  • 36. LINK RELATIONS Don’t reinvent the weel IANA  http://www.iana.org/assignments/link-relations/link-relations.xhtml Microformats  http://microformats.org/wiki/existing-rel-values 38 ISEP/IPP
  • 37. COMMON LINK RELATIONS rel Description Definitio n collecti on The target IRI points to a resource which represents the collection resource for the context IRI. [RFC657 3] edit Refers to a resource that can be used to edit the link's context. [RFC502 3] edit- form The target IRI points to a resource where a submission form for editing associated resource can be obtained. [RFC686 1] next Indicates that the link's context is a part of a series, and that the next in the series is the link target. [HTML5] payme Indicates a resource where payment is [RFC598 39 ISEP/IPP Source: http://www.iana.org/assignments/link-relations/link-relations.xht
  • 38. Message collection { MESSAGE_KEY : { MESSAGE_REPRESENTATION }, … links: [ { rel: "self", href: URI }, { rel: "start", href: URI }, { rel: "prev", href: URI }, { rel: "next", href: URI } ] } Message Item { text: "Sample one", createdOn: now, updatedOn: now, links: [ { rel: "self", href: SERVER_ROOT+"/message/a1", }, { rel: "edit", href: SERVER_ROOT+"/message/a1", }, { rel: "collection", href: SERVER_ROOT+"/message" }, { rel: "psidi:user", href: SERVER_ROOT+"/user/Mary", title: "Mary Smith" } ], };
  • 39. User collection { USER_KEY : { USER_REPRESENTATION }, … links: [ { rel: "self", href: URI }, { rel: "start", href: URI }, { rel: "prev", href: URI }, { rel: "next", href: URI } ] } UserItem { name:"Mary Smith", email:"mary@contoso.com", roles:[], createdOn: now, links: [ { rel: "self", href: SERVER_ADDR+"/user/Mary", }, { rel: "edit", href: SERVER_ADDR+"/user/Mary", }, { rel: "collection", href: SERVER_ADDR+"/user" }, { rel: "psidi:user-messages", href: SERVER_ADDR+"/message?user=Mary", }, { rel: "psidi:user-photo", href: SERVER_ADDR+"/user/Mary/photo", }, ], };
  • 40. { "a11": { "id": "a11", "text": "Sample one", "senderId": "Mary", "createdOn": "2014-12-17T18:35:00.149Z", "updatedOn": "2014-12-17T18:35:00.149Z", "links": [ { "rel": "self", "href": "http://localhost:3001/message/a11" }, { "rel": "edit", "href": "http://localhost:3001/message/a11" }, { "rel": "collection", 42 ISEP/IPP GET http://localhost:3001/messag e
  • 44. REPRESENTATION VS RESOURCE Resource (e.g., table record) { id: PRIMARY_KEY text: "Sample one", senderId: FOREIGN_KEY TO “users” createdOn: now, updatedOn: now, }; Resource Representation { text: "Sample one", createdOn: now, updatedOn: now, links: [ { rel: "self", href: SERVER_ROOT+"/message/a1", // URI }, { rel: "edit", href: SERVER_ROOT+"/message/a1", // URI }, { rel: "collection", href: SERVER_ROOT+"/message“ // URI }, { rel: "psidi:user", href: SERVER_ROOT+"/user/Mary", // URI title: "Mary Smith“ } ], }; 46
  • 45. CONTINUE THE ROAD TO HYPERMEDIA Several things are missing: Reuse generic collection media type, e.g., Collection+JSON, OData Reuse existing media types and microformats, e.g., hCard “New item” template Search templates, e.g., OpenSearch Photo subresource: information + media 47 ISEP/IPP
  • 47. THE HYPERMEDIA CONSTRAINT “The application is therefore an engine that moves from one state to the next by examining and choosing from among the alternative state transitions in the current set of representations.” Roy Fielding 49 ISEP/IPP
  • 48. SUGGESTED READINGS Fielding, R. “REST APIs must be hypertext-driven”. http://roy.gbiv.com/untangled/2008/rest-apis-must- be-hypertext-driven Atom/AtomPub. http://bitworking.org/projects/atom/rfc5023.html Collection+JSON media type. http://amundsen.com/media-types/collection/ OData. http://www.odata.org/ OpenSearch. http://www.opensearch.org/Home HTTP PATCH. http://tools.ietf.org/html/rfc5789 50 ISEP/IPP

Editor's Notes

  1. An API that is based on the notion of hypermedia Message payload is of an hypermedia-enabled media type
  2. From wikipedia: http://en.wikipedia.org/wiki/HATEOAS This principle is the key differentiator between a REST and most other forms of client server system. Rather than the actions and interfaces a client may use being defined elsewhere, such as in a WSDL file or predefined in the client code, the principle is that the hypermedia in eachserver response will contain links that correspond to all the actions that the client can currently perform. Therefore, dependent on the current application state, every server response describes the new actions that are available. The server can change the range of allowable responses in a dynamic way, and a client should adapt its behavior to these changes. A client of a RESTful application need only know a single fixed URL to access it. All future actions should be discoverable dynamically from hypermedia links included in the representations of the resources that are returned from that URL. Standardized media types are also expected to be understood by any client that might use a RESTful API. Application state transitions are driven by a combination of the known processing rules for each media type, client selection from the server-provided choices in representations received, and the user's manipulation of those representations. Thus interactions are driven by hypermedia, rather than by any out-of-band information.[1] If necessary, the client’s knowledge of media types, resource communication mechanisms, and user interaction, may be improved on-the-fly by the transmission of code-on-demand from the server to the client as defined elsewhere in the REST architecture.[2]
  3. LE: embed the contente of the link target with the current content LO: traverse the link LT: traverse the link but construct the link with the template LI: submit content using idempotent request (e.g., PUT, DELETE) LN: submit content using non-idepontent request (e.g., POST)
  4. The possible metadata elements (and their values) can vary between supported protocols (FTP, HTTP, etc.) as does the details for communicating this link metadata. For example, in HTTP, this is accomplished through HTTP Headers. Regardless of the mechanism, control factors fall into four categories: Read, Update, Method, and Link Annotation.
  5. Derived from the MIME type