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

Code before:
    @app.route("/params", methods=["GET"])
    def get_params():
        # pylint: disable=unused-variable
        response = config.json()
        return response

    def error(err_msg, status_code):
        return Response(json.dumps({"error": err_msg}), status=status_code)

    @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":
            # gets either a single example or all of them.
            if example_id:
                example = gpt.get_example(example_id)
                if example:
                    return json.dumps(example.as_dict())
                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)


Review comment (suggestion):
Separate each of the HTTP methods into their own functions so logic reads

```python
if method == "GET":
    return get_examples(args, example_id)
if method == "PUT":
    return put_examples(args, example_id)
if method == "POST":
    return post_examples(args)
if method == "DELETE":
    return delete_examples(args, example_id)

return error("Not implemented", 501)
```

Code after:

    @app.route("/params", methods=["GET"])
    def get_params():
        # pylint: disable=unused-variable
        response = config.json()
        return response

    def error(err_msg, status_code):
        return Response(json.dumps({"error": err_msg}), status=status_code)

    def get_example(example_id):
        """Gets a single example or all the examples."""
        # return all examples
        if not example_id:
            return json.dumps(gpt.get_all_examples())

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

    def post_example():
        """Adds an empty example."""
        new_example = Example("", "")
        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)