51 lines
No EOL
1.8 KiB
Python
51 lines
No EOL
1.8 KiB
Python
"""
|
|
Search resource for the OpenEventDatabase.
|
|
"""
|
|
|
|
import json
|
|
import falcon
|
|
from oedb.models.event import BaseEvent
|
|
from oedb.utils.logging import logger
|
|
|
|
class EventSearch(BaseEvent):
|
|
"""
|
|
Resource for searching events by geometry.
|
|
Handles the /event/search endpoint.
|
|
"""
|
|
|
|
def on_post(self, req, resp):
|
|
"""
|
|
Handle POST requests to the /event/search endpoint.
|
|
Expects a GeoJSON Feature in the request body.
|
|
|
|
Args:
|
|
req: The request object.
|
|
resp: The response object.
|
|
"""
|
|
logger.info("Processing POST request to /event/search")
|
|
|
|
try:
|
|
# Body should contain a GeoJSON Feature
|
|
body = req.stream.read().decode('utf-8')
|
|
j = json.loads(body)
|
|
|
|
# Validate the request body
|
|
if 'geometry' not in j:
|
|
logger.warning("Request body missing 'geometry' field")
|
|
resp.status = falcon.HTTP_400
|
|
resp.body = json.dumps({"error": "Request body must contain a 'geometry' field"})
|
|
return
|
|
|
|
# Pass the query with the geometry to event.on_get
|
|
# This will be handled by the main application
|
|
from oedb.resources.event import event
|
|
event.on_get(req, resp, None, j['geometry'])
|
|
logger.success("Successfully processed POST request to /event/search")
|
|
except json.JSONDecodeError as e:
|
|
logger.error(f"Error decoding JSON: {e}")
|
|
resp.status = falcon.HTTP_400
|
|
resp.body = json.dumps({"error": "Invalid JSON in request body"})
|
|
except Exception as e:
|
|
logger.error(f"Error processing POST request to /event/search: {e}")
|
|
resp.status = falcon.HTTP_500
|
|
resp.body = json.dumps({"error": str(e)}) |