Skip to content

Commit ebfa2f9

Browse files
committed
Rollback custom markers on multi-stop maps; Refactor google_maps module to package; Validate at least one stop with location when getting multi-stop map
1 parent 4644f2f commit ebfa2f9

File tree

8 files changed

+309
-254
lines changed

8 files changed

+309
-254
lines changed

vigobusapi/app.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
# # Native # #
66
import io
7+
import json
78
from typing import Optional, Set
89

910
# # Installed # #
@@ -108,7 +109,7 @@ async def endpoint_get_stop_map(
108109
):
109110
"""Get a picture of a map with the stop location marked on it."""
110111
stop = await get_stop(stop_id)
111-
if (stop.lat, stop.lon) == (None, None):
112+
if not stop.has_location:
112113
raise HTTPException(status_code=409, detail="The stop does not have information about the location")
113114

114115
map_request = GoogleMapRequest(
@@ -126,14 +127,30 @@ async def endpoint_get_stop_map(
126127

127128
@app.get("/stops/map")
128129
async def endpoint_get_stops_map(
129-
stops_ids: Set[int] = Query(None, alias="stop_id", min_items=1, max_items=35),
130+
stops_ids: Set[int] = Query(None, alias="stop_id",
131+
min_items=1, max_items=len(GoogleMapRequest.Tag.get_allowed_labels())),
130132
map_params: MapQueryParams = Depends(),
131133
):
132-
"""Get a picture of a map with the locations of the given stops marked on it."""
133-
stops = await get_stops(stops_ids)
134+
"""Get a picture of a map with the locations of the given stops marked on it.
135+
136+
Non existing stops, or those without location available, are ignored,
137+
but if none of the given stops are valid, returns 404.
134138
135-
# noinspection PyTypeChecker
136-
stops_tags = [GoogleMapRequest.Tag(label=stop.stop_id, location_x=stop.lat, location_y=stop.lon) for stop in stops]
139+
A header "X-Stops-Tags" is returned, being a JSON associating the Stops IDs with the tag label on the map,
140+
with the format: {"<stop id>" : "<tag label>"}
141+
"""
142+
stops = await get_stops(stops_ids)
143+
stops = [stop for stop in stops if stop.has_location]
144+
if not stops:
145+
raise HTTPException(status_code=404, detail="None of the stops exist or have location available")
146+
147+
stops_tags = list()
148+
stops_tags_relation = dict()
149+
for i, stop in enumerate(stops):
150+
tag_label = GoogleMapRequest.Tag.get_allowed_labels()[i]
151+
tag = GoogleMapRequest.Tag(label=tag_label, location_x=stop.lat, location_y=stop.lon)
152+
stops_tags.append(tag)
153+
stops_tags_relation[stop.stop_id] = tag_label
137154

138155
map_request = GoogleMapRequest(
139156
size_x=map_params.size_x,
@@ -146,7 +163,8 @@ async def endpoint_get_stops_map(
146163

147164
return StreamingResponse(
148165
content=io.BytesIO(map_data),
149-
media_type="image/png"
166+
media_type="image/png",
167+
headers={"X-Stops-Tags": json.dumps(stops_tags_relation)}
150168
)
151169

152170

@@ -157,7 +175,7 @@ async def endpoint_get_stop_photo(
157175
size_y: int = google_maps_settings.stop_photo_default_size_y,
158176
):
159177
stop = await get_stop(stop_id)
160-
if (stop.lat, stop.lon) == (None, None):
178+
if not stop.has_location:
161179
raise HTTPException(status_code=409, detail="The stop does not have information about the location")
162180

163181
photo_request = GoogleStreetviewRequest(

vigobusapi/entities.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def get_mongo_dict(self):
7777
d.pop("source")
7878
return d
7979

80+
@property
81+
def has_location(self):
82+
return (self.lat, self.lon) != (None, None)
83+
8084

8185
OptionalStop = Optional[Stop]
8286
StopOrNotExist = Union[Stop, StopNotExist]

vigobusapi/services/google_maps.py

Lines changed: 0 additions & 246 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""GOOGLE MAPS
2+
Classes and functions for acquiring static maps and photos using Google Maps & Street View Static APIs
3+
"""
4+
5+
from ._entities import *
6+
from ._getter_maps import *
7+
from ._getter_streetview import *

0 commit comments

Comments
 (0)