From 199d8cc28d49ca7070c0981c54f30706eab0578e Mon Sep 17 00:00:00 2001 From: Vladimir Vagaytsev Date: Tue, 14 Jun 2022 11:24:13 +0200 Subject: [PATCH] style(examples): more pythonic code-style and formatting --- examples/vote-helm/api-image/app.py | 86 +++++++++++++++-------------- examples/vote/api/app.py | 86 +++++++++++++++-------------- 2 files changed, 90 insertions(+), 82 deletions(-) diff --git a/examples/vote-helm/api-image/app.py b/examples/vote-helm/api-image/app.py index 6016b2a8f7..fe9477d298 100644 --- a/examples/vote-helm/api-image/app.py +++ b/examples/vote-helm/api-image/app.py @@ -1,57 +1,61 @@ -from flask import Flask, render_template, request, make_response, g -from flask_cors import CORS -from redis import Redis +import json import os -import socket import random -import json +import socket + +from flask import Flask, request, g +from flask_cors import CORS +from redis import Redis REDIS_HOST = 'redis-master' -option_a = os.getenv('OPTION_A', "Cats") -option_b = os.getenv('OPTION_B', "Dogs") +option_a = os.getenv('OPTION_A', 'Cats') +option_b = os.getenv('OPTION_B', 'Dogs') hostname = socket.gethostname() app = Flask(__name__) CORS(app) + def get_redis(): - if not hasattr(g, 'redis'): - g.redis = Redis(host=REDIS_HOST, db=0, socket_timeout=5) - return g.redis + if not hasattr(g, 'redis'): + g.redis = Redis(host=REDIS_HOST, db=0, socket_timeout=5) + return g.redis + -@app.route("/api", methods=['GET']) +@app.route('/api', methods=['GET']) def hello(): + return app.response_class( + response='Hello, I am the api service', + status=200, + ) + + +@app.route('/api/vote', methods=['POST', 'GET']) +def vote(): + app.logger.info('Received request') + + voter_id = hex(random.getrandbits(64))[2:-1] + + if request.method == 'POST': + redis = get_redis() + vote = request.form['vote'] + data = json.dumps({'voter_id': voter_id, 'vote': vote}) + + redis.rpush('votes', data) + app.logger.info('Registered vote') + return app.response_class( + response=json.dumps(data), + status=200, + mimetype='application/json' + ) + else: return app.response_class( - response="Hello, I am the api service", - status=200, + response=json.dumps({}), + status=404, + mimetype='application/json' ) -@app.route("/api/vote", methods=['POST','GET']) -def vote(): - app.logger.info("received request") - - voter_id = hex(random.getrandbits(64))[2:-1] - vote = None - - if request.method == 'POST': - redis = get_redis() - vote = request.form['vote'] - data = json.dumps({'voter_id': voter_id, 'vote': vote}) - - redis.rpush('votes', data) - print("Registered vote") - return app.response_class( - response=json.dumps(data), - status=200, - mimetype='application/json' - ) - else: - return app.response_class( - response=json.dumps({}), - status=404, - mimetype='application/json' - ) - -if __name__ == "__main__": - app.run(host='0.0.0.0', port=80, debug=True, threaded=True) + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=80, debug=True, threaded=True) diff --git a/examples/vote/api/app.py b/examples/vote/api/app.py index 8f5f8d8c76..935c7eff62 100644 --- a/examples/vote/api/app.py +++ b/examples/vote/api/app.py @@ -1,55 +1,59 @@ -from flask import Flask, render_template, request, make_response, g -from flask_cors import CORS -from redis import Redis +import json import os -import socket import random -import json +import socket -option_a = os.getenv('OPTION_A', "Cats") -option_b = os.getenv('OPTION_B', "Dogs") +from flask import Flask, request, g +from flask_cors import CORS +from redis import Redis + +option_a = os.getenv('OPTION_A', 'Cats') +option_b = os.getenv('OPTION_B', 'Dogs') hostname = socket.gethostname() app = Flask(__name__) CORS(app) + def get_redis(): - if not hasattr(g, 'redis'): - g.redis = Redis(host="redis", db=0, socket_timeout=5) - return g.redis + if not hasattr(g, 'redis'): + g.redis = Redis(host='redis', db=0, socket_timeout=5) + return g.redis + -@app.route("/api", methods=['GET']) +@app.route('/api', methods=['GET']) def hello(): + return app.response_class( + response='Hello, I am the api service', + status=200, + ) + + +@app.route('/api/vote', methods=['POST', 'GET']) +def vote(): + app.logger.info('Received request') + + voter_id = hex(random.getrandbits(64))[2:-1] + + if request.method == 'POST': + redis = get_redis() + vote = request.form['vote'] + data = json.dumps({'voter_id': voter_id, 'vote': vote}) + + redis.rpush('votes', data) + app.logger.info('Registered vote') + return app.response_class( + response=json.dumps(data), + status=200, + mimetype='application/json' + ) + else: return app.response_class( - response="Hello, I am the api service", - status=200, + response=json.dumps({}), + status=404, + mimetype='application/json' ) -@app.route("/api/vote", methods=['POST','GET']) -def vote(): - app.logger.info("received request") - - voter_id = hex(random.getrandbits(64))[2:-1] - vote = None - - if request.method == 'POST': - redis = get_redis() - vote = request.form['vote'] - data = json.dumps({'voter_id': voter_id, 'vote': vote}) - - redis.rpush('votes', data) - print("Registered vote") - return app.response_class( - response=json.dumps(data), - status=200, - mimetype='application/json' - ) - else: - return app.response_class( - response=json.dumps({}), - status=404, - mimetype='application/json' - ) - -if __name__ == "__main__": - app.run(host='0.0.0.0', port=80, debug=True, threaded=True) + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=80, debug=True, threaded=True)