Skip to content

Commit

Permalink
chore(entities): refactoring couple of entities
Browse files Browse the repository at this point in the history
* Contributor, Region and Dataset
  • Loading branch information
jonperron committed May 4, 2024
1 parent 537dce6 commit 51f9930
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 39 deletions.
11 changes: 2 additions & 9 deletions navitia_client/client/apis/contributors_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ def _get_contributors_from_response(
raw_contributors_response: Any,
) -> Sequence[Contributor]:
contributors = []
for contributor in raw_contributors_response:
contributors.append(
Contributor(
id=contributor.get("id"),
name=contributor.get("name"),
license=contributor.get("license"),
website=contributor.get("website"),
),
)
for contributor_data in raw_contributors_response:
contributors.append(Contributor.from_json(contributor_data))

return contributors

Expand Down
30 changes: 2 additions & 28 deletions navitia_client/client/apis/coverage_apis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime
from typing import Any, Sequence

from navitia_client.client.apis.api_base_client import ApiBaseClient
Expand All @@ -9,33 +8,8 @@ class CoverageApiClient(ApiBaseClient):
@staticmethod
def _get_regions_from_response(raw_regions_response: Any) -> Sequence[Region]:
regions = []
for region in raw_regions_response:
regions.append(
Region(
id=region.get("id"),
name=region.get("name"),
dataset_created_at=datetime.fromisoformat(
region.get("dataset_created_at")
)
if region.get("dataset_created_at")
else None,
end_production_date=datetime.strptime(
region.get("end_production_date"), "%Y%m%d"
)
if region.get("end_production_date")
else None,
last_load_at=datetime.fromisoformat(region.get("last_load_at"))
if region.get("last_load_at")
else None,
shape=region.get("shape"),
start_production_date=datetime.strptime(
region.get("start_production_date"), "%Y%m%d"
)
if region.get("end_production_date")
else None,
status=region.get("status"),
)
)
for region_data in raw_regions_response:
regions.append(Region.from_json(region_data))
return regions

def list_covered_areas(self) -> Sequence[Region]:
Expand Down
25 changes: 25 additions & 0 deletions navitia_client/entities/administrative_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ class Region(BaseEntity):
start_production_date: Optional[datetime]
status: Optional[str]

@staticmethod
def from_json(payload: dict[str, Any]) -> "Region":
return Region(
id=payload["id"],
name=payload["name"],
dataset_created_at=datetime.fromisoformat(payload["dataset_created_at"])
if "dataset_created_at" in payload
else None,
end_production_date=datetime.strptime(
payload["end_production_date"], "%Y%m%d"
)
if "end_production_date" in payload
else None,
last_load_at=datetime.fromisoformat(payload["last_load_at"])
if "last_load_at" in payload
else None,
shape=payload["shape"],
start_production_date=datetime.strptime(
payload["start_production_date"], "%Y%m%d"
)
if "end_production_date" in payload
else None,
status=payload["status"],
)


@dataclass
class AdministrativeRegion(BaseEntity):
Expand Down
11 changes: 10 additions & 1 deletion navitia_client/entities/contributor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Optional
from typing import Any, Optional

from navitia_client.entities.base_entity import BaseEntity

Expand All @@ -8,3 +8,12 @@
class Contributor(BaseEntity):
license: str
website: Optional[str]

@staticmethod
def from_json(payload: dict[str, Any]) -> "Contributor":
return Contributor(
id=payload["id"],
name=payload["name"],
license=payload["license"],
website=payload["website"],
)
18 changes: 17 additions & 1 deletion navitia_client/entities/dataset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass
from datetime import datetime
from typing import Optional
from typing import Any, Optional

from navitia_client.entities.contributor import Contributor

Expand All @@ -14,3 +14,19 @@ class Dataset:
realtime_level: str
start_validation_date: datetime
system: Optional[str]

@staticmethod
def from_json(payload: dict[str, Any]) -> "Dataset":
return Dataset(
contributor=Contributor.from_json(payload["contributor"]),
description=payload["description"],
end_validation_date=datetime.strptime(
payload["end_validation_date"], "%Y%m%dT%H%M%S"
),
id=payload["id"],
realtime_level=payload["realtime_level"],
start_validation_date=datetime.strptime(
payload["start_validation_date"], "%Y%m%dT%H%M%S"
),
system=payload["system"],
)

0 comments on commit 51f9930

Please sign in to comment.