Slide 1

Slide 1 text

Services REST & OpenData Benjamin Grandfond - PHP Tour Nantes 2012 jeudi 29 novembre 12

Slide 2

Slide 2 text

Benjamin Grandfond Benjamin Grandfond - PHP Tour Nantes 2012 Technical Team Manager @benjaM1 @benja-M-1 jeudi 29 novembre 12

Slide 3

Slide 3 text

Benjamin Grandfond - PHP Tour Nantes 2012 APIs jeudi 29 novembre 12

Slide 4

Slide 4 text

Benjamin Grandfond - PHP Tour Nantes 2012 Architectures • SOAP • XML-RPC • URL unique • POX (Plain Old XML) jeudi 29 novembre 12

Slide 5

Slide 5 text

Benjamin Grandfond - PHP Tour Nantes 2012 Architectures https://api.bf.com/createOrder?burger=doublecheese&fries=true&drink=coca https://api.bf.com/payOrder?id=1234&cardHolderName=Benjamin%20 Grandfond&cardNumber=11223344556677&cardSecuredNumber=123&expiryMonth=11& expiryDay=21 • multiples URL • un seul verbe HTTP (POST) jeudi 29 novembre 12

Slide 6

Slide 6 text

Benjamin Grandfond - PHP Tour Nantes 2012 Richardson Maturity Model Niveaux 0 et 1 http://martinfowler.com/articles/richardsonMaturityModel.html jeudi 29 novembre 12

Slide 7

Slide 7 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 ~Xavur deviantart jeudi 29 novembre 12

Slide 8

Slide 8 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Roy T. Fielding http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm jeudi 29 novembre 12

Slide 9

Slide 9 text

Benjamin Grandfond - PHP Tour Nantes 2012 REpresentational State Transfer jeudi 29 novembre 12

Slide 10

Slide 10 text

Benjamin Grandfond - PHP Tour Nantes 2012 Définitions • ressource : tout document exposé sur le web • représentation : vue d’une ressource à un instant t dans n’importe quel format (JSON, XML, HTML ...) jeudi 29 novembre 12

Slide 11

Slide 11 text

Benjamin Grandfond - PHP Tour Nantes 2012 Rappel • le web est composé de liens entre différentes ressources • le web utilise le protocol HTTP jeudi 29 novembre 12

Slide 12

Slide 12 text

Benjamin Grandfond - PHP Tour Nantes 2012 Contraintes • Client - Serveur • Stateless • Cacheable • Code On Demand (optionelle) • Interface uniforme jeudi 29 novembre 12

Slide 13

Slide 13 text

Benjamin Grandfond - PHP Tour Nantes 2012 Interface uniforme ?! • une URI identifie une ressource • manipulation par représentation • les headers HTTP définissent les opérations, les autorisations, le format de la réponse, etc ... • représentation avec hypermedias jeudi 29 novembre 12

Slide 14

Slide 14 text

Benjamin Grandfond - PHP Tour Nantes 2012 littlemalt flickr Burger Factory API jeudi 29 novembre 12

Slide 15

Slide 15 text

Benjamin Grandfond - PHP Tour Nantes 2012 Client Serveur doublecheese, frite coca commande 1234 coûte 12€ voilà 12€ voilà la facture voilà votre commande quelques minutes plus tard merci ! 1 2 3 4 5 6 jeudi 29 novembre 12

Slide 16

Slide 16 text

Création d’une commande Benjamin Grandfond - PHP Tour Nantes 2012 jeudi 29 novembre 12

Slide 17

Slide 17 text

POST /orders HTTP/1.1 Host: api.bf.com Accept: application/xml Content-Type: application/xml doublecheese fries coca Benjamin Grandfond - PHP Tour Nantes 2012 Request 1 jeudi 29 novembre 12

Slide 18

Slide 18 text

HTTP/1.1 201 Created Allow: DELETE, GET, PUT Content-Type: application/xml Location: https://api.bf.com/orders/1234 1234 doublecheese fries coca 12.00 pending payment Benjamin Grandfond - PHP Tour Nantes 2012 Response 2 jeudi 29 novembre 12

Slide 19

Slide 19 text

