Skip to content

Commit

Permalink
handle bahn without capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
the-infinity committed Jun 4, 2024
1 parent dc4788d commit c898ea8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/parkapi_sources/converters/bahn_v2/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def map_static_parking_site(bahn_input: BahnParkingSiteInput) -> StaticParkingSi
lon=bahn_input.address.location.longitude,
operator_name=bahn_input.operator.name,
address=f'{bahn_input.address.streetAndNumber}, {bahn_input.address.zip} {bahn_input.address.city}',
capacity=bahn_input.capacity,
type=bahn_input.type.name.to_parking_site_type_input(),
has_realtime_data=False, # TODO: change this as soon as Bahn offers proper rate limits
static_data_updated_at=datetime.now(tz=timezone.utc),
Expand All @@ -30,6 +29,7 @@ def map_static_parking_site(bahn_input: BahnParkingSiteInput) -> StaticParkingSi
static_parking_site_input.opening_hours = '24/7'

for capacity_data in bahn_input.capacity:
# Because it was checked in validation, we can be sure that capacity will be set
if capacity_data.type == BahnParkingSiteCapacityType.PARKING:
static_parking_site_input.capacity = int(round(capacity_data.total))
elif capacity_data.type == BahnParkingSiteCapacityType.HANDICAPPED_PARKING:
Expand Down
10 changes: 9 additions & 1 deletion src/parkapi_sources/converters/bahn_v2/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Optional

from validataclass.dataclasses import Default, validataclass
from validataclass.exceptions import ValidationError
from validataclass.validators import (
BooleanValidator,
DataclassValidator,
Expand Down Expand Up @@ -115,7 +116,7 @@ class BahnRestrictionInput:
class BahnAccessInput:
openingHours: BahnOpeningHoursInput = DataclassValidator(BahnOpeningHoursInput)
restrictions: BahnRestrictionInput = DataclassValidator(BahnRestrictionInput)
# TODO: ignored multible attributes which do not matter so far
# TODO: ignored multiple attributes which do not matter so far


@validataclass
Expand All @@ -129,3 +130,10 @@ class BahnParkingSiteInput:
capacity: list[BahnCapacityInput] = ListValidator(DataclassValidator(BahnCapacityInput))
access: BahnAccessInput = DataclassValidator(BahnAccessInput)
# TODO: ignored multible attributes which do not matter so far

def __post_init__(self):
for capacity in self.capacity:
if capacity.type == BahnParkingSiteCapacityType.PARKING:
return
# If no capacity with type PARKING was found, we miss the capacity and therefore throw a validation error
raise ValidationError(reason='Missing parking capacity')

0 comments on commit c898ea8

Please sign in to comment.