diff --git a/navitia_client/client/apis/contributors_apis.py b/navitia_client/client/apis/contributors_apis.py index 1d886d3..5b1f210 100644 --- a/navitia_client/client/apis/contributors_apis.py +++ b/navitia_client/client/apis/contributors_apis.py @@ -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 diff --git a/navitia_client/client/apis/coverage_apis.py b/navitia_client/client/apis/coverage_apis.py index b9f206d..9589111 100644 --- a/navitia_client/client/apis/coverage_apis.py +++ b/navitia_client/client/apis/coverage_apis.py @@ -1,4 +1,3 @@ -from datetime import datetime from typing import Any, Sequence from navitia_client.client.apis.api_base_client import ApiBaseClient @@ -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]: diff --git a/navitia_client/entities/administrative_region.py b/navitia_client/entities/administrative_region.py index cf25f93..db54d55 100644 --- a/navitia_client/entities/administrative_region.py +++ b/navitia_client/entities/administrative_region.py @@ -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): diff --git a/navitia_client/entities/contributor.py b/navitia_client/entities/contributor.py index 0e8a6b0..c768eb1 100644 --- a/navitia_client/entities/contributor.py +++ b/navitia_client/entities/contributor.py @@ -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 @@ -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"], + ) diff --git a/navitia_client/entities/dataset.py b/navitia_client/entities/dataset.py index 6e46184..fc8fbe6 100644 --- a/navitia_client/entities/dataset.py +++ b/navitia_client/entities/dataset.py @@ -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 @@ -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"], + )