tcc/FlaskAPI/controllers/simulation.py

42 lines
1.0 KiB
Python
Raw Normal View History

2023-10-29 22:01:13 +00:00
from flask import request, Blueprint, jsonify
2023-11-04 20:05:55 +00:00
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
2023-10-29 22:01:13 +00:00
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
2023-10-29 22:01:13 +00:00
2023-11-04 20:05:55 +00:00
@simulation_blueprint.route('/simulation/json', methods=['POST'])
2023-10-30 23:34:20 +00:00
def return_helloworld():
2023-11-04 20:05:55 +00:00
data = request.json
return data, 200
2023-11-04 03:41:12 +00:00
@simulation_blueprint.route('/simulation/protobuf', methods=['POST'])
def return_protobuf():
bytes_data = request.data
2023-11-04 20:05:55 +00:00
helloworld = person_pb2.Person()
2023-11-04 03:41:12 +00:00
helloworld.ParseFromString(bytes_data)
return helloworld.SerializeToString(), 200