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

Implement endpoint function #2

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions src/mpclookup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ class Config(BaseSettings):

name: str = Field("mpc-lookup", title="Name of application")

path_prefix: str = Field(
"/mpc-lookup", title="URL prefix for application"
)
path_prefix: str = Field("/mpc-lookup", title="URL prefix for application")

profile: Profile = Field(
Profile.development, title="Application logging profile"
Expand Down
31 changes: 30 additions & 1 deletion src/mpclookup/handlers/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from typing import Annotated

from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, Query
from fastapi.responses import RedirectResponse
from safir.dependencies.logger import logger_dependency
from safir.metadata import get_metadata
from structlog.stdlib import BoundLogger
Expand Down Expand Up @@ -50,3 +51,31 @@ async def get_index(
application_name=config.name,
)
return Index(metadata=metadata)


_DESIGNATION_PREPEND = "2011 "


@external_router.get("/search", response_class=RedirectResponse)
async def search(
designation: Annotated[str, Query()],
logger: Annotated[BoundLogger, Depends(logger_dependency)],
) -> str:
"""
Request a redirect to the MPCORB record for a given designation.

Notes
-----
Example request:

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

"""
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)
return redirect_url