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

Improve V1 compatibility #16

Merged
merged 4 commits into from
Jan 9, 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
18 changes: 12 additions & 6 deletions web/api_v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ def city_mapping(cls) -> Dict:
city["attribution"] = None

lng, lat = loc["geo_point"].tuple
source_url = city.pop("source_url")
public_url = city.pop("public_url")
city.update({
"coords": {"lat": lat, "lng": lng},
"name": loc["city"],
"url": city.pop("public_url"),
"source": city.pop("source_url"),
"url": public_url,
"source": source_url if source_url else public_url,
# TODO: no db-field yet
"active_support": False,
})
Expand Down Expand Up @@ -179,19 +181,21 @@ def get(self, request: Request, city: str):
"address": lot.address,
"coords": coords,
"forecast": False, # TODO
"free": None,
# "free": None, if free is unkown, we don't return it
"id": lot.lot_id,
"lot_type": LOT_TYPE_MAPPING.get(lot.type, "unbekannt"),
"name": lot.name,
"region": None, # TODO
"state": None,
"total": lot.max_capacity,
# ParkAPI v1 requires total, so we return 0 if unknown
"total": lot.max_capacity if lot.max_capacity else 0,
}
if lot.latest_data:
api_lot.update({
"free": lot.latest_data.num_free,
"state": lot.latest_data.status,
})
if lot.latest_data.num_free is not None:
api_lot["free"] = lot.latest_data.num_free
if lot.latest_data.capacity is not None:
api_lot["total"] = lot.latest_data.capacity

Expand All @@ -206,7 +210,9 @@ def get(self, request: Request, city: str):

return Response({
"last_downloaded": last_downloaded,
"last_updated": last_updated,
# v1 schema requires last_updated to be set. If it's not available,
# we fall back to (somewhat misleading) last_downloaded
"last_updated": last_updated if last_updated else last_downloaded,
"lots": api_lot_list,
})

6 changes: 4 additions & 2 deletions web/park_api/tests/test_api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test_100_root(self):
"coords": {"lat": 51.6512192, "lng": 7.3393014},
"name": "Datteln",
"url": "https://www.apag.de",
"source": None,
# Return url as source url, if no explicit source url was defined
"source": "https://www.apag.de",
"active_support": False,
},
"Dresden": {
Expand Down Expand Up @@ -74,7 +75,8 @@ def test_200_city(self):
'address': 'An der Frauenkirche 12a\n01067 Dresden\nEinfahrt von Schießgasse\nKontakt: 03 51 / 496 06 03\[email protected]',
'coords': {'lat': 51.051415072, 'lng': 13.7441934672},
'forecast': False,
'free': None,
# Don't return free, if not available (to stay backward compatible with ParkAPIv1)
jklmnn marked this conversation as resolved.
Show resolved Hide resolved
# 'free': None,
'id': 'dresdenanderfrauenkirche',
'lot_type': 'Tiefgarage',
'name': 'An der Frauenkirche',
Expand Down
7 changes: 6 additions & 1 deletion web/park_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic.base import RedirectView

from .views import stats

urlpatterns = [
path("", stats.StatsView.as_view(), name="stats"),
path("stats/", stats.StatsView.as_view(), name="stats"),
path("admin/", admin.site.urls),
path("api/", include('park_api.api_urls')),
# Maintain API v1 compatibility by redirecting former URLs to new locations
path("", RedirectView.as_view(url='api/')),
path("<city>", RedirectView.as_view(url='api/%(city)s')),
path("<city>/<lot_id>/timespan", RedirectView.as_view(url='api/v1/%(city)s/%(lot_id)s/timespan')),
]

if settings.DEBUG is True:
Expand Down
Loading