Paiement de la commande Benjamin Grandfond - PHP Tour Nantes 2012 jeudi 29 novembre 12

Slide 20

Slide 20 text

POST /payments HTTP/1.1 Host: api.bf.com Content-Type: application/xml 1234 12.00 Benjamin Grandfond 11223344556677 123 11 2013 Benjamin Grandfond - PHP Tour Nantes 2012 Request 3 jeudi 29 novembre 12

Slide 21

Slide 21 text

HTTP/1.1 201 Created Allow: GET Content-Type: application/xml Location: https://api.bf.com/receipts/1234 1234 1234 12.00 2012-11-04 17:53:42 Benjamin Grandfond - PHP Tour Nantes 2012 Response 4 jeudi 29 novembre 12

Slide 22

Slide 22 text

Livraison de la commande Benjamin Grandfond - PHP Tour Nantes 2012 jeudi 29 novembre 12

Slide 23

Slide 23 text

PUT /orders/1234 HTTP/1.1 Host: api.bf.com Content-Type: application/xml 1234 doublecheese fries coca 12.00 delivered Benjamin Grandfond - PHP Tour Nantes 2012 Request 5 jeudi 29 novembre 12

Slide 24

Slide 24 text

PATCH /orders/1234 HTTP/1.1 Host: api.bf.com Content-Type: application/xml delivered Benjamin Grandfond - PHP Tour Nantes 2012 Request 5 jeudi 29 novembre 12

Slide 25

Slide 25 text

HTTP/1.1 200 Ok Host: api.bf.com Content-Type: application/xml 1234 doublecheese fries coca 12.00 delivered Benjamin Grandfond - PHP Tour Nantes 2012 Response 6 jeudi 29 novembre 12

Slide 26

Slide 26 text

Benjamin Grandfond - PHP Tour Nantes 2012 Méthodes HTTP GET (sans risque, idempotent) POST (risqué, pas idempotent) PUT & DELETE (risqué, idempotent) jeudi 29 novembre 12

Slide 27

Slide 27 text

Benjamin Grandfond - PHP Tour Nantes 2012 Codes retours HTTP 200 OK (GET, PUT, DELETE) 201 Created (POST) 204 No Content (PUT, DELETE) 400 Bad Request (POST) 404 Not Found (GET, PUT, DELETE) 405 Method Not Allowed (PUT, DELETE) 409 Conflict (PUT) 500 Internal Server Error (GET, POST, PUT) 503 Service Unavailable (DELETE) jeudi 29 novembre 12

Slide 28

Slide 28 text

Niveau 2 du RMM Benjamin Grandfond - PHP Tour Nantes 2012 c’est déjà pas mal ;) jeudi 29 novembre 12

Slide 29

Slide 29 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 HATEOAS «Hypermedia as the engine of the application state» Roy T. Fielding jeudi 29 novembre 12

Slide 30

Slide 30 text

Benjamin Grandfond - PHP Tour Nantes 2012 Interface uniforme ?! • une URI identifie une ressource • manipulation par représentation • les headers HTTP définissent les opérations, les autorisations, le format de la réponse, etc ... • représentation avec hypermedias jeudi 29 novembre 12

Slide 31

Slide 31 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Ce sont des liens* qui permettent au client de changer l’état de l’application à un instant t. Hypermedia ? * les formulaires sont aussi des hypermedias ! jeudi 29 novembre 12

Slide 32

Slide 32 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Format XHTML est un format supportant les hypermedias interprété par les navigateurs ATOM est un format supportant les hypermedias interprété par les feed readers jeudi 29 novembre 12

Slide 33

Slide 33 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Format sur mesure HTTP/1.1 200 OK Allow: DELETE, GET Content-Type: application/xml 1234 doublecheese fries coca 12.00 delivered jeudi 29 novembre 12

Slide 34

Slide 34 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Format sur mesure HTTP/1.1 200 OK Allow: DELETE, GET Content-Type: application/xml 1234 doublecheese fries coca 12.00 delivered Définition des namespaces jeudi 29 novembre 12

Slide 35

Slide 35 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Format sur mesure HTTP/1.1 200 OK Allow: DELETE, GET Content-Type: application/xml 1234 doublecheese fries coca 12.00 delivered Lien vers la commande elle-même jeudi 29 novembre 12

