RESTful APIs




      giancarlo valente - @gncvalente
l’idea e’ semplice:

usare il protocollo che fa’ “muovere” il web
per realizzare l’architettura a servizi di un sistema complesso
l’idea e’ semplice:

usare il protocollo che fa’ “muovere” il web
per realizzare l’architettura a servizi di un sistema complesso

ed ereditarne i vantaggi
TANTE IMPLEMENTAZIONI
         DI ARCHITETTURE
        A SERVIZI DISTRIBUITI
DCOM

CORBA

WS_*

HTTP-RPC

...
WWW
         il web e' un insieme di servizi (APIs),
   utilizzabili dagli uomini, attraverso un browser


                    esempio:
      http://www.google.com/search?q=puglia


un insieme di regole prefissate consente di usufruire
     di n-milioni di servizi in modo trasparente
il web e' l'API distribuita di maggior successo

                   perche'?
HTTP
                                DOCUMENT BASED
                                  PROTOCOL
          il client mette un documento in un 'envelope'
                       e lo spedisce al server

   il server risponde con un'altro documento in un 'enevlope'

 l'HTTP definisce in modo dettagliato come deve essere la busta,
ma e' completamente libero sul formato del documento contenuto
METHOD INFORMATION
SCOPING INFORMATION
     cosa devo fare, e su cosa




                             Per Classificare le varie
                             tipologie di servizi
METHOD INFORMATION
SCOPING INFORMATION
       cosa devo fare, e su cosa

                WS* -
    Method information ==> body
    Scoping information ==> body
          es. AddItem(cartId)
METHOD INFORMATION
 SCOPING INFORMATION
                  cosa devo fare, e su cosa

                          WS* -
              Method information ==> body
              Scoping information ==> body
                    es. AddItem(cartId)

                  HTTP-Rpc protocls
                Method information ==> url
               Scoping information ==> body
/carrello.php?op=AggiungiProdotto (body:CarrelloID ProdottoID)
METHOD INFORMATION
 SCOPING INFORMATION

                        REST
          Method information ==> HTTP verb
            Scoping information ==> URI *

es. POST http://www.mioecommerce.it/carrello/34Abc9/prodotti
             body: { Prodotto: 5467, Quantita’: 2}

               *(unique resource identifier)
DIFFERENZA
                     in REST ragiono per nomi
                       (invece che per verbi)

          i verbi sono fissi : GET, PUT, POST, DELETE ...

            dare un nome alle risorse e’ il mio lavoro



                              Resource Oriented
Operation Oriented
                              /posts   (GET, POST)
- CreateBlogPost
                              /post/{id}    (GET, PUT, DELETE)
- GetBlogPostByTag
                              /tags/{a}/posts    (GET)
- GetBlogPostByAuthor
                              /authors/{a}/posts (GET)
ESEMPIO
devo aggiungere un prodotto al carrello
          POST /cart/{cartId}
           Body: prodotto

    devo creare una nuova sessione
            POST /sessions

devo associare uno studente a una classe
PUT /classes/Math101/students/Giancarlo
RESOURCE ORIENTED
        ARCHITECTURES


ROA is a way of turning a problem into a RESTful web service:
   an arrangement of URIs, HTTP verbs, xml, json, html ...
PRINCIPI: ADRESSABILITY
           URI (adressability principle)


           naming things as resources
 every resouce is identified by a unique identifier
PRINCIPI: REPRESENTATION
    Rappresentazioni (representation oriented)


 la comunicazione avviene tramite rappresentazioni
PRINCIPI: UNIFORMINTERFACE
HTTP method (uniform interface principle)



     GET                  SAFE                IDEMPOTENT


    PUT                 NOT SAFE              IDEMPOTENT


    POST                NOT SAFE            NOT IDEMPOTENT


  DELETE                NOT SAFE              IDEMPOTENT
PRINCIPI: STATELESS
 Stateless (stateless communication)
FRAMEWORKS PER
 REALIZZARE UN
  REST SERVICE?
 Ruby ---> webmachine, sinatra

     node.js ---> connect

        C# --> nancy
WEBMACHINE
Erlang toolkit ... build full featured HTTP services

        progetto finale, mini blog engine,
            completamente restful
RESTFUL BLOG

/posts         GET, POST   html, json json
RESTFUL BLOG

/posts                 GET, POST     html, json json

/post/{permalink}   GET, PUT, DELETE html, json json
RESTFUL BLOG

/posts                 GET, POST     html, json json

/post/{permalink}   GET, PUT, DELETE html, json json

/tags                     GET        html, json
RESTFUL BLOG

/posts                 GET, POST     html, json json

/post/{permalink}   GET, PUT, DELETE html, json json

/tags                     GET        html, json

/tags/{tag}/posts         GET        html, json
RESTFUL BLOG

/posts                 GET, POST     html, json json

/post/{permalink}   GET, PUT, DELETE html, json json

/tags                     GET        html, json

/tags/{tag}/posts         GET        html, json

