From ab4065816721f2a3694d4295a014079116d0c3a1 Mon Sep 17 00:00:00 2001 From: Jeremy McCormick Date: Wed, 29 May 2024 17:07:57 -0500 Subject: [PATCH] Add redirect to another endpoint if designation is a synthetic object If the object has a space in it, then redirect to another API endpoint which indicates that it is synthetic. --- src/mpclookup/handlers/external.py | 43 ++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/mpclookup/handlers/external.py b/src/mpclookup/handlers/external.py index 28eaab8..cd7020c 100644 --- a/src/mpclookup/handlers/external.py +++ b/src/mpclookup/handlers/external.py @@ -1,9 +1,10 @@ """Handlers for the app's external root, ``/mpc-lookup/``.""" from typing import Annotated +from urllib.parse import urlparse -from fastapi import APIRouter, Depends, Query -from fastapi.responses import RedirectResponse +from fastapi import APIRouter, Depends, Query, Request +from fastapi.responses import PlainTextResponse, RedirectResponse from safir.dependencies.logger import logger_dependency from safir.metadata import get_metadata from structlog.stdlib import BoundLogger @@ -60,6 +61,7 @@ async def get_index( async def search( designation: Annotated[str, Query()], logger: Annotated[BoundLogger, Depends(logger_dependency)], + request: Request, ) -> str: """ Request a redirect to the MPCORB record for a given designation. @@ -68,14 +70,39 @@ async def search( ----- Example request: - /mpc-lookup/search?designation=2011+1001+T-2 + `/mpc-lookup/search?designation=2011+1001+T-2` + In this case, the "2011 " prefix, which is present in the DP0.3 data, will + be stripped out automatically so that the designation is valid. + + Any designations containing spaces will be redirected to an endpoint that + will return a text response indicating that the object is synthetic. """ logger.info("Request for designation URL", designation=designation) fd = designation.replace(_DESIGNATION_PREPEND, "") - redirect_url = ( - "https://www.minorplanetcenter.net/db_search/" - f"show_object?object_id={fd}" - ) - logger.info("Redirecting to designation URL", redirect_url=redirect_url) + if " " not in fd: + redirect_url = ( + "https://www.minorplanetcenter.net/db_search/" + f"show_object?object_id={fd}" + ) + logger.info("Redirecting to MPC URL", redirect_url=redirect_url) + else: + redirect_url = ( + urlparse(str(request.url)).path.rsplit("/", 1)[0] + + "/synthetic_object?designation=" + + str(designation) + ) + logger.info( + "Redirecting synthetic object to", redirect_url=redirect_url + ) return redirect_url + + +@external_router.get("/synthetic_object", response_class=PlainTextResponse) +async def get_synthetic_object( + designation: Annotated[str, Query()], +) -> str: + return ( + f"{designation} " + "appears to be a synthetic object from the DP0.3 input simulation." + )