-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend/voe.py): adding create voe endpoint
- Loading branch information
Showing
3 changed files
with
62 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |