Repository: shreyashankar/gpt3-sandbox
File: api/demo_web_app.py (Python)

Code before:
                return error("id not found", 404)
            return json.dumps(gpt.get_all_examples())
        elif method == "POST":
            # adds an empty example.
            new_example = Example("", "")
            gpt.add_example(new_example)
            return json.dumps(gpt.get_all_examples())
        elif method == "PUT":
            # modifies an existing example.
            if example_id:
                example = gpt.get_example(example_id)
                if example:
                    if args.get("input", None):
                        example.input = args["input"]
                    if args.get("output", None):
                        example.output = args["output"]
                    return json.dumps(example.as_dict())
                return error("id not found", 404)
            return error("id required", 400)
        elif method == "DELETE":
            # deletes an example.
            if example_id:
                gpt.clear_example(example_id)
                return json.dumps(gpt.get_all_examples())
            return error("id required", 400)
        return error("Not implemented", 501)

    @app.route("/translate", methods=["GET", "POST"])
    def translate():
        # pylint: disable=unused-variable
        prompt = request.json["prompt"]
        response = gpt.submit_request(prompt)
        return {"text": response["choices"][0]["text"][7:]}

    subprocess.Popen(["yarn", "start"])
    app.run()


Review comment (suggestion):
No magic numbers. Please use the codes from the [`http`  std library](https://docs.python.org/3/library/http.html). For example, instead of `501` use

```python
from http import HTTPStatus 

# Use instead of magic number 501 
HTTPStatus.NOT_IMPLEMENTED
```

Code after:
        gpt.add_example(new_example)
        return json.dumps(gpt.get_all_examples())

    def put_example(args, example_id):
        """Modifies an existing example."""
        if not example_id:
            return error("id required", HTTPStatus.BAD_REQUEST)

        example = gpt.get_example(example_id)
        if not example:
            return error("id not found", HTTPStatus.NOT_FOUND)

        if "input" in args:
            example.input = args["input"]
        if "output" in args:
            example.output = args["output"]

        # update the example
        gpt.add_example(example)
        return json.dumps(example.as_dict())

    def delete_example(example_id):
        """Deletes an example."""
        if not example_id:
            return error("id required", HTTPStatus.BAD_REQUEST)

        gpt.delete_example(example_id)
        return json.dumps(gpt.get_all_examples())

    @app.route(
        "/examples",
        methods=["GET", "POST"],
        defaults={"example_id": ""},
    )
    @app.route(
        "/examples/<example_id>",
        methods=["GET", "PUT", "DELETE"],
    )
    def examples(example_id):
        method = request.method
        args = request.json
        if method == "GET":
            return get_example(example_id)
        if method == "POST":
            return post_example()
        if method == "PUT":
            return put_example(args, example_id)
        if method == "DELETE":
            return delete_example(example_id)
        return error("Not implemented", HTTPStatus.NOT_IMPLEMENTED)