This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from QwantResearch/directions-place-id
New directions endpoint with place id parameters
- Loading branch information
Showing
20 changed files
with
237 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,67 @@ | ||
from fastapi import HTTPException | ||
|
||
from pydantic import constr | ||
|
||
from fastapi import HTTPException, Query, Depends | ||
from starlette.requests import Request | ||
from starlette.responses import Response | ||
|
||
from idunn import settings | ||
from idunn.utils.geometry import city_surrounds_polygons | ||
from idunn.places import Latlon, place_from_id, InvalidPlaceId | ||
from idunn.utils.rate_limiter import IdunnRateLimiter | ||
from ..directions.client import directions_client | ||
|
||
|
||
rate_limiter = IdunnRateLimiter( | ||
resource="idunn.api.directions", | ||
max_requests=int(settings["DIRECTIONS_RL_MAX_REQUESTS"]), | ||
expire=int(settings["DIRECTIONS_RL_EXPIRE"]), | ||
) | ||
|
||
|
||
def get_directions( | ||
response: Response, | ||
def directions_request(request: Request, response: Response): | ||
""" | ||
FastAPI Dependency | ||
Responsible for rate limit and cache headers for directions requests | ||
""" | ||
rate_limiter.check_limit_per_client(request) | ||
response.headers["cache-control"] = "max-age={}".format(settings["DIRECTIONS_CLIENT_CACHE"]) | ||
return request | ||
|
||
|
||
def get_directions_with_coordinates( | ||
# URL values | ||
f_lon: float, | ||
f_lat: float, | ||
t_lon: float, | ||
t_lat: float, # URL values | ||
request: Request, | ||
type: constr(min_length=1), | ||
language: str = "en", # query parameters | ||
t_lat: float, | ||
# Query parameters | ||
type: str, | ||
language: str = "en", | ||
# Request | ||
request: Request = Depends(directions_request), | ||
): | ||
from_place = Latlon(f_lat, f_lon) | ||
to_place = Latlon(t_lat, t_lon) | ||
if not type: | ||
raise HTTPException(status_code=400, detail='"type" query param is required') | ||
return directions_client.get_directions( | ||
from_place, to_place, type, language, params=request.query_params | ||
) | ||
|
||
|
||
def get_directions( | ||
# Query parameters | ||
origin: str = Query(..., description="Origin place id"), | ||
destination: str = Query(..., description="Destination place id"), | ||
type: str = Query(..., description="Transport mode"), | ||
language: str = Query("en", description="User language"), | ||
# Request | ||
request: Request = Depends(directions_request), | ||
): | ||
rate_limiter.check_limit_per_client(request) | ||
from_position = (f_lon, f_lat) | ||
to_position = (t_lon, t_lat) | ||
try: | ||
from_place = place_from_id(origin) | ||
to_place = place_from_id(destination) | ||
except InvalidPlaceId as exc: | ||
raise HTTPException(status_code=404, detail=exc.message) | ||
|
||
response.headers["cache-control"] = "max-age={}".format(settings["DIRECTIONS_CLIENT_CACHE"]) | ||
return directions_client.get_directions( | ||
from_position, to_position, type, language, params=request.query_params | ||
from_place, to_place, type, language, params=request.query_params | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.