Hypermedia API – the Developer Journey – State Machine of Turnstile Gate



Hypermedia API – the Developer Journey – State Machine of Turnstile Gate

0 0


Hypermedia-API--the-Developer-Journey

Presenation about steps to create hypermedia API

On Github SamanSattari / Hypermedia-API--the-Developer-Journey

Hypermedia API

the Developer Journey

Saman Sattari Khavas saman.sattari.khavas@ing.nl

8 hour department wide hackathons

Our Goal

  • Make an application with ability to persist state
  • You can access the application from different devices and start from where you left off
  • A backend with a finite state machine
  • A good case study for Hypermedia Api

How to create a Hypermedia API?

Evaluate Business Processes Create State Machine Evaluate Media Types Create Media Type Implement

How to create a Hypermedia API?

Evaluate Business Processes Create State Machine Evaluate Media Types Create Media Type Implement

Evaluate Business Processes

  • Employee ab12cd logs in
  • Employee searches customer John Johnson
  • Employee adds customer John Johnson to the sales session (dialog box)
  • Employee swipes the sales session so his colleague can take over
  • Employee qr34st logs in
  • Employee Checks the dialog box and claims the sales session

How to create a Hypermedia API?

Evaluate Business Processes Create State Machine Evaluate Media Types Create Media Type Implement

Create State Machine

What is an state machine?

  • It is conceived as an abstract machine that can be in one of a finite number of states.
  • The machine is in only one state at a time.
  • consists of :
    • state
    • transition Function
  • Classic examples: traffic lights, vending machines and elevators

State Machine of Turnstile Gate

State Machine of traffic light

Create State Machine

Steps to make Hypermedia API

Evaluate Business Processes Create State Machine Define Resources Create State Machine

of each resource

Evaluate Media Types Create Media Type Implement

Define Resources

Steps to make Hypermedia API

Evaluate Business Processes Create State Machine Define Resources Create State Machine of each resource Evaluate Media Types Create Media Type Implement

State Machine of Employee

State Machine of Customer

State Machine of SalesSession

Steps to make Hypermedia API

Evaluate Business Processes Create State Machine Define Resources Create State Machine of each resource Evaluate Media Types Create Media Type Implement

Evaluate Media Types

  • application/json
    • need information about hypermedia
  • application/hal+json
{
        "_links": {
        "self": { "href": "/orders" },
        "curies": [{ "name": "ea", "href": "http://example.com/docs/rels/{rel}", "templated": true }],
        "next": { "href": "/orders?page=2" },
        "ea:find": { "href": "/orders{?id}", "templated": true },
        "ea:admin": [{
        "href": "/admins/2",
        "title": "Fred"
        }, {
        "href": "/admins/5",
        "title": "Kate"
        }]
        },
        "currentlyProcessing": 14,
        "shippedToday": 20,
        "_embedded": {
        "ea:order": [{
        "_links": {
        "self": { "href": "/orders/123" },
        "ea:basket": { "href": "/baskets/98712" },
        "ea:customer": { "href": "/customers/7809" }
        },
        "total": 30.00,
        "currency": "USD",
        "status": "shipped"
        }, {
        "_links": {
        "self": { "href": "/orders/124" },
        "ea:basket": { "href": "/baskets/97213" },
        "ea:customer": { "href": "/customers/12369" }
        },
        "total": 20.00,
        "currency": "USD",
        "status": "processing"
        }]
        }
        }

Chosen Media Type

application/json with a touch of HAL

Steps to make Hypermedia API

Evaluate Business Processes Create State Machine Define Resources Create State Machine of each resource Evaluate Media Types Create Media Type Implement

Create Media Type

{
        "_links": {
        "self": {
        "href":"/"
        }
        },
        "mysessions": [
        {"href":"/salesessions/2"}
        ],
        "generalsessions": [
        {"href":"/salesessions/1/claim", "method": "POST"},
        {"href":"/salesessions/3/claim", "method": "POST"}
        ],
        "search": {
        "href":"/customers",
        "query": ["accountNumber", "firstName"]
        }
        }
API Designer

HAL

{
        "_links": {
        "self": { "href": "/orders" },
        "curies": [{ "name": "ea", "href": "http://example.com/docs/rels/{rel}", "templated": true }],
        "next": { "href": "/orders?page=2" },
        "ea:find": { "href": "/orders{?id}", "templated": true },
        "ea:admin": [{
        "href": "/admins/2",
        "title": "Fred"
        }, {
        "href": "/admins/5",
        "title": "Kate"
        }]
        },
        "currentlyProcessing": 14,
        "shippedToday": 20,
        "_embedded": {
        "ea:order": [{
        "_links": {
        "self": { "href": "/orders/123" },
        "ea:basket": { "href": "/baskets/98712" },
        "ea:customer": { "href": "/customers/7809" }
        },
        "total": 30.00,
        "currency": "USD",
        "status": "shipped"
        }, {
        "_links": {
        "self": { "href": "/orders/124" },
        "ea:basket": { "href": "/baskets/97213" },
        "ea:customer": { "href": "/customers/12369" }
        },
        "total": 20.00,
        "currency": "USD",
        "status": "processing"
        }]
        }
        }

Steps to make Hypermedia API

Evaluate Business Processes Create State Machine Define Resources Create State Machine of each resource Evaluate Media Types Create Media Type Implementation

Implementation

Recap

Steps to make Hypermedia API

Evaluate Business Processes Create State Machine Define Resources Create State Machine of each resource Evaluate Media Types Create Media Type Implementation

technologies

team

Api team

Yun Bei Rui Chen Saman Sattari Khavas Saurabh Joshi Satya Deeleep Bandaru

Front end team

Martijn van der Wijst Willem Veelenturf Folkert Schram de Jong Jasmin Jane Rengers-Gerritsen Erik Mons Chris aukema Helma Duyvestein

Lessons Learned

  • Creating a hypermedia API is very hard
  • Know your Actor
  • Define Resources
  • Define state machine
  • Use a more descriptive media type
  • Implement the API before the front end

Questions?