Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redirect to another endpoint if designation is a synthetic object #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions src/mpclookup/handlers/external.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Handlers for the app's external root, ``/mpc-lookup/``."""

from typing import Annotated
from urllib.parse import urlencode, urlparse

from fastapi import APIRouter, Depends, Query
from fastapi.responses import RedirectResponse
from fastapi import APIRouter, Depends, Query, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from safir.dependencies.logger import logger_dependency
from safir.metadata import get_metadata
from structlog.stdlib import BoundLogger
Expand Down Expand Up @@ -60,22 +61,55 @@ 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.
Request a redirect to the MPCORB database record for a given designation.

Notes
-----
Example request:
An example request could be:

/mpc-lookup/search?designation=2011+1001+T-2
`/mpc-lookup/search?designation=2011+1001+T-2`

The "2011 " prefix, which is present in the DP0.3 data, will be
automatically stripped out so that the designation has a valid format.

A designation without any spaces will cause a redirect to an endpoint
which returns a message indicating that the object appears to be 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}"
if " " in fd:
redirect_url = urlparse(
"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_for("get_synthetic_object"))
+ "?"
+ urlencode({"designation": designation})
)
logger.info(
"Redirecting synthetic object to", redirect_url=redirect_url
)
return redirect_url.geturl()


@external_router.get("/synthetic_object", response_class=HTMLResponse)
async def get_synthetic_object(
designation: Annotated[str, Query()],
) -> HTMLResponse:
return HTMLResponse(
content=f"""
<html>
<body>
<p>{designation} appears to be a synthetic object
from the DP0.3 input simulation.</p>
</body>
</html>
""",
status_code=200,
)
logger.info("Redirecting to designation URL", redirect_url=redirect_url)
return redirect_url