netclock/countdown/api.py

32 lines
677 B
Python
Raw Normal View History

2020-09-12 10:58:38 +00:00
from flask import request, jsonify
from walrus import Walrus
from time import time
import uuid
import struct
2020-09-12 17:51:47 +00:00
from . import app
2020-09-12 10:58:38 +00:00
db = Walrus(host='localhost', port=6379, db=0)
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):
ct = db.Hash(str(id))
resp = ct.as_dict(decode=True)
resp['left'] = float(ct['total']) - (time() - float(ct['start']))
return resp
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():
countdown = request.json
ct_id = str(uuid.uuid4())
ct = db.Hash(ct_id)
ct.update(start=time(), total=countdown['total'])
resp = ct.as_dict(decode=True)
resp['id'] = ct_id
return resp