Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "toadr3"
version = "0.28.0"
version = "0.29.0"
description = "Tiny OpenADR 3 compatible client Python Library"
authors = ["Jean-Paul Balabanian <jean-paul.balabanian@eviny.no>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/_common_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def create_problem_response(title: str, status: int, detail: str) -> web.Respons
detail=detail,
),
status=status,
dumps=Problem.model_dump_json,
dumps=Problem.as_json,
)


Expand Down
5 changes: 5 additions & 0 deletions tests/test_event_payload_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def test_event_payload_descriptor_correct_object_type() -> None:
assert payload_descriptor.units is None
assert payload_descriptor.currency is None

json = payload_descriptor.as_json()
assert json == (
'{"objectType":"EVENT_PAYLOAD_DESCRIPTOR","payloadType":"CONSUMPTION_POWER_LIMIT"}'
)

json = payload_descriptor.model_dump_json()
assert json == (
'{"objectType":"EVENT_PAYLOAD_DESCRIPTOR",'
Expand Down
2 changes: 0 additions & 2 deletions tests/test_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ def test_toadr_error_from_problem_with_details() -> None:
assert error.message == "title: detail"
assert error.status_code == 500
assert error.json_response == {
"type": None,
"title": "title",
"status": 500,
"detail": "detail",
"instance": None,
}
2 changes: 1 addition & 1 deletion tests/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,5 @@ def test_interval_to_json() -> None:

interval = Interval.model_validate_json(json_data)

result = interval.model_dump_json()
result = interval.as_json()
assert result == json_data
3 changes: 3 additions & 0 deletions tests/test_interval_periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def test_interval_period_to_json() -> None:
result = ip.model_dump_json(exclude_defaults=True)
assert result == '{"start":"2024-09-24T01:02:03Z","duration":"P1DT2H"}'

result = ip.as_json()
assert result == '{"start":"2024-09-24T01:02:03Z","duration":"P1DT2H"}'


def test_interval_periods_sub_second_precision_in_json() -> None:
"""Test that the IntervalPeriod model can handle sub-second precision."""
Expand Down
6 changes: 3 additions & 3 deletions tests/test_json_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_json_encoder_report() -> None:
data = create_report()

report = Report.model_validate(data)
result = Report.model_dump(report, exclude_none=True, exclude_unset=True)
result = Report.as_dict(report)

# for comparison, we expect datetime objects and not strings
data["createdDateTime"] = from_iso(data["createdDateTime"])
Expand All @@ -38,7 +38,7 @@ def test_json_encoder_event() -> None:
data = create_event()

event = Event.model_validate(data)
result = Event.model_dump(event, exclude_none=True, exclude_unset=True)
result = Event.as_dict(event)

# for comparison, we expect datetime objects and not strings
data["createdDateTime"] = from_iso(data["createdDateTime"])
Expand All @@ -63,7 +63,7 @@ def test_json_encode_as_expected(model_class: type[BaseModel], model_data: dict[
# 1. we expect only default values to be part of the final JSON
# 2. we expect the 'objectType' field to be included in the final JSON
instance = model_class.model_validate(model_data)
result = model_class.model_dump(instance, exclude_none=True, exclude_unset=True)
result = model_class.as_dict(instance) # type: ignore[attr-defined]

assert "objectType" not in model_data
assert "objectType" in result
Expand Down
2 changes: 1 addition & 1 deletion toadr3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ def from_problem(cls, problem: Problem, status_code: int | None = None) -> "Toad
return ToadrError(
message=message,
status_code=status_code,
json_response=problem.model_dump(),
json_response=problem.as_dict(),
)
8 changes: 8 additions & 0 deletions toadr3/models/docstringbasemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ def model_post_init(self, _context: Any, /) -> None: # noqa: ANN401
def __str__(self) -> str:
"""Return a string representation of the object."""
return f"{self.__class__.__name__}({super().__str__()})"

def as_dict(self) -> dict[str, Any]:
"""Return the model as a dictionary."""
return self.model_dump(exclude_unset=True, exclude_none=True)

def as_json(self) -> str:
"""Return the model as a JSON string."""
return self.model_dump_json(exclude_unset=True, exclude_none=True)
2 changes: 1 addition & 1 deletion toadr3/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ async def put_program_by_id(
if program is None:
raise ValueError("program is required")

data = program.model_dump_json(exclude_none=True, exclude_unset=True)
data = program.as_json()

if custom_headers is None:
custom_headers = {}
Expand Down
2 changes: 1 addition & 1 deletion toadr3/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def post_report(

vtn_url = vtn_url.rstrip("/")

data = report.model_dump_json(exclude_none=True, exclude_unset=True)
data = report.as_json()
headers["Content-Type"] = "application/json"

async with session.post(f"{vtn_url}/reports", headers=headers, data=data) as response:
Expand Down
4 changes: 2 additions & 2 deletions toadr3/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async def post_subscription(

vtn_url = vtn_url.rstrip("/")

data = subscription.model_dump_json(exclude_none=True, exclude_unset=True)
data = subscription.as_json()
headers["Content-Type"] = "application/json"

async with session.post(f"{vtn_url}/subscriptions", headers=headers, data=data) as response:
Expand Down Expand Up @@ -314,7 +314,7 @@ async def put_subscription_by_id(
if subscription is None:
raise ValueError("subscription is required")

data = subscription.model_dump_json(exclude_none=True, exclude_unset=True)
data = subscription.as_json()

if custom_headers is None:
custom_headers = {}
Expand Down