diff --git a/swh/web/client/client.py b/swh/web/client/client.py --- a/swh/web/client/client.py +++ b/swh/web/client/client.py @@ -28,6 +28,7 @@ """ +from datetime import datetime from typing import Any, Callable, Dict, Iterator, List, Optional, Union from urllib.parse import urlparse @@ -67,11 +68,16 @@ """ - def to_swhid(object_type, s): + def to_swhid(object_type: str, s: Any) -> SWHID: return SWHID(object_type=object_type, object_id=s) - def to_date(s): - return dateutil.parser.parse(s) + def to_date(date: str) -> datetime: + return dateutil.parser.parse(date) + + def to_optional_date(date: Optional[str]) -> Optional[datetime]: + return None if date is None else to_date(date) + + # The date attribute is optional for Revision and Release object def obj_type_of_entry_type(s): if s == "file": @@ -92,12 +98,12 @@ data["id"] = to_swhid(obj_type, data["id"]) data["directory"] = to_swhid(DIRECTORY, data["directory"]) for key in ("date", "committer_date"): - data[key] = to_date(data[key]) + data[key] = to_optional_date(data[key]) for parent in data["parents"]: parent["id"] = to_swhid(REVISION, parent["id"]) elif obj_type == RELEASE: data["id"] = to_swhid(obj_type, data["id"]) - data["date"] = to_date(data["date"]) + data["date"] = to_optional_date(data["date"]) data["target"] = to_swhid(data["target_type"], data["target"]) elif obj_type == DIRECTORY: dir_swhid = None diff --git a/swh/web/client/tests/test_web_api_client.py b/swh/web/client/tests/test_web_api_client.py --- a/swh/web/client/tests/test_web_api_client.py +++ b/swh/web/client/tests/test_web_api_client.py @@ -7,7 +7,8 @@ from dateutil.parser import parse as parse_date -from swh.model.identifiers import parse_swhid +from swh.model.identifiers import REVISION, parse_swhid +from swh.web.client.client import typify_json from .api_data import API_DATA @@ -172,3 +173,17 @@ break assert actual == expected + + +def test_typify_json_minimal_revision(): + revision_data = { + "id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "directory": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "date": None, + "committer_date": None, + "parents": [], + } + revision_typed = typify_json(revision_data, REVISION) + pid = "swh:1:rev:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + assert revision_typed["id"] == parse_swhid(pid) + assert revision_typed["date"] is None