I have a basic example of a REST API that I've done to control LEDs. I've read Dr. Dobbs article about REST and how to do it but I feel that my example doesn't look professional for some reason. Particularly the main menu on GET /
request. Could anyone please suggest improvements?
import falcon
import json
LEDS = {"red": "RED COLOR",
"green": "GREEN COLOR"}
class LedsResource(object):
def on_get(self, req, resp):
"""
Return main menu
"""
resp.status = falcon.HTTP_200 # This is the default status
resp.set_header('Powered-By', 'Falcon')
resp.body = (json.dumps({"led_url": req.url + "led/{green | red}/"
,"led_url_POST": {"state": "{1 | 0}"}}))
class LedsResourceTwo(object):
def on_get(self, req, resp, color):
"""
Return current state for LED indicated by the color parameter
"""
resp.status = falcon.HTTP_200
resp.set_header('Powered-By', 'Falcon')
if color in LEDS:
resp.body = (json.dumps({color: "simulated_state"}))
else:
resp.body = (json.dumps({"error": "color_not_found"}))
def on_post(self, req, resp, color):
"""
Change state of LED indicated by the color parameter
"""
if color in LEDS:
print("Received:", color, req.get_params("state"))
resp.body = (json.dumps({"response": "all_ok"}))
app = falcon.API()
app.add_route('/', LedsResource())
app.add_route('/led/{color}', LedsResourceTwo())
if __name__ == "__main__":
pass
Outputs:
GET /
HTTP/1.1 200 OK Connection: close Date: Mon, 21 Nov 2016 22:20:48 GMT Server: gunicorn/19.6.0 content-length: 93 content-type: application/json; charset=UTF-8 powered-by: Falcon { "led_url": "http://localhost:8000/led/{green | red}/", "led_url_POST": { "state": "{1 | 0}" } }
POST /led/green state=1
HTTP/1.1 200 OK Connection: close Date: Mon, 21 Nov 2016 22:22:02 GMT Server: gunicorn/19.6.0 content-length: 22 content-type: application/json; charset=UTF-8 { "response": "all_ok" }