A REST Layer on Top of the World - IPC13 Munich

60
Nicolas Pastorino | eZ Systems VP Community & Online Marketing @jeanvoye | [email protected] Contents: Courtesy of Damien Pobel A REST Layer on Top of The World #REST #ezpublish #symfony #IPC13 #CMS #SOA #UX

description

Talk given at International PHP Conference 13 in Munich. Abstract: This talk will present how inserting a REST layer on top of the world of Content and Features in eZ Publish allows for more than service-to-service usage. It will dig into the details of this content-oriented, RESTful API, hands-on, and illustrate how the REST principles were applied to eZ Publish 5 and its Symfony-based architecture. Details: http://phpconference.com/2013/en/sessions/rest-layer-top-world

Transcript of A REST Layer on Top of the World - IPC13 Munich

Page 2: A REST Layer on Top of the World - IPC13 Munich

about:eZ

Page 3: A REST Layer on Top of the World - IPC13 Munich

about:eZ

Enterprise Open-source since 1999Creators of eZ Publish Platform

GlobalBusiness: http://ez.no | Community: http://share.ez.no

Page 4: A REST Layer on Top of the World - IPC13 Munich

@eZSystems

http://google.com/+ezsystems

http://linkedin.com/company/16738

https://github.com/ezsystems

about:eZ

Enterprise Open-source since 1999Creators of eZ Publish Platform

GlobalBusiness: http://ez.no | Community: http://share.ez.no

Page 5: A REST Layer on Top of the World - IPC13 Munich

about:me

Page 6: A REST Layer on Top of the World - IPC13 Munich

about:me

eZ Publish Community since 2005Lead Community + Marketing at eZ

Content Management, Open-source, Mountaineering

Page 7: A REST Layer on Top of the World - IPC13 Munich

@jeanvoye

http://bit.ly/nicolas-on-gplus

http://fr.linkedin.com/in/nicolaspastorino/

https://github.com/nfrp

about:me

eZ Publish Community since 2005Lead Community + Marketing at eZ

Content Management, Open-source, Mountaineering

Page 8: A REST Layer on Top of the World - IPC13 Munich

TO

DA

Y

Page 9: A REST Layer on Top of the World - IPC13 Munich

REST

eZ Publish

REST in eZ Publish

DEMO

REST for UXTO

DA

Y

Page 10: A REST Layer on Top of the World - IPC13 Munich

REST

5

Page 12: A REST Layer on Top of the World - IPC13 Munich

REST?Set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used. Give every “thing” an ID

Link things together (“HATEOAS”) Use standard methods Resources with multiple representations Communicate statelessly

h"p://en.wikipedia.org/wiki/Representa4onal_State_Transferh"p://www.infoq.com/ar4cles/rest-­‐introduc4on

Page 14: A REST Layer on Top of the World - IPC13 Munich

REST? (ctd)Key goals of REST include: Scalability of component interactions Generality of interfaces Independent deployment of components Intermediary components to reduce latency, enforce security and encapsulate legacy systems

h"p://en.wikipedia.org/wiki/Representa4onal_State_Transferh"p://www.infoq.com/ar4cles/rest-­‐introduc4on

Page 15: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Request - Anatomy

Page 16: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Request - Anatomy

• HTTP verb / method : GET,POST,PUT,DELETE, TRACE,OPTIONS,HEAD, ...

Page 17: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Request - Anatomy

• HTTP verb / method : GET,POST,PUT,DELETE, TRACE,OPTIONS,HEAD, ...

• ... or a custom verb: PATCH,COPY, ...

Page 18: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Request - Anatomy

• HTTP verb / method : GET,POST,PUT,DELETE, TRACE,OPTIONS,HEAD, ...

• ... or a custom verb: PATCH,COPY, ...

• Set of headers

Page 19: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Request - Anatomy

• HTTP verb / method : GET,POST,PUT,DELETE, TRACE,OPTIONS,HEAD, ...

• ... or a custom verb: PATCH,COPY, ...

• Set of headers

• A body in POST,PUT or custom verb requests

Page 20: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Response - Anatomy

Page 22: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Response - Anatomy• Status code (by @stevelosh)

• 1XX: hold on

Page 23: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Response - Anatomy• Status code (by @stevelosh)

• 1XX: hold on

• 2XX: here you go