/authors                  GET        html, json
faccio vedere subito il

               RESTFUL BLOG                          risualtato finale

                                                     dalle risorse piu’ semplici
                                                     al blog
                                                     e etag
                      il codice erlang ...

                      parto dal JS della pagina
                      statica
/posts                            GET, POST       html, json json
                      e vado poi di risorsa in
                      risorsa seguendo le slide
/post/{permalink}          GET, PUT, DELETE html, json json
                      seguenti



/tags                                    GET      html, json

/tags/{tag}/posts                        GET      html, json

/authors                                 GET      html, json

/authors/{author}/posts                  GET      html, json
demo:

               simple resource

               git checkout “tag” v1

               fa’ output di un testo




HTTP E’ SOLO TESTO
CODICE

                   demo:

                   rappresentazioni
                   json e html        demo:

                   /posts             cambio titolo del post
demo:                                 e comunico un permanent
                   /post/x
                                      redirect
/posts POST, GET                      demo col browser
con client HTTP    git checkout v4
                                      git checkout v5
git checkout v3
RISORSA SEMPLICE
invia in output la configurazione di input
RISORSA CON TEMPLATE
    home page del blog, da template.
RAPPRESENTAZONI
risorsa con rappresentazioni in JSON e HTML
ACCEPT: JSON
risorsa che accetta in ingresso JSON
MATURITY LEVEL 3
le risorse includono dei link, per indirizzare il client, verso la URI
             della risorsa stessa, o verso sottorisorse




                                                     demo:

                                                     altre risorse, e links

                                                     git checkout v6
MATURITY LEVEL 3
le risorse includono dei link, per indirizzare il client, verso la URI
             della risorsa stessa, o verso sottorisorse




                                                       demo:

                                                       altre risorse, e links

                                                       git checkout v6
demo

                                        demo col browser

                                        git checkout v7




GET CONDIZIONALE
                  etag
  it is not caching, it is much more!
GET CONDIZIONALE
           etag
     with webmachine
MOVED PERMANENTLY
   il titolo del blogpost e’ cambiato
            il permalink cambia
IL BLOG
RESTful API, HTML, CSS ... e un po’ di JS (pjax)




                              demo:

                              git checkout v8

                              verifica che etag
                              funziona ancora!
                              anche col blog.
demo:

                                                Amazon Cloud Front




                       ALTRO ?
cache control, layers, index dell’api, cdn, web cache, application
                    firewall, reverse proxy ...
CONTATTI
Agile Coach, Giancarlo Valente

    twitter: @gncvalente

   www.giancarlovalente.it
ENJOY AND HAVE FUN
 Learn some functional programming language
          Clojure, Haskell, Erlang ...
          the hacker that you save,
                could be you!



       Agile Coach, Giancarlo Valente

            twitter: @gncvalente

          www.giancarlovalente.it

