handle CORS header, only basic implementation for get.

This commit is contained in:
Deadswitch
2021-04-13 19:10:03 -05:00
parent 1a05b85d19
commit f2dcc1c316

16
api1.py
View File

@@ -1,6 +1,6 @@
import flask
import sys
from flask import request, jsonify, g
from flask import request, jsonify, g, make_response
from flask_apscheduler import APScheduler
from datetime import datetime,timedelta,timezone
import sqlite3
@@ -88,6 +88,14 @@ if __name__ == '__main__':
</ul>"""
@app.route('/api/v1', methods=['OPTIONS'])
def api_returnOptions():
response = make_response()
response.headers.add("Access-Control-Allow-Origin", "*")
response.headers.add('Access-Control-Allow-Headers', "*")
response.headers.add('Access-Control-Allow-Methods', "*")
return response
@app.route('/api/v1', methods=['GET'])
def api_getByTopic():
if 'topic' in request.args:
@@ -101,7 +109,7 @@ if __name__ == '__main__':
if theRow is None:
return jsonify({'success':False, 'error':f"No entry found for topic _{topic}_"})
return jsonify({'success':True, 'topic':topic, 'active':bool(theRow[0]), 'expiry':theRow[1].strftime("%Y-%m-%d %H:%M:%S"), 'lastSeen':theRow[5].strftime("%Y-%m-%d %H:%M:%S"),'isExpired':bool(theRow[4]),'email':theRow[2], 'isNotified':bool(theRow[3])})
return _corsify_actual_response(jsonify({'success':True, 'topic':topic, 'active':bool(theRow[0]), 'expiry':theRow[1].strftime("%Y-%m-%d %H:%M:%S"), 'lastSeen':theRow[5].strftime("%Y-%m-%d %H:%M:%S"),'isExpired':bool(theRow[4]),'email':theRow[2], 'isNotified':bool(theRow[3])}))
@app.route('/api/v1', methods=['PUT','POST'])
def api_setRecord():
@@ -132,4 +140,8 @@ if __name__ == '__main__':
return jsonify({'success':True, 'expiry':expiryDateTime.strftime("%Y-%m-%d %H:%M:%S"), 'email':email})
def _corsify_actual_response(response):
response.headers.add("Access-Control-Allow-Origin", "*")
return response
app.run(use_reloader=False,port=5123)