Page 24: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Response - Anatomy• Status code (by @stevelosh)

• 1XX: hold on

• 2XX: here you go

• 3XX: go away

Page 25: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Response - Anatomy• Status code (by @stevelosh)

• 1XX: hold on

• 2XX: here you go

• 3XX: go away

• 4XX: you f * * * * d up

Page 26: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Response - Anatomy• Status code (by @stevelosh)

• 1XX: hold on

• 2XX: here you go

• 3XX: go away

• 4XX: you f * * * * d up

• 5XX: I f * * * * d up

Page 27: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Response - Anatomy• Status code (by @stevelosh)

• 1XX: hold on

• 2XX: here you go

• 3XX: go away

• 4XX: you f * * * * d up

• 5XX: I f * * * * d up

• Set of headers

Page 28: A REST Layer on Top of the World - IPC13 Munich

HTTP 1.1 Response - Anatomy• Status code (by @stevelosh)

• 1XX: hold on

• 2XX: here you go

• 3XX: go away

• 4XX: you f * * * * d up

• 5XX: I f * * * * d up

• Set of headers

• A body depending on the response

Page 29: A REST Layer on Top of the World - IPC13 Munich

eZ Publish

10

Page 30: A REST Layer on Top of the World - IPC13 Munich

Standard Symfony2 App.

(=app)

Page 31: A REST Layer on Top of the World - IPC13 Munich

Standard Symfony2 App.

(=app) +eZ Publish

bundles(PHP & REST APIs

+ Legacy)

vendor/ ezsystems/ ezpublish-kernel

Page 32: A REST Layer on Top of the World - IPC13 Munich

Standard Symfony2 App.

(=app) +eZ Publish

bundles(PHP & REST APIs

+ Legacy)

vendor/ ezsystems/ ezpublish-kernel

+Full-blownBack-office

Page 33: A REST Layer on Top of the World - IPC13 Munich

Standard Symfony2 App.

(=app) +eZ Publish

bundles(PHP & REST APIs

+ Legacy)

vendor/ ezsystems/ ezpublish-kernel

+Full-blownBack-office

THIS is eZ Publish 5

Page 34: A REST Layer on Top of the World - IPC13 Munich

REST in eZ Publish

12

Page 35: A REST Layer on Top of the World - IPC13 Munich

REST API in eZ Publish 4.x

Page 36: A REST Layer on Top of the World - IPC13 Munich

REST API in eZ Publish 4.x

• eZ Publish 4.x has a REST API

• Prefix /ezp/api/v1/

• Read only

• Very limited scope / incomplete

Page 37: A REST Layer on Top of the World - IPC13 Munich

REST API in eZ Publish 5.x

Page 38: A REST Layer on Top of the World - IPC13 Munich

REST API in eZ Publish 5.x

Page 39: A REST Layer on Top of the World - IPC13 Munich

REST API in eZ Publish 5.x• Read / Write

• Prefix /ezp/api/v2/

• Allows to handle almost everything in the repository (Content, Location, Types, ...)

• Support Basic Auth and Session Auth

• XML or JSON support in input and output

• Hypermedia as the Engine of Application State (HATEOAS)• Specifications: http://j.mp/ezpublish-rest-spec

Page 40: A REST Layer on Top of the World - IPC13 Munich

A REST Layer on top of a world of Content

Page 41: A REST Layer on Top of the World - IPC13 Munich

A REST Layer on top of a world of Content

Page 42: A REST Layer on Top of the World - IPC13 Munich

A REST Layer on top of a world of Content \o/

Page 43: A REST Layer on Top of the World - IPC13 Munich

DEMO

16

Page 44: A REST Layer on Top of the World - IPC13 Munich

Demo Synopsis• Navigate to http://cp2013.9.loc/ then to http://cp2013.9.loc/Blog

• Add Blog post from http://cp2013.9.loc/ezdemo_site_admin/Blog (Content: http://phpconference.com/2013/en/sessions/rest-layer-top-world )

• REST: Request REST Root:

• chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html#RequestPlace:projectEndpoint/1

• Explain all parameters, then run.

• REST: Show children of Content root

• chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html#RequestPlace:projectEndpoint/2

• REST: Show Location/Content/ContentInfo for one child (“Blog”)

• chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html#RequestPlace:projectEndpoint/3

• chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html#RequestPlace:projectEndpoint/4 Edit Content, then reload.

• chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html#RequestPlace:projectEndpoint/5

• REST: Show children of one child (“Blog”)

• Show children: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html#RequestPlace:projectEndpoint/8

• Delete the previously created blog post: curl -u "admin:publish" -X DELETE http://cp2013.9.loc/api/ezp/v2/content/objects/<objectId>

• Refresh children list: Show children: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html#RequestPlace:projectEndpoint/8

• REST: Create an Image object

• Show ContentType (ContentClass) in back-office: http://cp2013.9.loc/ezdemo_site_admin/class/view/27

• Create image from command line

• $> cdipc13

• $> curl -u "admin:publish" -i -H "Accept: application/json" -H "Content-Type: application/vnd.ez.api.ContentCreate+json" -X POST -d @createimage.json http://cp2013.9.loc/api/ezp/v2/content/objects

• Publish object: curl -u "admin:publish" -X PUBLISH http://cp2013.9.loc/api/ezp/v2/content/objects/<object Id>/versions/1

• Show image in the back-office: http://cp2013.9.loc/ezdemo_site_admin/eZ-Systems-logo-created-with-the-REST-API

Page 45: A REST Layer on Top of the World - IPC13 Munich

REST for UX

18

Page 46: A REST Layer on Top of the World - IPC13 Munich

REST API for more than service-to-service: UX example

Page 47: A REST Layer on Top of the World - IPC13 Munich

REST API for more than service-to-service: UX example

• API REST as provider for ajax calls - data

• Initial page load (JS code, generating an interface) - then Ajax+REST for navigation, data retrieval,

• Total separation of functional UX and data retrieval + back-end business logic.

Page 48: A REST Layer on Top of the World - IPC13 Munich

REST API for more than service-to-service: UX example

Page 49: A REST Layer on Top of the World - IPC13 Munich

REST API for more than service-to-service: UX example• Tools

• JS lib, REST client: https://github.com/ezsystems/ez-js-rest-client

• Reproduction of the public PHP API: exposing services

• Relying on HATEOS, but simplifying usage of it

Page 50: A REST Layer on Top of the World - IPC13 Munich

REST API for more than service-to-service: UX example

Page 51: A REST Layer on Top of the World - IPC13 Munich

REST API for more than service-to-service: UX example• Why?

• Easy to discover the REST API structure from a client

• BC support: no URL hard-coding, painless client-side, all existing projects relying on the API need not be modified,

• Simplifies cache management (HTTP): easy to know exactly which resource is to be expired after a change in content repository.

Page 52: A REST Layer on Top of the World - IPC13 Munich
Page 53: A REST Layer on Top of the World - IPC13 Munich
Page 54: A REST Layer on Top of the World - IPC13 Munich
Page 55: A REST Layer on Top of the World - IPC13 Munich
Page 56: A REST Layer on Top of the World - IPC13 Munich

Demo Synopsis

• Navigate to http://cp2013.9.loc/summercamp/rest-test

• Open Console > Network > Filter with “XHR” only

• Reload page, and clear console

• Click on “Details” for “Blog”

• Inspect request in console:

• show”Accept” header value,

• show URL

• In “Preview” subtab: show response, focus on “Content.Section”

• --> Show next request (back to “Headers” subtab): Section

• show”Accept” header value,

• show URL

• In “Preview” subtab: show response, focus on “Section.name”

• --> Show next request (back to “Headers” subtab): Owner

Page 57: A REST Layer on Top of the World - IPC13 Munich

Wrap-up

24

Page 58: A REST Layer on Top of the World - IPC13 Munich

TO

DA

Y

Page 59: A REST Layer on Top of the World - IPC13 Munich

REST

eZ Publish

REST in eZ Publish

DEMO

REST for UXTO

DA

Y

Page 60: A REST Layer on Top of the World - IPC13 Munich

eZ Publish Community http://share.ez.no

eZ Publish 5 Community Projecthttp://share.ez.no/download

https://github.com/ezsystems/ezpublish-community

eZ Systems & eZ Publish Platform Enterprise Editionhttp://ez.no

Slides available soon athttp://slideshare.net/jeanvoye

@jeanvoye

http://bit.ly/nicolas-on-gplus

http://fr.linkedin.com/in/nicolaspastorino/

https://github.com/nfrp