RESTful Style

Zack YL Shih

REST vs RESTful

Beauty vs Beautiful

Representational State Transfer

Standard
Protocol
Style

REST

  • Client-Server
  • Stateless
  • Cacheable
  • Uniform Interface
    • Identification of resources
    • Manipulation of resources
  • Layered System
  • Code-On-Demand

Be Simplicity

REST

  • It's all about RESOURCE
  • Every resource has an unique identifier
  • Generic connector interface (e.g. HTTP)
  • Resource identifier is immutable
  • Each operation is stateless

Restful Web Service

  • Web Service build with HTTP in RESTful Style
  • CRUD
    • Create (POST)
    • Read (GET)
    • Update (PUT)
    • Delete (DELETE)

Web Service Defination

  • Nouns: http://example.com/resources/
  • Verbs: GET, POST, PUT, DELETE
  • Content Types : JSON, XML

Constraints are liberating

In Action

Ethernet

/network/ethernets/{id}

{
    "id":1,
    "name":"eth0",
    "ip":"192.168.3.127",
    "mac":"FF:FF:FF:FF:FF:FF",
    "netmask":"255.255.255.0",
    "subnet":"192.168.3.0",
    "gateway":"192.168.3.254",
    "dns":["8.8.8.8", "8.8.4.4"],
    "currentStatus":1,
    "enable":1,
    "enableDhcp":0
}

Ethernet

/network/ethernets/{id}

Request:
    curl 'http://localhost/network/ethernets/1'

Response:
    200 (OK)
    Content-Type: application/json

    {
        "id":1,
        "name":"eth0",
        "ip":"192.168.3.127",
        "mac":"FF:FF:FF:FF:FF:FF",
        "netmask":"255.255.255.0",
        "subnet":"192.168.3.0",
        "gateway":"192.168.3.254",
        "dns":["8.8.8.8", "8.8.4.4"],
        "currentStatus":1,
        "enable":1,
        "enableDhcp":0
    }

Get eth0 information

Ethernet

/network/ethernets/{id}

Request:
    curl --request PUT \
         --header "Content-Type: application/json" \
         --data-binary '{"dns":["192.168.50.33", "168.95.1.1"]}' \
         'http://localhost/network/ethernets/1'

Response:
    200 (OK)
    Content-Type: application/json

    {
        "id":1,
        "name":"eth0",
        "ip":"192.168.3.127",
        "mac":"FF:FF:FF:FF:FF:FF",
        "netmask":"255.255.255.0",
        "subnet":"192.168.3.0",
        "gateway":"192.168.3.254",
        "dns":["8.8.8.8", "8.8.4.4"],
        "currentStatus":1,
        "enable":1,
        "enableDhcp":0
    }

Set eth0 DNS to 192.168.50.33 and 168.95.1.1

Wifi

/network/wifis

{
    "id":1,
    "name":"wlan0",
    "mode":1,
    "enable":1,
    "currentStatus":1,
    "mac":"FF:FF:FF:FF:FF:FF",
    "client": {
        "ssid": "MOXA-MAR-1",
        "signal": -79,
        "ip":"192.168.41.127",
        "netmask":"255.255.255.0",
        "subnet":"192.168.41.0",
        "gateway":"192.168.41.254",
        "enableDhcp": 1,
        "network": [
            {
                "enable": 1,
                "priority": 0,
                "ssid": "MOXA-MAR-1",
                "mode": 2,
                "keyFormat": 0,
                "psk": "moxamoxa",
                "txKey": 0,
                "key": ["", "", "", ""],
                "identity": "",
                "password": ""
            }
        ]
    }
}

CPU Status

/system/status/cpu

{
    "data": [
        {"time": "2014/09/14 15:18:48", "value": 54.1},
        {"time": "2014/09/14 15:19:48", "value": 40.2},
        {"time": "2014/09/14 15:20:48", "value": 35.9}
    ]
}

Memory Status

/system/status/memory

{
    "data": [
        {
            "time": "2014/09/14 15:18:48",
            "total": "200MB",
            "used": "50MB",
            "free": "150MB",
            "usedPercentage": 25
        },
        {
            "time": "2014/09/14 15:19:48",
            "total": "200MB",
            "used": "50MB",
            "free": "150MB",
            "usedPercentage": 25
        }
    ]
}

RESTful Style

By YuLun Shih

RESTful Style

  • 957