write more schema for routing api
This commit is contained in:
parent
11f96f7e83
commit
210d079bb7
2 changed files with 100 additions and 20 deletions
|
@ -206,6 +206,7 @@ PositionID = Annotated[NonEmptyStr, APIField(
|
|||
pattern=r"p:[A-Za-z0-9]+$",
|
||||
description="the ID of a user-defined tracked position is made up of its secret"
|
||||
)]
|
||||
Coordinates3D = tuple[float, float, float]
|
||||
|
||||
|
||||
AnyLocationID = Union[
|
||||
|
|
|
@ -1,43 +1,122 @@
|
|||
from ninja import Router as APIRouter
|
||||
from enum import StrEnum
|
||||
from typing import Annotated, Union, Optional
|
||||
|
||||
from c3nav.api.newauth import auth_responses
|
||||
from ninja import Router as APIRouter, Schema, Field as APIField
|
||||
|
||||
from c3nav.api.newauth import auth_responses, validate_responses
|
||||
from c3nav.api.utils import NonEmptyStr
|
||||
from c3nav.mapdata.models import Source
|
||||
from pydantic import PositiveInt
|
||||
from c3nav.mapdata.schemas.model_base import AnyLocationID, Coordinates3D
|
||||
from c3nav.mapdata.schemas.responses import BoundsSchema
|
||||
|
||||
routing_api_router = APIRouter(tags=["routing"])
|
||||
|
||||
|
||||
class RouteMode(StrEnum):
|
||||
""" how to optimize the route """
|
||||
FASTEST = "fastest"
|
||||
SHORTEST = "shortest"
|
||||
|
||||
|
||||
class WalkSpeed(StrEnum):
|
||||
""" the walk speed """
|
||||
SLOW = "slow"
|
||||
DEFAULT = "default"
|
||||
FAST = "fast"
|
||||
|
||||
|
||||
class LevelWayTypeChoice(StrEnum):
|
||||
""" route preferences for way types that are level """
|
||||
ALLOW = "allow"
|
||||
AVOID = "avoid"
|
||||
|
||||
|
||||
class AltitudeWayTypeChoice(StrEnum):
|
||||
""" route preferences for way types that overcome a change in altitude """
|
||||
ALLOW = "allow"
|
||||
AVOID_UP = "avoid_up"
|
||||
AVOID_DOWN = "avoid_down"
|
||||
AVOID = "avoid"
|
||||
|
||||
|
||||
class RouteOptionsSchema(Schema):
|
||||
mode: RouteMode = RouteMode.FASTEST
|
||||
walk_speed: WalkSpeed = WalkSpeed.DEFAULT
|
||||
way_types: dict[
|
||||
Annotated[NonEmptyStr, APIField(title="waytype")],
|
||||
Union[
|
||||
Annotated[LevelWayTypeChoice, APIField(default=LevelWayTypeChoice.ALLOW)],
|
||||
Annotated[AltitudeWayTypeChoice, APIField(default=AltitudeWayTypeChoice.ALLOW)],
|
||||
]
|
||||
] = APIField(default_factory=dict)
|
||||
|
||||
|
||||
class RouteParametersSchema(Schema):
|
||||
origin: AnyLocationID
|
||||
destination: AnyLocationID
|
||||
options_override: Optional[RouteOptionsSchema] = None
|
||||
|
||||
|
||||
class RouteItemSchema(Schema):
|
||||
id: PositiveInt
|
||||
coordinates: Coordinates3D
|
||||
way_type: Optional[dict] # todo: improve
|
||||
space: Optional[dict] = APIField(title="new space being entered")
|
||||
level: Optional[dict] = APIField(title="new level being entered")
|
||||
descriptions: list[NonEmptyStr]
|
||||
|
||||
|
||||
class RouteSchema(Schema):
|
||||
origin: dict # todo: improve this
|
||||
destination: dict # todo: improve this
|
||||
distance: float
|
||||
duration: int
|
||||
distance_str: NonEmptyStr
|
||||
duration_str: NonEmptyStr
|
||||
summary: NonEmptyStr
|
||||
options_summary: NonEmptyStr
|
||||
items: list[RouteItemSchema]
|
||||
|
||||
|
||||
@routing_api_router.post('/route/', summary="get route between two locations",
|
||||
response={200: BoundsSchema, **auth_responses})
|
||||
def get_route(request):
|
||||
response={200: RouteSchema, **validate_responses, **auth_responses})
|
||||
# todo: route failure responses
|
||||
def get_route(request, parameters: RouteParametersSchema):
|
||||
# todo: implement
|
||||
return {
|
||||
"bounds": Source.max_bounds(),
|
||||
}
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@routing_api_router.get('/options/', summary="get current route options",
|
||||
response={200: BoundsSchema, **auth_responses})
|
||||
response={200: RouteOptionsSchema, **auth_responses})
|
||||
def get_route_options(request):
|
||||
# todo: implement
|
||||
return {
|
||||
"bounds": Source.max_bounds(),
|
||||
}
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@routing_api_router.put('/options/', summary="set route options for user or session",
|
||||
response={200: BoundsSchema, **auth_responses})
|
||||
def set_route_options(request):
|
||||
response={200: RouteOptionsSchema, **validate_responses, **auth_responses})
|
||||
def set_route_options(request, options: RouteOptionsSchema):
|
||||
# todo: implement
|
||||
return {
|
||||
"bounds": Source.max_bounds(),
|
||||
}
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class RouteOptionsFieldChoices(Schema):
|
||||
name: NonEmptyStr
|
||||
title: NonEmptyStr
|
||||
|
||||
|
||||
class RouteOptionsField(Schema):
|
||||
name: NonEmptyStr
|
||||
type: NonEmptyStr
|
||||
label: NonEmptyStr
|
||||
choices: list[RouteOptionsFieldChoices]
|
||||
value: NonEmptyStr
|
||||
value_display: NonEmptyStr
|
||||
|
||||
|
||||
@routing_api_router.get('/options/form/', summary="get current route options with form definitions (like old API)",
|
||||
response={200: BoundsSchema, **auth_responses})
|
||||
response={200: list[RouteOptionsField], **auth_responses})
|
||||
def get_route_options_form(request):
|
||||
# todo: implement
|
||||
return {
|
||||
"bounds": Source.max_bounds(),
|
||||
}
|
||||
raise NotImplementedError
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue