Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response models print as indented JSON #82

Merged
merged 4 commits into from
Dec 18, 2024
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
3 changes: 3 additions & 0 deletions src/dapla_metadata/variable_definitions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Client for working with Variable Definitions at Statistics Norway."""

from .generated.vardef_client.exceptions import * # noqa: F403
from .generated.vardef_client.models import * # noqa: F403
from .vardef import Vardef
from .variable_definition import CompletePatchOutput
from .variable_definition import VariableDefinition
8 changes: 4 additions & 4 deletions src/dapla_metadata/variable_definitions/vardef.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Vardef:
@vardef_exception_handler
def create_draft(cls, draft: Draft) -> VariableDefinition:
"""Create a Draft Variable Definition."""
return VariableDefinition.from_complete_response(
return VariableDefinition.from_model(
DraftVariableDefinitionsApi(
VardefClient.get_client(),
).create_variable_definition(
Expand All @@ -105,7 +105,7 @@ def migrate_from_vardok(cls, vardok_id: str) -> VariableDefinition:
Returns:
VariableDefinition: The migrated Variable Definition in Vardef.
"""
return VariableDefinition.from_complete_response(
return VariableDefinition.from_model(
DataMigrationApi(
VardefClient.get_client(),
).create_variable_definition_from_var_dok(
Expand Down Expand Up @@ -135,7 +135,7 @@ def list_variable_definitions(
list[VariableDefinition]: The list of Variable Definitions.
"""
return [
VariableDefinition.from_complete_response(definition)
VariableDefinition.from_model(definition)
for definition in VariableDefinitionsApi(
VardefClient.get_client(),
).list_variable_definitions(
Expand All @@ -162,7 +162,7 @@ def get_variable_definition(
Raises:
NotFoundException when the given ID is not found
"""
return VariableDefinition.from_complete_response(
return VariableDefinition.from_model(
VariableDefinitionsApi(
VardefClient.get_client(),
).get_variable_definition_by_id(
Expand Down
119 changes: 75 additions & 44 deletions src/dapla_metadata/variable_definitions/variable_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,66 @@
)


class VariableDefinition(CompleteResponse):
class CompletePatchOutput(CompleteResponse):
"""Complete response For internal users who need all details while maintaining variable definitions."""

@staticmethod
def from_model(
model: CompleteResponse,
) -> "CompletePatchOutput":
"""Create a CompletePatchOutput instance from a CompletePatchOutput."""
return CompletePatchOutput.model_construct(**model.model_dump())

def __str__(self) -> str:
"""Format as indented JSON."""
return self.model_dump_json(indent=2)


class VariableDefinition(CompletePatchOutput):
"""A Variable Definition.

- Provides access to the fields of the specific Variable Definition.
- Provides methods to access Patches and Validity Periods of this Variable Definition.
- Provides methods allowing maintenance of this Variable Definition.

Args:
CompleteResponse: The Pydantic model superclass, representing a Variable Definition.
CompletePatchOutput: The Pydantic model superclass, representing a Variable Definition.
"""

@staticmethod
def from_complete_response(
complete_response: CompleteResponse,
def from_model(
model: CompleteResponse,
) -> "VariableDefinition":
"""Create a VariableDefinition instance from a CompleteResponse."""
return VariableDefinition.model_construct(**complete_response.model_dump())
"""Create a VariableDefinition instance from a CompletePatchOutput or CompletePatchOutput."""
return VariableDefinition.model_construct(**model.model_dump())

@vardef_exception_handler
def list_validity_periods(self) -> list[CompleteResponse]:
def list_validity_periods(self) -> list[CompletePatchOutput]:
"""List all Validity Periods for this Variable Definition."""
return ValidityPeriodsApi(VardefClient.get_client()).list_validity_periods(
variable_definition_id=self.id,
)
return [
CompletePatchOutput.from_model(validity_period)
for validity_period in ValidityPeriodsApi(
VardefClient.get_client(),
).list_validity_periods(
variable_definition_id=self.id,
)
]

@vardef_exception_handler
def list_patches(self) -> list[CompleteResponse]:
def list_patches(self) -> list[CompletePatchOutput]:
"""List all Patches for this Variable Definition."""
return PatchesApi(VardefClient.get_client()).list_patches(
variable_definition_id=self.id,
)
return [
CompletePatchOutput.from_model(patch)
for patch in PatchesApi(VardefClient.get_client()).list_patches(
variable_definition_id=self.id,
)
]

@vardef_exception_handler
def update_draft(
self,
update_draft: UpdateDraft,
) -> CompleteResponse:
) -> CompletePatchOutput:
"""Update this Variable definition.

Variable definition must have status 'DRAFT'.
Expand All @@ -71,14 +94,16 @@ def update_draft(
update_draft: The input with updated values.

Returns:
CompleteResponse: Updated Variable definition with all details.
CompletePatchOutput: Updated Variable definition with all details.
"""
return DraftVariableDefinitionsApi(
VardefClient.get_client(),
).update_variable_definition_by_id(
variable_definition_id=self.id,
active_group=config.get_active_group(),
update_draft=update_draft,
return CompletePatchOutput.from_model(
DraftVariableDefinitionsApi(
VardefClient.get_client(),
).update_variable_definition_by_id(
variable_definition_id=self.id,
active_group=config.get_active_group(),
update_draft=update_draft,
),
)

@vardef_exception_handler
Expand All @@ -102,26 +127,28 @@ def delete_draft(
return f"Variable {self.id} safely deleted"

@vardef_exception_handler
def get_patch(self, patch_id: int) -> CompleteResponse:
def get_patch(self, patch_id: int) -> CompletePatchOutput:
"""Get a single Patch by ID.

Args:
patch_id (int): The ID of the patch.

Returns:
CompleteResponse: The desired patch.
CompletePatchOutput: The desired patch.
"""
return PatchesApi(VardefClient.get_client()).get_patch(
variable_definition_id=self.id,
patch_id=patch_id,
return CompletePatchOutput.from_model(
PatchesApi(VardefClient.get_client()).get_patch(
variable_definition_id=self.id,
patch_id=patch_id,
),
)

@vardef_exception_handler
def create_patch(
self,
patch: Patch,
valid_from: date | None = None,
) -> CompleteResponse:
) -> CompletePatchOutput:
"""Create a new patch for this Variable definition.

Args:
Expand All @@ -130,23 +157,25 @@ def create_patch(
If value is None the patch is created in the last validity period.

Returns:
CompleteResponse: Variable definition with all details.
CompletePatchOutput: Variable definition with all details.

"""
return PatchesApi(
VardefClient.get_client(),
).create_patch(
variable_definition_id=self.id,
active_group=config.get_active_group(),
patch=patch,
valid_from=valid_from,
return CompletePatchOutput.from_model(
PatchesApi(
VardefClient.get_client(),
).create_patch(
variable_definition_id=self.id,
active_group=config.get_active_group(),
patch=patch,
valid_from=valid_from,
),
)

@vardef_exception_handler
def create_validity_period(
self,
validity_period: ValidityPeriod,
) -> CompleteResponse:
) -> CompletePatchOutput:
"""Create a new validity period for this Variable definition.

In order to create a new validity period input must contain updated
Expand All @@ -156,12 +185,14 @@ def create_validity_period(
validity_period: The input for new validity period

Returns:
CompleteResponse: Variable definition with all details.
CompletePatchOutput: Variable definition with all details.
"""
return ValidityPeriodsApi(
VardefClient.get_client(),
).create_validity_period(
variable_definition_id=self.id,
active_group=config.get_active_group(),
validity_period=validity_period,
return CompletePatchOutput.from_model(
ValidityPeriodsApi(
VardefClient.get_client(),
).create_validity_period(
variable_definition_id=self.id,
active_group=config.get_active_group(),
validity_period=validity_period,
),
)
22 changes: 10 additions & 12 deletions tests/variable_definitions/test_vardef.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
from dapla_metadata.variable_definitions.generated.vardef_client.configuration import (
Configuration,
)
from dapla_metadata.variable_definitions.generated.vardef_client.models.complete_response import (
CompleteResponse,
)
from dapla_metadata.variable_definitions.generated.vardef_client.models.draft import (
Draft,
)
Expand All @@ -25,6 +22,7 @@
VariableStatus,
)
from dapla_metadata.variable_definitions.vardef import Vardef
from dapla_metadata.variable_definitions.variable_definition import CompletePatchOutput
from dapla_metadata.variable_definitions.variable_definition import VariableDefinition
from tests.utils.constants import NOT_FOUND_STATUS
from tests.utils.constants import VARDEF_EXAMPLE_ACTIVE_GROUP
Expand All @@ -45,7 +43,7 @@ def test_list_variable_definitions_with_date_of_validity(
VardefClient.set_config(client_configuration)
assert isinstance(
Vardef.list_variable_definitions(date_of_validity=VARDEF_EXAMPLE_DATE)[0],
CompleteResponse,
CompletePatchOutput,
)


Expand Down Expand Up @@ -74,23 +72,23 @@ def test_list_patches(client_configuration: Configuration):
landbak = Vardef.get_variable_definition(
variable_definition_id=VARDEF_EXAMPLE_DEFINITION_ID,
)
assert isinstance(landbak.list_patches()[0], CompleteResponse)
assert isinstance(landbak.list_patches()[0], CompletePatchOutput)


def test_get_patch(client_configuration: Configuration):
VardefClient.set_config(client_configuration)
landbak = Vardef.get_variable_definition(
variable_definition_id=VARDEF_EXAMPLE_DEFINITION_ID,
)
assert isinstance(landbak.get_patch(1), CompleteResponse)
assert isinstance(landbak.get_patch(1), CompletePatchOutput)


def test_list_validity_periods(client_configuration: Configuration):
VardefClient.set_config(client_configuration)
landbak = Vardef.get_variable_definition(
variable_definition_id=VARDEF_EXAMPLE_DEFINITION_ID,
)
assert isinstance(landbak.list_validity_periods()[0], CompleteResponse)
assert isinstance(landbak.list_validity_periods()[0], CompletePatchOutput)


def test_create_draft(
Expand All @@ -103,7 +101,7 @@ def test_create_draft(
my_draft = Vardef.create_draft(
draft=draft,
)
assert isinstance(my_draft, CompleteResponse)
assert isinstance(my_draft, CompletePatchOutput)
assert my_draft.id is not None
assert my_draft.patch_id == 1
assert my_draft.variable_status == VariableStatus.DRAFT
Expand All @@ -118,7 +116,7 @@ def test_migrate_from_vardok(
my_draft = Vardef.migrate_from_vardok(
vardok_id="1607",
)
assert isinstance(my_draft, CompleteResponse)
assert isinstance(my_draft, CompletePatchOutput)
assert my_draft.id is not None
assert my_draft.patch_id == 1
assert my_draft.variable_status == VariableStatus.DRAFT
Expand All @@ -134,7 +132,7 @@ def test_update_draft(
my_draft = Vardef.get_variable_definition(
variable_definition_id=VARDEF_EXAMPLE_DEFINITION_ID,
)
assert isinstance(my_draft.update_draft(update_draft), CompleteResponse)
assert isinstance(my_draft.update_draft(update_draft), CompletePatchOutput)


def test_delete_draft(
Expand Down Expand Up @@ -167,7 +165,7 @@ def test_create_patch(
)
assert isinstance(
created_patch,
CompleteResponse,
CompletePatchOutput,
)
assert created_patch.patch_id == PATCH_ID

Expand All @@ -183,5 +181,5 @@ def test_create_validity_period(
my_variable = variable_definition
assert isinstance(
my_variable.create_validity_period(validity_period),
CompleteResponse,
CompletePatchOutput,
)
Loading
Loading