Compare commits
2 Commits
ad087b9547
...
686e4ff477
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
686e4ff477 | ||
|
|
c7b3f95439 |
63
app.py
63
app.py
@@ -8,6 +8,9 @@ from ingress import IngressAPI
|
||||
from models import EventType, Plext
|
||||
from pymongo import MongoClient
|
||||
from pymongo.errors import PyMongoError
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Timezone configuration
|
||||
TIMEZONE = ZoneInfo("Europe/Rome")
|
||||
@@ -49,6 +52,7 @@ def basic_auth_required(f):
|
||||
Returns:
|
||||
401 Unauthorized if authentication fails or is missing
|
||||
"""
|
||||
|
||||
@wraps(f)
|
||||
def decorated(*args, **kwargs):
|
||||
auth = request.authorization
|
||||
@@ -58,7 +62,7 @@ def basic_auth_required(f):
|
||||
"Could not verify your access level for that URL.\n"
|
||||
"You have to login with proper credentials",
|
||||
401,
|
||||
{"WWW-Authenticate": 'Basic realm="Login Required"'}
|
||||
{"WWW-Authenticate": 'Basic realm="Login Required"'},
|
||||
)
|
||||
|
||||
return f(*args, **kwargs)
|
||||
@@ -150,19 +154,22 @@ def plext_to_dict(plext: Plext) -> dict:
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@app.route("/plexts/from-db", methods=["GET", "OPTIONS"])
|
||||
@basic_auth_required
|
||||
def get_plexts_from_db():
|
||||
"""
|
||||
Get plexts from MongoDB with optional filters.
|
||||
Returns plexts sorted by timestamp (most recent first).
|
||||
|
||||
Query Parameters:
|
||||
player_name: Filter by player name (optional)
|
||||
timestamp_from: Minimum timestamp in milliseconds (optional)
|
||||
timestamp_to: Maximum timestamp in milliseconds (optional)
|
||||
limit: Maximum number of results to return (optional, default: 100)
|
||||
|
||||
Returns:
|
||||
JSON response with list of plexts (without _id field)
|
||||
JSON response with list of plexts (without _id field), limited by the limit parameter
|
||||
"""
|
||||
# CORS headers
|
||||
cors_headers = {
|
||||
@@ -180,19 +187,47 @@ def get_plexts_from_db():
|
||||
player_name = request.args.get("player_name")
|
||||
timestamp_from = request.args.get("timestamp_from")
|
||||
timestamp_to = request.args.get("timestamp_to")
|
||||
limit_param = request.args.get("limit")
|
||||
|
||||
# Validate and convert timestamp parameters to integers if provided
|
||||
if timestamp_from is not None:
|
||||
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,
|
||||
cors_headers,
|
||||
)
|
||||
|
||||
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,
|
||||
cors_headers,
|
||||
)
|
||||
|
||||
# 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,
|
||||
)
|
||||
except ValueError:
|
||||
return (
|
||||
jsonify({"error": "limit must be an integer"}),
|
||||
400,
|
||||
cors_headers,
|
||||
)
|
||||
else:
|
||||
limit = 100
|
||||
|
||||
# Build MongoDB filter query
|
||||
filter_query = {}
|
||||
@@ -221,19 +256,17 @@ def get_plexts_from_db():
|
||||
# Projection to exclude _id field
|
||||
projection = {"_id": 0}
|
||||
|
||||
# Execute query with sorting (timestamp DESC - most recent first)
|
||||
cursor = collection.find(
|
||||
filter=filter_query,
|
||||
projection=projection
|
||||
).sort("timestamp", -1)
|
||||
# Execute query with sorting (timestamp DESC - most recent first) and dynamic limit
|
||||
cursor = (
|
||||
collection.find(filter=filter_query, projection=projection)
|
||||
.sort("timestamp", -1)
|
||||
.limit(limit)
|
||||
)
|
||||
|
||||
# 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, cors_headers
|
||||
|
||||
except PyMongoError as e:
|
||||
logger.error(f"MongoDB error: {e}")
|
||||
@@ -245,6 +278,7 @@ def get_plexts_from_db():
|
||||
logger.exception("Unexpected error in get_plexts_from_db")
|
||||
return jsonify({"error": "An error occurred"}), 500, cors_headers
|
||||
|
||||
|
||||
@app.route("/plexts/from-api", methods=["GET"])
|
||||
@basic_auth_required
|
||||
def get_plexts_from_api():
|
||||
@@ -359,11 +393,12 @@ def index():
|
||||
},
|
||||
"/plexts/from-db": {
|
||||
"method": "GET",
|
||||
"description": "Get plexts from MongoDB with optional filters",
|
||||
"description": "Get plexts from MongoDB with optional filters. Returns plexts sorted by timestamp (most recent first).",
|
||||
"parameters": {
|
||||
"player_name": "Filter by player name (optional)",
|
||||
"timestamp_from": "Minimum timestamp in milliseconds (optional)",
|
||||
"timestamp_to": "Maximum timestamp in milliseconds (optional)",
|
||||
"limit": "Maximum number of results to return (optional, default: 100)",
|
||||
},
|
||||
"authentication": "Basic Auth required",
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user