tcc/FlaskAPI/controllers/simulation.py

42 lines
1.0 KiB
Python

from flask import request, Blueprint, jsonify
import person_pb2
simulation_blueprint = Blueprint('simulation_blueprint', __name__)
class SimulationController:
def __init__(self):
pass
def harmonic_progression(self, n):
sum = 0
for i in range(1, n):
sum += 1/i
return sum
def return_helloworld(self):
return jsonify(message="Hello World!")
simulation_controller = SimulationController()
@simulation_blueprint.route('/simulation/harmonic-progression', methods=['GET'])
def return_ok():
n = int(request.args.get('n'))
sum = simulation_controller.harmonic_progression(n)
return str(sum), 200
@simulation_blueprint.route('/simulation/json', methods=['POST'])
def return_helloworld():
data = request.json
return data, 200
@simulation_blueprint.route('/simulation/protobuf', methods=['POST'])
def return_protobuf():
bytes_data = request.data
helloworld = person_pb2.Person()
helloworld.ParseFromString(bytes_data)
return helloworld.SerializeToString(), 200