mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-25 00:34:32 +00:00
Allow spots and alerts to have multiple activities #143
This commit is contained in:
@@ -24,7 +24,7 @@ _V2_TO_V3_QUERY_PARAMS = {
|
||||
# Values of query parameters renamed in v3
|
||||
_V2_TO_V3_QUERY_VALUES = {
|
||||
"activity": {"NO_SIG": "NO_ACTIVITY"},
|
||||
"fields": {"sig": "activity", "sig_refs": "activity_refs"},
|
||||
"fields": {"sig": "activities", "sig_refs": "activity_refs"},
|
||||
}
|
||||
# Keys of JSON objects in API responses renamed in v3
|
||||
_V3_TO_V2_RESPONSE_KEYS = {
|
||||
@@ -54,19 +54,28 @@ class V2CompatibilityWrapper(CompatibilityWrapper):
|
||||
return rename_keys_and_values(obj, _V3_TO_V2_RESPONSE_KEYS, _V3_TO_V2_RESPONSE_VALUES)
|
||||
|
||||
|
||||
class V2APISpotsHandler(V2CompatibilityWrapper, RequestCompatibilityWrapper, APISpotsHandler):
|
||||
class V2SpotsAlertsCompatibilityWrapper(V2CompatibilityWrapper):
|
||||
"""Extra translation for spots and alerts. In v3 these have a list of "activities" rather than a single activity,
|
||||
so for v2 we collapse this back down to a single value using the first activity in the list. This must happen
|
||||
before the generic key renaming, which would otherwise rename "activities" to "sigs" as it does for /options."""
|
||||
|
||||
def translate_response_object(self, obj):
|
||||
return super().translate_response_object(collapse_activities(obj))
|
||||
|
||||
|
||||
class V2APISpotsHandler(V2SpotsAlertsCompatibilityWrapper, RequestCompatibilityWrapper, APISpotsHandler):
|
||||
"""No special handling for this, the compatibility wrapper will handle translation to and from the later version."""
|
||||
|
||||
|
||||
class V2APISpotsStreamHandler(V2CompatibilityWrapper, StreamCompatibilityWrapper, APISpotsStreamHandler):
|
||||
class V2APISpotsStreamHandler(V2SpotsAlertsCompatibilityWrapper, StreamCompatibilityWrapper, APISpotsStreamHandler):
|
||||
"""No special handling for this, the compatibility wrapper will handle translation to and from the later version."""
|
||||
|
||||
|
||||
class V2APIAlertsHandler(V2CompatibilityWrapper, RequestCompatibilityWrapper, APIAlertsHandler):
|
||||
class V2APIAlertsHandler(V2SpotsAlertsCompatibilityWrapper, RequestCompatibilityWrapper, APIAlertsHandler):
|
||||
"""No special handling for this, the compatibility wrapper will handle translation to and from the later version."""
|
||||
|
||||
|
||||
class V2APIAlertsStreamHandler(V2CompatibilityWrapper, StreamCompatibilityWrapper, APIAlertsStreamHandler):
|
||||
class V2APIAlertsStreamHandler(V2SpotsAlertsCompatibilityWrapper, StreamCompatibilityWrapper, APIAlertsStreamHandler):
|
||||
"""No special handling for this, the compatibility wrapper will handle translation to and from the later version."""
|
||||
|
||||
|
||||
@@ -118,7 +127,8 @@ class V2APISpotHandler(V2CompatibilityWrapper, RequestCompatibilityWrapper, APIS
|
||||
|
||||
spot_data = dict(spot_data)
|
||||
if "sig" in spot_data:
|
||||
spot_data["activity"] = spot_data.pop("sig")
|
||||
sig = spot_data.pop("sig")
|
||||
spot_data["activities"] = [sig] if sig else []
|
||||
if "sig_refs" in spot_data:
|
||||
spot_data["activity_refs"] = spot_data.pop("sig_refs")
|
||||
if isinstance(spot_data.get("activity_refs"), list):
|
||||
@@ -130,3 +140,18 @@ class V2APISpotHandler(V2CompatibilityWrapper, RequestCompatibilityWrapper, APIS
|
||||
refs.append(ref)
|
||||
spot_data["activity_refs"] = refs
|
||||
return spot_data
|
||||
|
||||
|
||||
def collapse_activities(obj):
|
||||
"""Utility method to replace the "activities" list in a spot or alert JSON object with a single "activity" value,
|
||||
being the first activity in the list, or None if there are none. The object can be a single spot/alert dict, or a
|
||||
list of them. Anything else is returned untouched. Used to translate v3's list of activities to the single sig
|
||||
expected in v2 API calls."""
|
||||
|
||||
if isinstance(obj, list):
|
||||
return [collapse_activities(i) for i in obj]
|
||||
if isinstance(obj, dict) and "activities" in obj:
|
||||
obj = dict(obj)
|
||||
activities = obj.pop("activities")
|
||||
obj["activity"] = activities[0] if activities else None
|
||||
return obj
|
||||
Reference in New Issue
Block a user