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
6 changes: 2 additions & 4 deletions hcloud/actions/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING

from dateutil.parser import isoparse

from .._exceptions import HCloudException
from ..core import BaseDomain

Expand Down Expand Up @@ -59,8 +57,8 @@ def __init__(

self.status = status
self.progress = progress
self.started = isoparse(started) if started else None
self.finished = isoparse(finished) if finished else None
self.started = self._parse_datetime(started)
self.finished = self._parse_datetime(finished)
self.resources = resources
self.error = error

Expand Down
8 changes: 3 additions & 5 deletions hcloud/certificates/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -69,9 +67,9 @@ def __init__(
self.certificate = certificate
self.domain_names = domain_names
self.fingerprint = fingerprint
self.not_valid_before = isoparse(not_valid_before) if not_valid_before else None
self.not_valid_after = isoparse(not_valid_after) if not_valid_after else None
self.created = isoparse(created) if created else None
self.not_valid_before = self._parse_datetime(not_valid_before)
self.not_valid_after = self._parse_datetime(not_valid_after)
self.created = self._parse_datetime(created)
self.labels = labels
self.status = status

Expand Down
15 changes: 14 additions & 1 deletion hcloud/core/domain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

from typing import Any
from datetime import datetime
from typing import Any, overload

from dateutil.parser import isoparse


class BaseDomain:
Expand All @@ -27,6 +30,16 @@ def __eq__(self, other: Any) -> bool:
return False
return True

@overload
def _parse_datetime(self, value: str) -> datetime: ...
@overload
def _parse_datetime(self, value: None) -> None: ...

def _parse_datetime(self, value: str | None) -> datetime | None:
if value is None:
return None
return isoparse(value)


class DomainIdentityMixin:

Expand Down
8 changes: 2 additions & 6 deletions hcloud/deprecation/domain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from dateutil.parser import isoparse

from ..core import BaseDomain


Expand All @@ -28,7 +26,5 @@ def __init__(
announced: str | None = None,
unavailable_after: str | None = None,
):
self.announced = isoparse(announced) if announced else None
self.unavailable_after = (
isoparse(unavailable_after) if unavailable_after else None
)
self.announced = self._parse_datetime(announced)
self.unavailable_after = self._parse_datetime(unavailable_after)
4 changes: 1 addition & 3 deletions hcloud/firewalls/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING, Any

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -46,7 +44,7 @@ def __init__(
self.rules = rules
self.applied_to = applied_to
self.labels = labels
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)


class FirewallRule(BaseDomain):
Expand Down
4 changes: 1 addition & 3 deletions hcloud/floating_ips/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -83,7 +81,7 @@ def __init__(
self.blocked = blocked
self.protection = protection
self.labels = labels
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)
self.name = name


Expand Down
6 changes: 2 additions & 4 deletions hcloud/images/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -96,11 +94,11 @@ def __init__(
self.id = id
self.name = name
self.type = type
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)
self.description = description
self.image_size = image_size
self.disk_size = disk_size
self.deprecated = isoparse(deprecated) if deprecated else None
self.deprecated = self._parse_datetime(deprecated)
self.bound_to = bound_to
self.os_flavor = os_flavor
self.os_version = os_version
Expand Down
4 changes: 1 addition & 3 deletions hcloud/load_balancers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import warnings
from typing import TYPE_CHECKING, Any, Literal

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -93,7 +91,7 @@ def __init__(
):
self.id = id
self.name = name
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)
self.public_net = public_net
self.private_net = private_net
self.location = location
Expand Down
6 changes: 2 additions & 4 deletions hcloud/metrics/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from datetime import datetime
from typing import Literal

from dateutil.parser import isoparse

from ..core import BaseDomain

TimeSeries = dict[str, dict[Literal["values"], list[tuple[float, str]]]]
Expand Down Expand Up @@ -41,7 +39,7 @@ def __init__(
step: float,
time_series: TimeSeries,
):
self.start = isoparse(start)
self.end = isoparse(end)
self.start = self._parse_datetime(start)
self.end = self._parse_datetime(end)
self.step = step
self.time_series = time_series
4 changes: 1 addition & 3 deletions hcloud/networks/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import warnings
from typing import TYPE_CHECKING

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -65,7 +63,7 @@ def __init__(
):
self.id = id
self.name = name
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)
self.ip_range = ip_range
self.subnets = subnets
self.routes = routes
Expand Down
4 changes: 1 addition & 3 deletions hcloud/placement_groups/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -50,7 +48,7 @@ def __init__(
self.labels = labels
self.servers = servers
self.type = type
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)


