Skip to content

Commit

Permalink
Use TypeAlias for path parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansick committed Apr 29, 2024
1 parent 86da34a commit cd4ddc6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ ignore = [
"TID252", # if we're going to use relative imports, use them always
"TRY003", # good general advice but lint is way too aggressive
"TRY301", # sometimes raising exceptions inside try is the best flow
"UP040", # type keyword not supported by mypy yet

# The following settings should be disabled when using ruff format
# per https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
Expand Down
22 changes: 10 additions & 12 deletions src/fastapibootcamp/handlers/astroplan/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Path operations for the astroplan router."""

from typing import Annotated
from typing import Annotated, TypeAlias

from fastapi import APIRouter, Depends, Path, Query
from safir.models import ErrorLocation
Expand All @@ -26,9 +26,11 @@
# e.g. GET /astroplan/observers/rubin

# We can declare path parameters that are used commonly across multiple
# endpoints in a single place.
# endpoints in a single place. Since this is a type annotation, we can use
# the type keyword in Python 3.12, which is equivalent to the TypeAlias type
# before.

observer_id_type = Annotated[
ObserverIdPathParam: TypeAlias = Annotated[
str,
Path(
...,
Expand All @@ -44,7 +46,7 @@
response_model=ObserverModel,
)
async def get_observer(
observer_id: observer_id_type,
observer_id: ObserverIdPathParam,
context: Annotated[RequestContext, Depends(context_dependency)],
) -> ObserverModel:
# Use the request context and factory patterns to get an observer service.
Expand All @@ -67,14 +69,9 @@ async def get_observer(
raise

# Transform the domain object into a response object.
response_data = ObserverModel.from_domain(
return ObserverModel.from_domain(
observer=observer, request=context.request
)
# Make any modifications to the response headers or status code.
context.response.headers["Location"] = response_data.self_url

# Return the response object. FastAPI takes care of serializing it to JSON.
return response_data


# RESTFul APIs commonly let clients list all resources of a type, usually
Expand Down Expand Up @@ -117,7 +114,8 @@ async def get_observers(

# This is a POST endpoint. A POST request lets the client send a JSON payload.
# Often this is used to create a new resource, but in this case we're using it
# to trigger a calculation that's too complex to be done in a query string.
# to trigger a calculation that's too complex to be done in a GET endpoint
# with a query string.
#
# e.g. POST /astroplan/observers/rubin/observable

Expand All @@ -130,7 +128,7 @@ async def get_observers(
response_model=ObservabilityResponseModel,
)
async def post_observable(
observer_id: observer_id_type,
observer_id: ObserverIdPathParam,
request_data: ObservationRequestModel,
context: Annotated[RequestContext, Depends(context_dependency)],
) -> ObservabilityResponseModel:
Expand Down

0 comments on commit cd4ddc6

Please sign in to comment.