use flask cors
This commit is contained in:
45
app.py
45
app.py
@@ -4,6 +4,7 @@ from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
from functools import wraps
|
||||
from flask import Flask, request, jsonify, Response
|
||||
from flask_cors import CORS
|
||||
from ingress import IngressAPI
|
||||
from models import EventType, Plext
|
||||
from pymongo import MongoClient
|
||||
@@ -22,6 +23,7 @@ logging.basicConfig(
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
|
||||
def check_basic_auth(username: str, password: str) -> bool:
|
||||
@@ -155,7 +157,7 @@ def plext_to_dict(plext: Plext) -> dict:
|
||||
}
|
||||
|
||||
|
||||
@app.route("/plexts/from-db", methods=["GET", "OPTIONS"])
|
||||
@app.route("/plexts/from-db", methods=["GET"])
|
||||
@basic_auth_required
|
||||
def get_plexts_from_db():
|
||||
"""
|
||||
@@ -171,17 +173,6 @@ def get_plexts_from_db():
|
||||
Returns:
|
||||
JSON response with list of plexts (without _id field), limited by the limit parameter
|
||||
"""
|
||||
# CORS headers
|
||||
cors_headers = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
||||
}
|
||||
|
||||
# Handle OPTIONS preflight request
|
||||
if request.method == "OPTIONS":
|
||||
return "", 200, cors_headers
|
||||
|
||||
try:
|
||||
# Parse query parameters
|
||||
player_name = request.args.get("player_name")
|
||||
@@ -194,38 +185,22 @@ def get_plexts_from_db():
|
||||
try:
|
||||
timestamp_from = int(timestamp_from)
|
||||
except ValueError:
|
||||
return (
|
||||
jsonify({"error": "timestamp_from must be an integer"}),
|
||||
400,
|
||||
cors_headers,
|
||||
)
|
||||
return jsonify({"error": "timestamp_from must be an integer"}), 400
|
||||
|
||||
if timestamp_to is not None:
|
||||
try:
|
||||
timestamp_to = int(timestamp_to)
|
||||
except ValueError:
|
||||
return (
|
||||
jsonify({"error": "timestamp_to must be an integer"}),
|
||||
400,
|
||||
cors_headers,
|
||||
)
|
||||
return jsonify({"error": "timestamp_to must be an integer"}), 400
|
||||
|
||||
# Validate and convert limit parameter to integer if provided, otherwise default to 100
|
||||
if limit_param is not None:
|
||||
try:
|
||||
limit = int(limit_param)
|
||||
if limit <= 0:
|
||||
return (
|
||||
jsonify({"error": "limit must be a positive integer"}),
|
||||
400,
|
||||
cors_headers,
|
||||
)
|
||||
return jsonify({"error": "limit must be a positive integer"}), 400
|
||||
except ValueError:
|
||||
return (
|
||||
jsonify({"error": "limit must be an integer"}),
|
||||
400,
|
||||
cors_headers,
|
||||
)
|
||||
return jsonify({"error": "limit must be an integer"}), 400
|
||||
else:
|
||||
limit = 100
|
||||
|
||||
@@ -266,17 +241,17 @@ def get_plexts_from_db():
|
||||
# Convert cursor to list
|
||||
plexts = list(cursor)
|
||||
|
||||
return jsonify({"count": len(plexts), "plexts": plexts}), 200, cors_headers
|
||||
return jsonify({"count": len(plexts), "plexts": plexts}), 200
|
||||
|
||||
except PyMongoError as e:
|
||||
logger.error(f"MongoDB error: {e}")
|
||||
return jsonify({"error": "Database error"}), 500, cors_headers
|
||||
return jsonify({"error": "Database error"}), 500
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
except Exception as e:
|
||||
logger.exception("Unexpected error in get_plexts_from_db")
|
||||
return jsonify({"error": "An error occurred"}), 500, cors_headers
|
||||
return jsonify({"error": "An error occurred"}), 500
|
||||
|
||||
|
||||
@app.route("/plexts/from-api", methods=["GET"])
|
||||
|
||||
Reference in New Issue
Block a user