Slide 36

Slide 36 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Format sur mesure HTTP/1.1 200 OK Allow: DELETE, GET Content-Type: application/xml 1234 fries coca 12.00 delivered Lien vers le burger commandé jeudi 29 novembre 12

Slide 37

Slide 37 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Format sur mesure HTTP/1.1 200 OK Allow: DELETE, GET Content-Type: application/xml 1234 coca 12.00 delivered Lien vers l’accompagnement jeudi 29 novembre 12

Slide 38

Slide 38 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Format sur mesure HTTP/1.1 200 OK Allow: DELETE, GET Content-Type: application/xml 1234 12.00 delivered Lien vers la boisson jeudi 29 novembre 12

Slide 39

Slide 39 text

HTTP/1.1 200 OK Allow: DELETE, GET Content-Type: application/vnd.bf.com+xml 1234 12.00 delivered Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Media Type Définition d’un contrat entre client et serveur jeudi 29 novembre 12

Slide 40

Slide 40 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Niveau 3 du RMM Hypermedia jeudi 29 novembre 12

Slide 41

Slide 41 text

Benjamin Grandfond - PHP Tour Nantes 2012 jeudi 29 novembre 12

Slide 42

Slide 42 text

Benjamin Grandfond - PHP Tour Nantes 2012 ✔ GET, POST, PUT, DELETE ✔ PATCH, HEAD, OPTIONS Interface uniforme ? jeudi 29 novembre 12

Slide 43

Slide 43 text

Benjamin Grandfond - PHP Tour Nantes 2012 ✔ Codes HTTP Interface uniforme ? # Symfony/Component/HttpFoundation/Response.php public static $statusTexts = array( 200 => 'OK', 201 => 'Created', ... 204 => 'No Content', ... 304 => 'Not Modified', ... 400 => 'Bad Request', 401 => 'Unauthorized', ... 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', ... 409 => 'Conflict', ... 415 => 'Unsupported Media Type', ... 500 => 'Internal Server Error', ... 503 => 'Service Unavailable' ... jeudi 29 novembre 12

Slide 44

Slide 44 text

Benjamin Grandfond - PHP Tour Nantes 2012 ✔ FOSRestBundle URL hiérarchisées ? https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/ 5-automatic-route-generation_single-restful-controller.md#define-resource-actions jeudi 29 novembre 12

Slide 45

Slide 45 text

Benjamin Grandfond - PHP Tour Nantes 2012 URL hiérarchisées ? "get_users" [GET] /users "post_users" [POST] /users "get_user" [GET] /users/{slug} "put_user" [PUT] /users/{slug} "patch_user" [PATCH] /users/{slug} "delete_user" [DELETE] /users/{slug}

Slide 46

Slide 46 text

Benjamin Grandfond - PHP Tour Nantes 2012 URL hiérarchisées ? public function getUserCommentsAction($slug); public function postUserCommentsAction($slug); public function getUserCommentAction($slug, $id); public function putUserCommentAction($slug, $id); public function deleteUserCommentAction($slug, $id); "get_user_comments" [GET] /users/{slug}/comments "post_user_comments" [POST] /users/{slug}/comments "get_user_comment" [GET] /users/{slug}/comments/{id} "put_user_comment" [PUT] /users/{slug}/comments/{id} "delete_user_comment" [DELETE] /users/{slug}/comments/{id} jeudi 29 novembre 12

Slide 47

Slide 47 text

Benjamin Grandfond - PHP Tour Nantes 2012 ✔ FOSRestBundle + Twig HATEOAS ? jeudi 29 novembre 12

Slide 48

Slide 48 text