class CreatePlacementGroupResponse(BaseDomain):
Expand Down
4 changes: 1 addition & 3 deletions hcloud/primary_ips/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -84,7 +82,7 @@ def __init__(
self.blocked = blocked
self.protection = protection
self.labels = labels
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)
self.name = name
self.assignee_id = assignee_id
self.assignee_type = assignee_type
Expand Down
4 changes: 1 addition & 3 deletions hcloud/servers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING, Literal

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -135,7 +133,7 @@ def __init__(
self.id = id
self.name = name
self.status = status
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)
self.public_net = public_net
self.server_type = server_type
self.datacenter = datacenter
Expand Down
4 changes: 1 addition & 3 deletions hcloud/ssh_keys/domain.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin


Expand Down Expand Up @@ -46,4 +44,4 @@ def __init__(
self.fingerprint = fingerprint
self.public_key = public_key
self.labels = labels
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)
8 changes: 3 additions & 5 deletions hcloud/storage_boxes/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING, Any, Literal

from dateutil.parser import isoparse

from ..actions import BoundAction
from ..core import BaseDomain, DomainIdentityMixin
from ..locations import BoundLocation, Location
Expand Down Expand Up @@ -82,7 +80,7 @@ def __init__(
self.access_settings = access_settings
self.stats = stats
self.status = status
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)


class StorageBoxAccessSettings(BaseDomain):
Expand Down Expand Up @@ -285,7 +283,7 @@ def __init__(
self.is_automatic = is_automatic
self.labels = labels
self.storage_box = storage_box
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)
self.stats = stats


Expand Down Expand Up @@ -386,7 +384,7 @@ def __init__(
self.access_settings = access_settings
self.labels = labels
self.storage_box = storage_box
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)


class StorageBoxSubaccountAccessSettings(BaseDomain):
Expand Down
4 changes: 1 addition & 3 deletions hcloud/volumes/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -77,7 +75,7 @@ def __init__(
self.id = id
self.name = name
self.server = server
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)
self.location = location
self.size = size
self.linux_device = linux_device
Expand Down
10 changes: 2 additions & 8 deletions hcloud/zones/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import TYPE_CHECKING, Any, Literal, TypedDict

from dateutil.parser import isoparse

from ..core import BaseDomain, DomainIdentityMixin

if TYPE_CHECKING:
Expand Down Expand Up @@ -78,7 +76,7 @@ def __init__(
):
self.id = id
self.name = name
self.created = isoparse(created) if created else None
self.created = self._parse_datetime(created)
self.mode = mode
self.ttl = ttl
self.labels = labels
Expand Down Expand Up @@ -185,11 +183,7 @@ def __init__(
):
self.assigned = assigned
self.delegated = delegated
self.delegation_last_check = (
isoparse(delegation_last_check)
if delegation_last_check is not None
else None
)
self.delegation_last_check = self._parse_datetime(delegation_last_check)
self.delegation_status = delegation_status


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/core/test_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ActionDomain(BaseDomain, DomainIdentityMixin):
def __init__(self, id, name="name1", started=None):
self.id = id
self.name = name
self.started = isoparse(started) if started else None
self.started = self._parse_datetime(started)


class SomeOtherDomain(BaseDomain):
Expand Down