Skip to content

Commit

Permalink
style(examples): more pythonic code-style and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vvagaytsev committed Jun 14, 2022
1 parent 33e2b98 commit 199d8cc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 82 deletions.
86 changes: 45 additions & 41 deletions examples/vote-helm/api-image/app.py
Original file line number Diff line number Diff line change
@@ -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)
86 changes: 45 additions & 41 deletions examples/vote/api/app.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 199d8cc

Please sign in to comment.