Benjamin Grandfond - PHP Tour Nantes 2012 HATEOAS ? {# order.xml.twig #} {{ order.id }} {{order.amount}} {{ order.state }} jeudi 29 novembre 12

Slide 49

Slide 49 text

Benjamin Grandfond - PHP Tour Nantes 2012 ✔ FOSRestBundle + JMSSerializerBundle HATEOAS ? jeudi 29 novembre 12

Slide 50

Slide 50 text

Benjamin Grandfond - PHP Tour Nantes 2012 HATEOAS ?

Slide 51

Slide 51 text

Benjamin Grandfond - PHP Tour Nantes 2012 ✔ FSCHateoasBundle HATEOAS ? https://github.com/TheFootballSocialClub/FSCHateoasBundle jeudi 29 novembre 12

Slide 52

Slide 52 text

Benjamin Grandfond - PHP Tour Nantes 2012 ✔ Symfony Mime Type ? jeudi 29 novembre 12

Slide 53

Slide 53 text

Benjamin Grandfond - PHP Tour Nantes 2012 Mime Type ? // src/BurgerFactory/ApiBundle/EventListener/RequestListener.php namespace BurgerFactory\ApiBundle; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; class RequestListener { public function onKernelRequest(GetResponseEvent $event) { $event->getRequest()->setFormat('xml', 'application/vnd.bf.com'); } } jeudi 29 novembre 12

Slide 54

Slide 54 text

Benjamin Grandfond - PHP Tour Nantes 2012 ✔ FOSRestBundle Mime Type ? # app/config/config.yml fos_rest: view: mime_types: { 'xml': ['application/vnd.bf.com+xml'] } jeudi 29 novembre 12

Slide 55

Slide 55 text

Benjamin Grandfond - PHP Tour Nantes 2012 Vous n’aimez pas Symfony ? Silex Slim Zend Framework ... le vôtre jeudi 29 novembre 12

Slide 56

Slide 56 text

Benjamin Grandfond - PHP Tour Nantes 2012 Pourquoi ferais-je une API RESTful ? jeudi 29 novembre 12

Slide 57

Slide 57 text

Benjamin Grandfond - PHP Tour Nantes 2012 Questions à se poser • Mon business en a-t-il réellement besoin ? • Combien de temps ? • SOAP n’est pas plus simple/efficace ? ... ça dépend jeudi 29 novembre 12

Slide 58

Slide 58 text

Benjamin Grandfond - PHP Tour Nantes 2012 Les avantages évolutivité auto-documenté scalabilité HTTP (cache, authentification, content negociation ...) jeudi 29 novembre 12

Slide 59

Slide 59 text

Benjamin Grandfond - PHP Tour Nantes 2012 Vous n’êtes pas obligé de faire une API RESTful ! Un CRUD «HTTP» c’est bien aussi :) * * mais ne dîtes pas que c’est une API Rest jeudi 29 novembre 12

Slide 60

Slide 60 text

Benjamin Grandfond - PHP Tour Nantes 2012 OpenData jeudi 29 novembre 12

Slide 61

Slide 61 text

Benjamin Grandfond - PHP Tour Nantes 2012 En informatique, l’open data est une information structurée publique ou privée et généralement non utilisable par un humain mais interprétable par une machine. wikipedia Open Data jeudi 29 novembre 12

Slide 62

Slide 62 text

Benjamin Grandfond - PHP Tour Nantes 2012 En informatique, l’open data est une information structurée publique ou privée et généralement non utilisable par un humain mais interprétable par une machine. wikipedia = API ! Open Data jeudi 29 novembre 12

Slide 63

Slide 63 text

Benjamin Grandfond - PHP Tour Nantes 2012 http://www.odata.org/ Open Data Protocol jeudi 29 novembre 12

Slide 64

Slide 64 text

Benjamin Grandfond - PHP Tour Nantes 2012 http://data.stackexchange.com/stackoverflow/atom http://ebayodata.cloudapp.net/ http://www.nerddinner.com/Services/OData.svc/ Open Data Protocol jeudi 29 novembre 12

Slide 65

Slide 65 text

Benjamin Grandfond - PHP Tour Nantes 2012 Open Data Protocol jeudi 29 novembre 12

Slide 66

Slide 66 text

Benjamin Grandfond - PHP Tour Nantes 2012 Open Data Protocol jeudi 29 novembre 12

Slide 67

Slide 67 text

Benjamin Grandfond - PHP Tour Nantes 2012 Benjamin Grandfond - PHP Tour Nantes 2012 Questions ? @benjaM1 @benja-M-1 https://joind.in/7257 [email protected] jeudi 29 novembre 12