netclock/countdown/api.py

50 lines
1.2 KiB
Python
Raw Normal View History

2020-09-12 10:58:38 +00:00
from time import time
2020-10-26 10:07:42 +00:00
from flask import request
2020-09-12 10:58:38 +00:00
2020-09-12 17:51:47 +00:00
from . import app
2020-09-13 13:07:11 +00:00
from .countdown import Cache
2020-09-12 10:58:38 +00:00
2020-10-26 10:07:42 +00:00
2020-09-12 17:51:47 +00:00
@app.route('/api/v1/<uuid:id>', methods=['GET'])
2020-09-12 10:58:38 +00:00
def get_countdown(id):
2020-10-26 10:07:42 +00:00
cache = Cache.get_instance()
2020-09-13 13:07:11 +00:00
countdown = cache.get_countdown(id)
response = countdown
2020-09-12 10:58:38 +00:00
2020-09-13 13:07:11 +00:00
time_passed = time() - float(countdown['start'])
time_left = float(countdown['total']) - time_passed
response['left'] = time_left
response['roundtrip_start'] = request.args.get('roundtrip_start')
2020-09-12 10:58:38 +00:00
2020-09-13 13:07:11 +00:00
return response
2020-09-12 10:58:38 +00:00
2020-10-26 10:07:42 +00:00
2020-09-12 17:51:47 +00:00
@app.route('/api/v1', methods=['POST'])
2020-09-12 10:58:38 +00:00
def create_countdown():
2020-10-26 10:07:42 +00:00
cache = Cache.get_instance()
2020-09-12 10:58:38 +00:00
2020-09-13 13:07:11 +00:00
countdown = request.json
total = float(countdown['total'])
response = cache.add_countdown(total)
return response
2020-10-26 10:07:42 +00:00
2020-09-13 13:07:11 +00:00
@app.route('/api/v1/start/<uuid:id>', methods=['PATCH'])
def start_countdown(id):
2020-10-26 10:07:42 +00:00
cache = Cache.get_instance()
2020-09-13 13:07:11 +00:00
return cache.start_countdown(id)
2020-10-26 10:07:42 +00:00
2020-09-13 13:07:11 +00:00
@app.route('/api/v1/reset/<uuid:id>', methods=['PATCH'])
def reset_countdown(id):
2020-10-26 10:07:42 +00:00
cache = Cache.get_instance()
2020-09-13 13:07:11 +00:00
return cache.reset_countdown(id)
2020-10-26 10:07:42 +00:00
2020-09-13 13:07:11 +00:00
@app.route('/api/v1/stop/<uuid:id>', methods=['PATCH'])
def stop_countdown(id):
2020-10-26 10:07:42 +00:00
cache = Cache.get_instance()
2020-09-13 13:07:11 +00:00
return cache.stop_countdown(id)