RESTful APIs (ITA) - /w WebMachine

  • 1.
    RESTful APIs giancarlo valente - @gncvalente
  • 2.
    l’idea e’ semplice: usareil protocollo che fa’ “muovere” il web per realizzare l’architettura a servizi di un sistema complesso
  • 3.
    l’idea e’ semplice: usareil protocollo che fa’ “muovere” il web per realizzare l’architettura a servizi di un sistema complesso ed ereditarne i vantaggi
  • 4.
    TANTE IMPLEMENTAZIONI DI ARCHITETTURE A SERVIZI DISTRIBUITI DCOM CORBA WS_* HTTP-RPC ...
  • 5.
    WWW il web e' un insieme di servizi (APIs), utilizzabili dagli uomini, attraverso un browser esempio: http://www.google.com/search?q=puglia un insieme di regole prefissate consente di usufruire di n-milioni di servizi in modo trasparente
  • 6.
    il web e'l'API distribuita di maggior successo perche'?
  • 7.
    HTTP DOCUMENT BASED PROTOCOL il client mette un documento in un 'envelope' e lo spedisce al server il server risponde con un'altro documento in un 'enevlope' l'HTTP definisce in modo dettagliato come deve essere la busta, ma e' completamente libero sul formato del documento contenuto
  • 8.
    METHOD INFORMATION SCOPING INFORMATION cosa devo fare, e su cosa Per Classificare le varie tipologie di servizi
  • 9.
    METHOD INFORMATION SCOPING INFORMATION cosa devo fare, e su cosa WS* - Method information ==> body Scoping information ==> body es. AddItem(cartId)
  • 10.
    METHOD INFORMATION SCOPINGINFORMATION cosa devo fare, e su cosa WS* - Method information ==> body Scoping information ==> body es. AddItem(cartId) HTTP-Rpc protocls Method information ==> url Scoping information ==> body /carrello.php?op=AggiungiProdotto (body:CarrelloID ProdottoID)
  • 11.
    METHOD INFORMATION SCOPINGINFORMATION REST Method information ==> HTTP verb Scoping information ==> URI * es. POST http://www.mioecommerce.it/carrello/34Abc9/prodotti body: { Prodotto: 5467, Quantita’: 2} *(unique resource identifier)
  • 12.
    DIFFERENZA in REST ragiono per nomi (invece che per verbi) i verbi sono fissi : GET, PUT, POST, DELETE ... dare un nome alle risorse e’ il mio lavoro Resource Oriented Operation Oriented /posts (GET, POST) - CreateBlogPost /post/{id} (GET, PUT, DELETE) - GetBlogPostByTag /tags/{a}/posts (GET) - GetBlogPostByAuthor /authors/{a}/posts (GET)
  • 13.
    ESEMPIO devo aggiungere unprodotto al carrello POST /cart/{cartId} Body: prodotto devo creare una nuova sessione POST /sessions devo associare uno studente a una classe PUT /classes/Math101/students/Giancarlo
  • 14.
    RESOURCE ORIENTED ARCHITECTURES ROA is a way of turning a problem into a RESTful web service: an arrangement of URIs, HTTP verbs, xml, json, html ...
  • 15.
    PRINCIPI: ADRESSABILITY URI (adressability principle) naming things as resources every resouce is identified by a unique identifier
  • 16.
    PRINCIPI: REPRESENTATION Rappresentazioni (representation oriented) la comunicazione avviene tramite rappresentazioni
  • 17.
    PRINCIPI: UNIFORMINTERFACE HTTP method(uniform interface principle) GET SAFE IDEMPOTENT PUT NOT SAFE IDEMPOTENT POST NOT SAFE NOT IDEMPOTENT DELETE NOT SAFE IDEMPOTENT
  • 18.
    PRINCIPI: STATELESS Stateless(stateless communication)
  • 19.
    FRAMEWORKS PER REALIZZAREUN REST SERVICE? Ruby ---> webmachine, sinatra node.js ---> connect C# --> nancy
  • 20.
    WEBMACHINE Erlang toolkit ...build full featured HTTP services progetto finale, mini blog engine, completamente restful
  • 21.
    RESTFUL BLOG /posts GET, POST html, json json
  • 22.
    RESTFUL BLOG /posts GET, POST html, json json /post/{permalink} GET, PUT, DELETE html, json json
  • 23.
    RESTFUL BLOG /posts GET, POST html, json json /post/{permalink} GET, PUT, DELETE html, json json /tags GET html, json
  • 24.
    RESTFUL BLOG /posts GET, POST html, json json /post/{permalink} GET, PUT, DELETE html, json json /tags GET html, json /tags/{tag}/posts GET html, json
  • 25.
    RESTFUL BLOG /posts GET, POST html, json json /post/{permalink} GET, PUT, DELETE html, json json /tags GET html, json /tags/{tag}/posts GET html, json /authors GET html, json
  • 26.
    faccio vedere subitoil RESTFUL BLOG risualtato finale dalle risorse piu’ semplici al blog e etag il codice erlang ... parto dal JS della pagina statica /posts GET, POST html, json json e vado poi di risorsa in risorsa seguendo le slide /post/{permalink} GET, PUT, DELETE html, json json seguenti /tags GET html, json /tags/{tag}/posts GET html, json /authors GET html, json /authors/{author}/posts GET html, json
  • 27.
    demo: simple resource git checkout “tag” v1 fa’ output di un testo HTTP E’ SOLO TESTO
  • 28.
    CODICE demo: rappresentazioni json e html demo: /posts cambio titolo del post demo: e comunico un permanent /post/x redirect /posts POST, GET demo col browser con client HTTP git checkout v4 git checkout v5 git checkout v3
  • 29.
    RISORSA SEMPLICE invia inoutput la configurazione di input
  • 30.
    RISORSA CON TEMPLATE home page del blog, da template.
  • 31.
  • 32.
    ACCEPT: JSON risorsa cheaccetta in ingresso JSON
  • 33.
    MATURITY LEVEL 3 lerisorse includono dei link, per indirizzare il client, verso la URI della risorsa stessa, o verso sottorisorse demo: altre risorse, e links git checkout v6
  • 34.
    MATURITY LEVEL 3 lerisorse includono dei link, per indirizzare il client, verso la URI della risorsa stessa, o verso sottorisorse demo: altre risorse, e links git checkout v6
  • 35.
    demo demo col browser git checkout v7 GET CONDIZIONALE etag it is not caching, it is much more!
  • 36.
    GET CONDIZIONALE etag with webmachine
  • 37.
    MOVED PERMANENTLY il titolo del blogpost e’ cambiato il permalink cambia
  • 38.
    IL BLOG RESTful API,HTML, CSS ... e un po’ di JS (pjax) demo: git checkout v8 verifica che etag funziona ancora! anche col blog.
  • 39.
    demo: Amazon Cloud Front ALTRO ? cache control, layers, index dell’api, cdn, web cache, application firewall, reverse proxy ...
  • 40.
    CONTATTI Agile Coach, GiancarloValente twitter: @gncvalente www.giancarlovalente.it
  • 42.
    ENJOY AND HAVEFUN Learn some functional programming language Clojure, Haskell, Erlang ... the hacker that you save, could be you! Agile Coach, Giancarlo Valente twitter: @gncvalente www.giancarlovalente.it