Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hypermedia APIs for the Internet of Things

Oliver Wolf
September 03, 2014

Hypermedia APIs for the Internet of Things

Oliver Wolf

September 03, 2014
Tweet

More Decks by Oliver Wolf

Other Decks in Technology

Transcript

  1. The Internet Of Things Architectural Tiers GW GW GW API

    App App App Data Collection MQTT CoAP ZigBee Bluetooth ZigBee Bluetooth HTTP JSON HTML Things
 Sensors, Actors Mesh Networks Gateway
 Personal Area Networks User
 User Interface Cloud
 Application/Service Platform
  2. Characteristics of a Good API
 (according to Joshua Bloch)
 ‣

    Easy to learn ‣ Easy to use, even without documentation ‣ Hard to misuse ‣ Easy to read and maintain code that uses it ‣ Sufficiently powerful to satisfy requirements ‣ Easy to extend ‣ Appropriate to the audience
  3. The “Uniform Interface” constraint revisited Identification of resources Manipulation of

    resources through representations Hypermedia as the engine of application state Self-descriptive messages
  4. Simple canonical JSON representation 
 for HTTP API entry points

    (“home documents”) Special media type Cacheable on client side Direct link (1 resource) Link relation type Relative URI GET / HTTP/1.1! Host: example.org! Accept: application/json-home! ! HTTP/1.1 200 OK! Cache-Control: max-age=3600! Content-Type: application/json-home! {! "resources": {! "http://example.org/rel/widgets": {! "href": "/widgets/"! },! "http://example.org/rel/widget": {! "href-template": "/widgets/{widget_id}",! "href-vars": {! "widget_id": "http://example.org/param/widget"! },! "hints": {! "allow": ["GET", "PUT", "DELETE", "PATCH"],! "representations": ["application/json"],! "accept-patch": ["application/json-patch"],! "accept-post": ["application/xml"],! "accept-ranges": ["bytes"]! }! }! }! }!
  5. Link Relation Types identify the semantics of a link Two

    types of Relation Types: Registered alternate, appendix, bookmark, chapter, contents, copyright, current, describedby, edit, edit-media, enclosure, first, glossary, help, hub, index, last, latest- version, license, next, next-archive, payment, prev, predecessor-version, previous, prev-archive, related, replies, section, self, service, start, stylesheet, subsection, successor-version, up, version- history, via, working-copy, working-copy-of Extension Uniquely identified by a URI which can (but doesn’t have to) point to a document describing the link sematics Identified by a registered token, currently one of:
  6. Simple canonical JSON representation 
 for HTTP API entry points

    (“home documents”) Special media type Cacheable on client side Direct link (1 resource) Templated link
 (0..n resources)
 Link relation type Relative URI Link template Template variables Link relation type GET / HTTP/1.1! Host: example.org! Accept: application/json-home! ! HTTP/1.1 200 OK! Cache-Control: max-age=3600! Content-Type: application/json-home! {! "resources": {! "http://example.org/rel/widgets": {! "href": "/widgets/"! },! "http://example.org/rel/widget": {! "href-template": "/widgets/{widget_id}",! "href-vars": {! "widget_id": "http://example.org/param/widget"! },! "hints": {! "allow": ["GET", "PUT", "DELETE", "PATCH"],! "representations": ["application/json"],! "accept-patch": ["application/json-patch"],! "accept-post": ["application/xml"],! "accept-ranges": ["bytes"]! }! }! }! }!
  7. Template Examples Template Examples Template Examples URI Templates describe a

    range of URIs 
 through variable expansion http://example.com/~fred/! http://example.com/~mark/ http://example.com/~{username}/ http://example.com/dictionary/c/cat! http://example.com/dictionary/d/dog http://example.com/dictionary/{term:1}/{term} http://example.com/search?q=cat&lang=en! http://example.com/search?q=chien&lang=fr http://example.com/search{?q,lang} (The expression syntax is a little bit richer than this, actually.)
  8. Simple canonical JSON representation 
 for HTTP API entry points

    (“home documents”) Special media type Cacheable on client side Direct link (1 resource) Templated link
 (0..n resources)
 Link relation type Relative URI Link template Template variables Hints Link relation type GET / HTTP/1.1! Host: example.org! Accept: application/json-home! ! HTTP/1.1 200 OK! Cache-Control: max-age=3600! Content-Type: application/json-home! {! "resources": {! "http://example.org/rel/widgets": {! "href": "/widgets/"! },! "http://example.org/rel/widget": {! "href-template": "/widgets/{widget_id}",! "href-vars": {! "widget_id": "http://example.org/param/widget"! },! "hints": {! "allow": ["GET", "PUT", "DELETE", "PATCH"],! "representations": ["application/json"],! "accept-patch": ["application/json-patch"],! "accept-post": ["application/xml"],! "accept-ranges": ["bytes"]! }! }! }! }!
  9. Mike Amundsen’s “H-Factors” CL CU CM CR LT LI LN

    LO LE Link Factors Control Factors Source: M. Amundsen: Hypermedia APIs with HTML5 & Node, O’Reilly, 2011
  10. Mike Amundsen’s “H-Factors” Embedding Links CL CU CM CR LT

    LI LN LO LE ‣ dereference URI using HTTP GET and merge result with original document content Example: IMG markup tag in HTML ! <img src=”...”/>
  11. Mike Amundsen’s “H-Factors” Outbound Links CL CU CM CR LT

    LI LN LO LE ‣ dereference URI using HTTP GET and replace original document content with result (traversal/navigational link) Example: A markup tag in HTML ! <a href=”...”> ... </a>
  12. Mike Amundsen’s “H-Factors” Templated Links CL CU CM CR LT

    LI LN LO LE ‣ provide a way to indicate parameters that can be supplied when executing an HTTP GET Example: URI Templates ! <link href=”http://example.org?search={search}”>
  13. Mike Amundsen’s “H-Factors” Idempotent Links CL CU CM CR LT

    LI LN LO LE ‣ provides a way to define support for idempotent operations on server via HTTP PUT and DELETE methods Example: Link with link relation type “edit” in the Atom media type ! <link rel=”edit” href=”http://example.org/edit/1”/>
  14. Mike Amundsen’s “H-Factors” Non-Idempotent Links CL CU CM CR LT

    LI LN LO LE ‣ provides a way to define support for non-idempotent operations on server via the HTTP POST method Example: HTTP FORM element ! <form method=”post” ! action=”http://example.org/comments>! <textarea name=”comment”></textarea>! <input type=”submit”/>! </form>
  15. Mike Amundsen’s “H-Factors” Read Controls CL CU CM CR LT

    LI LN LO LE ‣ provides a way to support manipulation of control data for HTTP GET requests Example: XInclude markup w/ accept-language attribute ! <x:include href=”http://example.org/newsfeed”! accept-language=”de, en-gb;q=0.8” />
  16. Example: HTML FORM w/ enctype attribute ! <form method=”post” !

    action=”http://example.org/comments>! enctype=”text/plain”! <textarea name=”comment”></textarea>! <input type=”submit”/>! </form> Mike Amundsen’s “H-Factors” Update Controls CL CU CM CR LT LI LN LO LE ‣ provides a way to support manipulation of control data for HTTP PUT/POST requests
  17. Example: HTML FORM’s “method” element ! <form method=”post”> ! ...!

    </form>! ! <form method=”get”> ! ...! </form> Mike Amundsen’s “H-Factors” Method Controls CL CU CM CR LT LI LN LO LE ‣ support the ability to change the control data for the protocol method used
  18. Example: well-known link relation type “stylesheet” in HTML ! <link

    rel=”stylesheet” href=”...”/> Mike Amundsen’s “H-Factors” Link Annotation Controls CL CU CM CR LT LI LN LO LE ‣ decorate links with additional metadata communicating a link’s semantics to a client
  19. { "class": [ "order" ], "properties": { "orderNumber": 42, "itemCount":

    3, "status": "pending" }, "entities": [ { "class": [ "items", "collection" ], "rel": [ "http://x.io/rels/order-items" ], "href": "http://api.x.io/orders/42/items" }, { "class": [ "info", "customer" ], "rel": [ "http://x.io/rels/customer" ], "properties": { "customerId": "pj123", "name": "Peter Joseph" }, "links": [ { "rel": [ "self" ], "href": "http://api.x.io/customers/pj123" } ] } ], "actions": [ { "name": "add-item", "title": "Add Item", "method": "POST", "href": "http://api.x.io/orders/42/items", "type": "application/x-www-form-urlencoded", "fields": [ { "name": "orderNumber", "type": "hidden", "value": "42" }, { "name": "productCode", "type": "text" }, { "name": "quantity", "type": "number" } ] } ], "links": [ { "rel": [ "self" ], "href": "http://api.x.io/orders/42" }, { "rel": [ "previous" ], "href": "http://api.x.io/orders/41" }, { "rel": [ "next" ], "href": "http://api.x.io/orders/43" } ] } SIREN example Order Customer OrderItem
  20. { "class": [ "order" ], "properties": { "orderNumber": 42, "itemCount":

    3, "status": "pending" }, "entities": [ { "class": [ "items", "collection" ], "rel": [ "http://x.io/rels/order-items" ], "href": "http://api.x.io/orders/42/items" }, { "class": [ "info", "customer" ], "rel": [ "http://x.io/rels/customer" ], "properties": { "customerId": "pj123", "name": "Peter Joseph" }, "links": [ { "rel": [ "self" ], "href": "http://api.x.io/customers/pj123" } ] } ], "actions": [ { "name": "add-item", "title": "Add Item", "method": "POST", "href": "http://api.x.io/orders/42/items", "type": "application/x-www-form-urlencoded", "fields": [ { "name": "orderNumber", "type": "hidden", "value": "42" }, { "name": "productCode", "type": "text" }, { "name": "quantity", "type": "number" } ] } ], "links": [ { "rel": [ "self" ], "href": "http://api.x.io/orders/42" }, { "rel": [ "previous" ], "href": "http://api.x.io/orders/41" }, { "rel": [ "next" ], "href": "http://api.x.io/orders/43" } ] } An entity (“Order”) The entity’s properties (state) Collection of related (sub-)entities First sub-entity: 
 collection of order items (linked) Second sub-entity: 
 customer (embedded) The actions that can be performed based on the current state of the entity One action: “add-item” Navigational links
  21. GET$/household$ ! {" " "class":""household"," " "properties":"{" " " "household_id":"4711,"

    " " "location":""Berlin,"Germany"" " }," " "entities":"[" " " {" " " " "class":"["gateway"]," " " " "rel":"["http://acme.com/rels/gateway"]," " " " "href":""http://api.acme.com/households/4711/gateway"" " " }," " " {" " " " "class":"["devices","“collection"]," " " " "rel":"["http://acme.com/rels/devices"]," " " " "href":""http://api.acme.com/households/4711/devices"" " " }" " ]," " "links":"[" " " {""rel":"[“self"],""href":""http://api.acme.com/households/4711""}" " ]" ! } Entity “Household” Related entity “Gateway” (linked) Collection of “Device” (linked) Self link
  22. GET$/households/4711/gateway$ ! {" " "class":""gateway"," " "properties":"{" " " "gateway_id":"0815,"

    " " "mode":""NORMAL"" " }," " "actions":"[" """"" {" """""""" "name":""setPmode"," """""""" "method":""PUT"," """""""" "href":""http://api.acme.com/households/4711/gateway/mode"," """""""" "type":""application/json"," """""""" "fields":"[" """"""""" " {""name":""mode",""type":""radio""}" """"""""" ]" """"""""}" " ]," " "links":"[" " " {""rel":"["self"],"
 " " """href":""http://api.acme.com/households/4711/gateway""}" " ]" }" Entity “Gateway” Valid actions on “Gateway” based
 on current state Self link
  23. GET$/households/4711/devices$ ! {" " "class":"["devices",""collection"]," " "entities":"[" " " {"

    " " " "class":"["device",""collectionPitem"]," " " " "rel":"["http://acme.com/rels/device"]," " " " "href":""http://api.acme.com/households/4711/devices/1234"" " " }," " " {" " " " "class":"["device",""collectionPitem"]," " " " "rel":"["http://acme.com/rels/device"]," " " " "href":""http://api.acme.com/households/4711/devices/5678"" " " }," " " {" """""""""""class":"["device",""collectionPitem"]," " " " "rel":"["http://acme.com/rels/device"]," " " " "href":""http://api.acme.com/households/4711/devices/9012"" " " }" " ]," " "links":"[" " " {""rel":"["self"],"
 """"""""""href":""http://api.acme.com/households/4711/devices""}" " ]" }" Collection of “Device” entities Entity “Device” (linked) Self link
  24. GET$/households/4711/devices/5678$ ! {" " "class":""device"," " "properties":"{" " " "device_id":"5678,"

    " " "device_type":""SWITCHABLE_LIGHT"," " " "display_name":""Living"room"ambient"lamp"" " " "power_status":""off"" " }," " "actions":"[" " " {" " " " "name":""powerPon"," " " " "method":""POST"," " " " "href":""http://api.acme.com/households/4711/devices/5678/power_on"," " " }," " " {" " " " "name":""setPdisplay_name"," " " " "method":""PUT"," " " " "href":""http://api.acme.com/households/4711/devices/5678"," " " " "type":""application/json"," " " " "fields":"[{""name":""display_name",""type":""text"}]" " " }" " ]," " "links":"[" " " {""rel":"["self"],"
 """"""""""href":"“http://api.acme.com/households/4711/devices/5678""}" " ]" }
  25. GET$/households/4711/devices/5678$ ! {" " "class":""device"," " "properties":"{" " " "device_id":"5678,"

    " " "device_type":""SWITCHABLE_LIGHT"," " " "display_name":""Living"room"ambient"lamp"" " " "power_status":""off"" " }," " "actions":"[" " " {" " " " "name":""powerPon"," " " " "method":""POST"," " " " "href":""http://api.acme.com/households/4711/devices/5678/power_on"," " " }," " " {" " " " "name":""setPdisplay_name"," " " " "method":""PUT"," " " " "href":""http://api.acme.com/households/4711/devices/5678"," " " " "type":""application/json"," " " " "fields":"[{""name":""display_name",""type":""text"}]" " " }" " ]," " "links":"[" " " {""rel":"["self"],"
 """"""""""href":"“http://api.acme.com/households/4711/devices/5678""}" " ]" } Entity “Device” Valid actions on “Device” based
 on current state
  26. GET$/households/4711/devices/5678$ ! {" " "class":""device"," " "properties":"{" " " "device_id":"5678,"

    " " "device_type":""SWITCHABLE_LIGHT"," " " "display_name":""Living"room"ambient"lamp"" " " "power_status":""on"" " }," " "actions":"[" " " {" " " " "name":""powerPoff"," " " " "method":""POST"," " " " "href":""http://api.acme.com/households/4711/devices/5678/power_off"," " " }," " " {" " " " "name":""setPdisplay_name"," " " " "method":""PUT"," " " " "href":""http://api.acme.com/households/4711/devices/5678"," " " " "type":""application/json"," " " " "fields":"[{""name":""display_name",""type":""text"}]" " " }" " ]," " "links":"[" " " {""rel":"["self"],"
 """"""""""href":"“http://api.acme.com/households/4711/devices/5678""}" " ]" }
  27. GET$/households/4711/devices/5678$ ! {" " "class":""device"," " "properties":"{" " " "device_id":"5678,"

    " " "device_type":""SWITCHABLE_LIGHT"," " " "display_name":""Living"room"ambient"lamp"" " " "power_status":""on"" " }," " "actions":"[" " " {" " " " "name":""powerPoff"," " " " "method":""PUT"," " " " "href":"“http://api.acme.com/households/4711/devices/5678"," " " " "type":""application/json"," " " " "fields":"[{""name":""power_status",""type":""text",""value":""off"}]" " " }," " " {" " " " "name":""setPdisplay_name"," " " " "method":""PUT"," " " " "href":""http://api.acme.com/households/4711/devices/5678"," " " " "type":""application/json"," " " " "fields":"[{""name":""display_name",""type":""text"}]" " " }" " ]," " "links":"[" " " {""rel":"["self"],"
 """"""""""""href":"“http://api.acme.com/households/4711/devices/5678""}" " ]" } API change!
  28. GET$/households/4711/devices/1234$ ! {" " "class":""device"," " "properties":"{" " " "device_id":"1234,"

    " " "device_type":""HEATING_VALVE"," " " "display_name":""Heating"Valve"Living"Room”," " " "current_temperature":"20," " " "set_temperature":"22" " }," " "actions":"[" " " {" " " " "name":"“setPset_temperature"," " " " "method":""PUT"," " " " "href":"“http://api.yetu.com/households/4711/devices/1234"," " " " "type":""application/json"," " " " "fields":"[{""name":"“set_temperature",""type":""number"}]" " " }," " " {" " " " "name":""setPdisplay_name"," " " " "method":""PUT"," " " " "href":""http://api.acme.com/households/4711/devices/1234"," " " " "type":""application/json"," " " " "fields":"[{""name":""display_name",""type":""text"}]" " " }" " ]," " "links":"[" " " {""rel":"["self"],"
 """"""""""""""href":"“http://api.acme.com/households/4711/devices/1234""}" " ]" }