From 42b86a028672a79ad803d5194d33833558aa3827 Mon Sep 17 00:00:00 2001 From: Claudio Mansilla Date: Fri, 22 Nov 2024 16:53:38 -0300 Subject: [PATCH] deleted try-except in get_crossmatch_data and put it in htmx. also changed function name in htmx --- lightcurve/src/crossmatch_api/get_crossmatch_data.py | 11 ++++------- lightcurve/src/crossmatch_api/routes/htmx.py | 12 ++++++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lightcurve/src/crossmatch_api/get_crossmatch_data.py b/lightcurve/src/crossmatch_api/get_crossmatch_data.py index 3991bee60..ddaf0680e 100644 --- a/lightcurve/src/crossmatch_api/get_crossmatch_data.py +++ b/lightcurve/src/crossmatch_api/get_crossmatch_data.py @@ -26,10 +26,7 @@ def get_alerce_data(ra, dec, radius): "radius": radius } - try: - # Make the GET request - response = requests.get(base_url, params=params) - return response.json() # Return the JSON response - - except requests.RequestException as e: - return f"Error: {str(e)}" + # Make the GET request + response = requests.get(base_url, params=params) + return response.json() # Return the JSON response + \ No newline at end of file diff --git a/lightcurve/src/crossmatch_api/routes/htmx.py b/lightcurve/src/crossmatch_api/routes/htmx.py index e20e69257..2a5b847a1 100644 --- a/lightcurve/src/crossmatch_api/routes/htmx.py +++ b/lightcurve/src/crossmatch_api/routes/htmx.py @@ -4,11 +4,12 @@ from typing import Annotated from fastapi import Query import json +import requests from core.services.object import get_object from fastapi import APIRouter, Request, HTTPException from fastapi.templating import Jinja2Templates -from fastapi.responses import HTMLResponse +from fastapi.responses import HTMLResponse, HTMLError from ..result_handler import handle_error, handle_success from ..get_crossmatch_data import get_alerce_data router = APIRouter() @@ -20,14 +21,17 @@ ) @router.get("/crossmatch/{oid}", response_class=HTMLResponse) -async def object_mag_app( +async def object_xmatch_app( request: Request, oid: str, ): object = get_object(oid,session_factory = request.app.state.psql_session) - cross = get_alerce_data(object.meanra, object.meandec, 20) + try: + cross = get_alerce_data(object.meanra, object.meandec, 20) + except requests.RequestException as e: + return HTMLError(e) ''' get_alerce_data returns a list with several dictionaries. The dict format is one key and then a value that is another dictionary. @@ -42,7 +46,7 @@ async def object_mag_app( cross_keys = [] for i in range(len(cross)): cross_keys.append(next(iter(cross[i].keys()))) - + return templates.TemplateResponse( name='crossmatch.html.jinja', context={'request': request,