Skip to content

Commit

Permalink
feat(backend/voe.py): adding create voe endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
odarotto committed May 3, 2024
1 parent 620ce77 commit 488c000
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
52 changes: 48 additions & 4 deletions frbvoe/backend/voe.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,52 @@
"""VOEvent Server Blueprint."""

# Post at /voe
from sanic import Blueprint
from sanic.log import logger
from sanic.request import Request
from sanic.response import HTTPResponse
from sanic.response import json as json_response
from sanic_ext import openapi
from pymongo.errors import PyMongoError

from frbvoe.models.voe import VOEvent
from frbvoe.utilities.senders import send_to_comet

voe = Blueprint("voe", url_prefix="/")


# Post at /voe
@voe.post("voe")
@openapi.response(201, description="Validates data from the telescope.")
# Add the validated payload to the MongoDB Database
# Database Name: frbvoe
# Collection Name: voe
# Document -> VOEvent Payload Dict.
async def create_voe(request: Request, voe_event: VOEvent):

mongo = request.app.ctx.mongo
request.app.add_task(send_to_comet(voe_event.model_dump()))
# Database Name: frbvoe
# Collection Name: voe
# Document -> VOEvent Payload Dict.
try:
insert_result = await mongo["frbvoe"]["voe"].insert_one(voe_event.model_dump())
except (Exception, PyMongoError) as mongo_error:
logger.error(f"{mongo_error} on /voe")
return json_response({"message": mongo_error}, status=500)

return json_response({"id": insert_result.inserted_id}, status=201)


async def delete_voe(request: Request):

# /voe?id=<id>
if "id" not in request.args.keys():
return json_response(
{"message": "Your request needs an ID."}, status=400
)
id = request.args["id"]
mongo = request.app.ctx.mongo
try:
delete_result = await mongo["frbvoe"]["voe"].delete_one({"id": id})
except (Exception, PyMongoError) as mongo_error:
logger.error(f"{mongo_error} on /voe")
return json_response({"message": mongo_error}, status=500)

return json_response({"message": delete_result.delete_count}, status=202)
7 changes: 6 additions & 1 deletion frbvoe/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sanic.worker.loader import AppLoader

from frbvoe.models.voe import VOEvent
from frbvoe.backend.voe import voe as voe_blueprint


async def mongo(app: Sanic, loop: AbstractEventLoop) -> None:
Expand Down Expand Up @@ -70,7 +71,11 @@ def create(name: str = "frbvoe", debug: bool = False) -> Sanic:
app.config.HEALTH = True
app.config.HEALTH_ENDPOINT = True
app.ctx.debug = debug
app.config.FALLBACK_ERROR_FORMAT = "json"
app.config.FALL
BACK_ERROR_FORMAT = "json"
# ? Blueprints
app.blueprint(voe_blueprint)
# ? Listeners
app.register_listener(mongo, "before_server_start")
return app

Expand Down
8 changes: 8 additions & 0 deletions frbvoe/utilities/senders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Function to send request to Comet."""

import requests

async def send_to_comet(payload) -> bool:
response = requests.post("http://comet:8098/", json=payload)
# ? Maybe you'll need to change this
return response.status_code == 200

0 comments on commit 488c000

Please sign in to comment.