diff --git a/flask_util.py b/flask_util.py new file mode 100644 index 0000000..c3df185 --- /dev/null +++ b/flask_util.py @@ -0,0 +1,24 @@ +#!/usr/bin/env/python3 + +from flask import jsonify + +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 = {API_NAME_KEY: API_NAME, + VERSION_KEY: VERSION, + RESPONSE_KEY: response_string} + + json = jsonify(data) + return json + 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__': diff --git a/service.py b/service.py index 455247a..d979fb5 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.API_NAME, command.value) def transmit_command(command): @@ -44,29 +38,21 @@ 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 -# 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(API_NAME), methods=['GET']) +@app.route("/api/v1/{}/ping/".format(flask_util.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 @@ -101,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.API_NAME), methods=['POST']) def volume_duck(): return scheduler.Scheduler.volume_decrease_increase(decrease_count=4, increase_count=3, duration_seconds=10)