Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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"
}
share|improve this question
    
Apologies for that, it's fixed now – Shady Programmer Nov 22 '16 at 10:10
    
looks more or less fine the only issue I see is that you always use parenthesis around the value of resp.body, you don't need them there. Apart from that, I would have write some things a bit different but it's not about your code being ugly or wrongs, it look good. It's just I prefer a bit different style. – Alex Nov 23 '16 at 16:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.