From dacbc01bc66b1e73b8a524d6fd8b6cc93a5f52d5 Mon Sep 17 00:00:00 2001 From: beepscore Date: Tue, 23 Apr 2019 22:47:39 -0700 Subject: [PATCH 1/7] Add flask_util.py --- flask_util.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 flask_util.py diff --git a/flask_util.py b/flask_util.py new file mode 100644 index 0000000..3c4ec40 --- /dev/null +++ b/flask_util.py @@ -0,0 +1,27 @@ +#!/usr/bin/env/python3 + +from enum import Enum +from flask import jsonify + + +class ServiceConstants(Enum): + API_NAME = 'tv' + API_NAME_KEY = 'api_name' + RESPONSE_KEY = 'response' + VERSION = '1.0' + VERSION_KEY = 'version' + + +def flask_response(response_string): + """ + https://stackoverflow.com/questions/7907596/json-dumps-vs-flask-jsonify + :param response_string: + :return: a flask.Response object + """ + data = {ServiceConstants.API_NAME_KEY: ServiceConstants.API_NAME, + ServiceConstants.VERSION_KEY: ServiceConstants.VERSION, + ServiceConstants.RESPONSE_KEY: response_string} + + json = jsonify(data) + return json + From d5d3a186fabbf3fd0ffd2ceab91a44c4024b7e89 Mon Sep 17 00:00:00 2001 From: beepscore Date: Tue, 23 Apr 2019 22:48:38 -0700 Subject: [PATCH 2/7] In service.py use flask_util --- service.py | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/service.py b/service.py index 455247a..a803f15 100644 --- a/service.py +++ b/service.py @@ -5,6 +5,7 @@ """ from flask import Flask, jsonify, request +import flask_util from remote_command import RemoteCommand from ir_remote import transmit_command_ir @@ -15,20 +16,13 @@ # app is a flask object app = Flask(__name__) -API_NAME_KEY = 'api_name' -RESPONSE_KEY = 'response' -VERSION_KEY = 'version' - -API_NAME = 'tv' -VERSION = '1.0' - def route(command): """ :param command: a RemoteCommand :return: route string """ - return "/api/v1/{}/{}/".format(API_NAME, command.value) + return "/api/v1/{}/{}/".format(flask_util.ServiceConstants.API_NAME, command.value) def transmit_command(command): @@ -44,14 +38,10 @@ def transmit_command(command): transmit_command_ir(command) # f string requires Python >= 3.6, so don't use it yet. - # response = f'transmitted command {command}' - response = 'transmitted command {}'.format(command.value) + # response_string = f'transmitted command {command}' + response_string = 'transmitted command {}'.format(command.value) - data = {API_NAME_KEY: API_NAME, - VERSION_KEY: VERSION, - RESPONSE_KEY: response} - - return jsonify(data) + return flask_util.flask_response(response_string) # / is the website root, the entry point @@ -59,14 +49,10 @@ def transmit_command(command): # home http://127.0.0.1 # port :5000 @app.route('/') -@app.route("/api/v1/{}/ping/".format(API_NAME), methods=['GET']) +@app.route("/api/v1/{}/ping/".format(flask_util.ServiceConstants.API_NAME), methods=['GET']) def api_status(): if request.method == 'GET': - data = {API_NAME_KEY: API_NAME, - VERSION_KEY: VERSION, - RESPONSE_KEY: 'pong'} - - return jsonify(data) + return flask_util.flask_response('pong') # POST but not GET because GET should not change any state on the server From 12301f3d11146db7190ed5ad07d9dbc76e01b460 Mon Sep 17 00:00:00 2001 From: beepscore Date: Tue, 23 Apr 2019 22:50:54 -0700 Subject: [PATCH 3/7] In volume_decrease_increase return a value. # TODO: check if returning here fixes ValueError: View function did not return a response https://stackoverflow.com/questions/25034123/flask-value-error-view-function-did-not-return-a-response --- scheduler.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scheduler.py b/scheduler.py index e519d25..35b056e 100644 --- a/scheduler.py +++ b/scheduler.py @@ -9,6 +9,7 @@ from remote_command import RemoteCommand from ir_remote import transmit_command_ir import quiet_times +import flask_util import time @@ -109,6 +110,9 @@ def volume_decrease_increase(decrease_count=4, increase_count=3, duration_second transmit_command_ir(RemoteCommand.VOLUME_INCREASE) time.sleep(5) + # TODO: check if returning here fixes ValueError: View function did not return a response + return flask_util.flask_response('volume-decrease-increase') + if __name__ == '__main__': From a4399d6dff6d948d65e9e40259df6d0d25d4c316 Mon Sep 17 00:00:00 2001 From: beepscore Date: Tue, 23 Apr 2019 22:55:15 -0700 Subject: [PATCH 4/7] Update reference to API_NAME --- service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service.py b/service.py index a803f15..5646634 100644 --- a/service.py +++ b/service.py @@ -87,7 +87,7 @@ def volume_increase(): return transmit_command(RemoteCommand.VOLUME_INCREASE) -@app.route("/api/v1/{}/volume-decrease-increase/".format(API_NAME), methods=['POST']) +@app.route("/api/v1/{}/volume-decrease-increase/".format(flask_util.ServiceConstants.API_NAME), methods=['POST']) def volume_duck(): return scheduler.Scheduler.volume_decrease_increase(decrease_count=4, increase_count=3, duration_seconds=10) From 744ab38f587d1d3d6a4bb91c05e490283a93d059 Mon Sep 17 00:00:00 2001 From: beepscore Date: Tue, 23 Apr 2019 23:35:11 -0700 Subject: [PATCH 5/7] Attempt to fix broken routes, probably due to enum. e.g. 404 Client Error: NOT FOUND for url: http://10.0.0.4:5000/api/v1/tv/volume-decrease-increase/. --- flask_util.py | 7 ++++--- service.py | 10 +++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/flask_util.py b/flask_util.py index 3c4ec40..faa0e29 100644 --- a/flask_util.py +++ b/flask_util.py @@ -18,10 +18,11 @@ def flask_response(response_string): :param response_string: :return: a flask.Response object """ - data = {ServiceConstants.API_NAME_KEY: ServiceConstants.API_NAME, - ServiceConstants.VERSION_KEY: ServiceConstants.VERSION, - ServiceConstants.RESPONSE_KEY: response_string} + data = {ServiceConstants.API_NAME_KEY.value: ServiceConstants.API_NAME.value, + ServiceConstants.VERSION_KEY.value: ServiceConstants.VERSION.value, + ServiceConstants.RESPONSE_KEY.value: response_string.value} + print('flask_response data: {}'.format(data)) json = jsonify(data) return json diff --git a/service.py b/service.py index 5646634..3e921c7 100644 --- a/service.py +++ b/service.py @@ -22,7 +22,7 @@ def route(command): :param command: a RemoteCommand :return: route string """ - return "/api/v1/{}/{}/".format(flask_util.ServiceConstants.API_NAME, command.value) + return "/api/v1/{}/{}/".format(flask_util.ServiceConstants.API_NAME.value, command.value) def transmit_command(command): @@ -45,11 +45,11 @@ def transmit_command(command): # / is the website root, the entry point -# http://127.0.0.1:5000 +# http://10.0.0.4:5000 # home http://127.0.0.1 # port :5000 @app.route('/') -@app.route("/api/v1/{}/ping/".format(flask_util.ServiceConstants.API_NAME), methods=['GET']) +@app.route("/api/v1/{}/ping/".format(flask_util.ServiceConstants.API_NAME.value), methods=['GET']) def api_status(): if request.method == 'GET': return flask_util.flask_response('pong') @@ -87,8 +87,8 @@ def volume_increase(): return transmit_command(RemoteCommand.VOLUME_INCREASE) -@app.route("/api/v1/{}/volume-decrease-increase/".format(flask_util.ServiceConstants.API_NAME), methods=['POST']) -def volume_duck(): +@app.route("/api/v1/{}/volume-decrease-increase/".format(flask_util.ServiceConstants.API_NAME.value), methods=['POST']) +def volume_decrease_increase(): return scheduler.Scheduler.volume_decrease_increase(decrease_count=4, increase_count=3, duration_seconds=10) From 379b2d43630fcc73e6b4d4888f8d32d93a839dd5 Mon Sep 17 00:00:00 2001 From: beepscore Date: Tue, 23 Apr 2019 23:37:53 -0700 Subject: [PATCH 6/7] Revert "Attempt to fix broken routes, probably due to enum. e.g. 404 Client Error: NOT FOUND for url: http://10.0.0.4:5000/api/v1/tv/volume-decrease-increase/." This reverts commit 744ab38f587d1d3d6a4bb91c05e490283a93d059. --- flask_util.py | 7 +++---- service.py | 10 +++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/flask_util.py b/flask_util.py index faa0e29..3c4ec40 100644 --- a/flask_util.py +++ b/flask_util.py @@ -18,11 +18,10 @@ def flask_response(response_string): :param response_string: :return: a flask.Response object """ - data = {ServiceConstants.API_NAME_KEY.value: ServiceConstants.API_NAME.value, - ServiceConstants.VERSION_KEY.value: ServiceConstants.VERSION.value, - ServiceConstants.RESPONSE_KEY.value: response_string.value} + data = {ServiceConstants.API_NAME_KEY: ServiceConstants.API_NAME, + ServiceConstants.VERSION_KEY: ServiceConstants.VERSION, + ServiceConstants.RESPONSE_KEY: response_string} - print('flask_response data: {}'.format(data)) json = jsonify(data) return json diff --git a/service.py b/service.py index 3e921c7..5646634 100644 --- a/service.py +++ b/service.py @@ -22,7 +22,7 @@ def route(command): :param command: a RemoteCommand :return: route string """ - return "/api/v1/{}/{}/".format(flask_util.ServiceConstants.API_NAME.value, command.value) + return "/api/v1/{}/{}/".format(flask_util.ServiceConstants.API_NAME, command.value) def transmit_command(command): @@ -45,11 +45,11 @@ def transmit_command(command): # / is the website root, the entry point -# http://10.0.0.4:5000 +# http://127.0.0.1:5000 # home http://127.0.0.1 # port :5000 @app.route('/') -@app.route("/api/v1/{}/ping/".format(flask_util.ServiceConstants.API_NAME.value), methods=['GET']) +@app.route("/api/v1/{}/ping/".format(flask_util.ServiceConstants.API_NAME), methods=['GET']) def api_status(): if request.method == 'GET': return flask_util.flask_response('pong') @@ -87,8 +87,8 @@ def volume_increase(): return transmit_command(RemoteCommand.VOLUME_INCREASE) -@app.route("/api/v1/{}/volume-decrease-increase/".format(flask_util.ServiceConstants.API_NAME.value), methods=['POST']) -def volume_decrease_increase(): +@app.route("/api/v1/{}/volume-decrease-increase/".format(flask_util.ServiceConstants.API_NAME), methods=['POST']) +def volume_duck(): return scheduler.Scheduler.volume_decrease_increase(decrease_count=4, increase_count=3, duration_seconds=10) From 1dae75eaed082845772e37f4a64367bed23ec7bf Mon Sep 17 00:00:00 2001 From: beepscore Date: Tue, 23 Apr 2019 23:47:00 -0700 Subject: [PATCH 7/7] Still trying to fix broken routes. Remove ServiceConstants enum. --- flask_util.py | 19 ++++++++----------- service.py | 8 ++++---- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/flask_util.py b/flask_util.py index 3c4ec40..c3df185 100644 --- a/flask_util.py +++ b/flask_util.py @@ -1,15 +1,12 @@ #!/usr/bin/env/python3 -from enum import Enum from flask import jsonify - -class ServiceConstants(Enum): - API_NAME = 'tv' - API_NAME_KEY = 'api_name' - RESPONSE_KEY = 'response' - VERSION = '1.0' - VERSION_KEY = 'version' +API_NAME = 'tv' +API_NAME_KEY = 'api_name' +RESPONSE_KEY = 'response' +VERSION = '1.0' +VERSION_KEY = 'version' def flask_response(response_string): @@ -18,9 +15,9 @@ def flask_response(response_string): :param response_string: :return: a flask.Response object """ - data = {ServiceConstants.API_NAME_KEY: ServiceConstants.API_NAME, - ServiceConstants.VERSION_KEY: ServiceConstants.VERSION, - ServiceConstants.RESPONSE_KEY: response_string} + data = {API_NAME_KEY: API_NAME, + VERSION_KEY: VERSION, + RESPONSE_KEY: response_string} json = jsonify(data) return json diff --git a/service.py b/service.py index 5646634..d979fb5 100644 --- a/service.py +++ b/service.py @@ -22,7 +22,7 @@ def route(command): :param command: a RemoteCommand :return: route string """ - return "/api/v1/{}/{}/".format(flask_util.ServiceConstants.API_NAME, command.value) + return "/api/v1/{}/{}/".format(flask_util.API_NAME, command.value) def transmit_command(command): @@ -45,11 +45,11 @@ def transmit_command(command): # / is the website root, the entry point -# http://127.0.0.1:5000 +# http://10.0.0.4:5000 # home http://127.0.0.1 # port :5000 @app.route('/') -@app.route("/api/v1/{}/ping/".format(flask_util.ServiceConstants.API_NAME), methods=['GET']) +@app.route("/api/v1/{}/ping/".format(flask_util.API_NAME), methods=['GET']) def api_status(): if request.method == 'GET': return flask_util.flask_response('pong') @@ -87,7 +87,7 @@ def volume_increase(): return transmit_command(RemoteCommand.VOLUME_INCREASE) -@app.route("/api/v1/{}/volume-decrease-increase/".format(flask_util.ServiceConstants.API_NAME), methods=['POST']) +@app.route("/api/v1/{}/volume-decrease-increase/".format(flask_util.API_NAME), methods=['POST']) def volume_duck(): return scheduler.Scheduler.volume_decrease_increase(decrease_count=4, increase_count=3, duration_seconds=10)