Skip to content

Commit

Permalink
refactor: allow differences between api object properties and slots (#…
Browse files Browse the repository at this point in the history
…421)

Allow differences between the classes slots and the API object
properties.
  • Loading branch information
jooola committed Jul 23, 2024
1 parent c517c85 commit 3a83ed7
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 61 deletions.
3 changes: 2 additions & 1 deletion hcloud/actions/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Action(BaseDomain):
STATUS_ERROR = "error"
"""Action Status error"""

__slots__ = (
__api_properties__ = (
"id",
"command",
"status",
Expand All @@ -41,6 +41,7 @@ class Action(BaseDomain):
"started",
"finished",
)
__slots__ = __api_properties__

def __init__(
self,
Expand Down
7 changes: 5 additions & 2 deletions hcloud/certificates/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Certificate(BaseDomain, DomainIdentityMixin):
:param status: ManagedCertificateStatus Current status of a type managed Certificate, always none for type uploaded Certificates
"""

__slots__ = (
__api_properties__ = (
"id",
"name",
"certificate",
Expand All @@ -44,6 +44,8 @@ class Certificate(BaseDomain, DomainIdentityMixin):
"type",
"status",
)
__slots__ = __api_properties__

TYPE_UPLOADED = "uploaded"
TYPE_MANAGED = "managed"

Expand Down Expand Up @@ -119,7 +121,8 @@ class CreateManagedCertificateResponse(BaseDomain):
Shows the progress of the certificate creation
"""

__slots__ = ("certificate", "action")
__api_properties__ = ("certificate", "action")
__slots__ = __api_properties__

def __init__(
self,
Expand Down
13 changes: 7 additions & 6 deletions hcloud/core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@


class BaseDomain:
__slots__ = ()
__api_properties__: tuple

@classmethod
def from_dict(cls, data: dict): # type: ignore[no-untyped-def]
"""
Build the domain object from the data dict.
"""
supported_data = {k: v for k, v in data.items() if k in cls.__slots__}
supported_data = {k: v for k, v in data.items() if k in cls.__api_properties__}
return cls(**supported_data)

def __repr__(self) -> str:
kwargs = [f"{key}={getattr(self, key)!r}" for key in self.__slots__] # type: ignore[var-annotated]
kwargs = [f"{key}={getattr(self, key)!r}" for key in self.__api_properties__] # type: ignore[var-annotated]
return f"{self.__class__.__qualname__}({', '.join(kwargs)})"


class DomainIdentityMixin:
__slots__ = ()

id: int | None
name: str | None
Expand Down Expand Up @@ -54,14 +53,15 @@ def has_id_or_name(self, id_or_name: int | str) -> bool:


class Pagination(BaseDomain):
__slots__ = (
__api_properties__ = (
"page",
"per_page",
"previous_page",
"next_page",
"last_page",
"total_entries",
)
__slots__ = __api_properties__

def __init__(
self,
Expand All @@ -81,7 +81,8 @@ def __init__(


class Meta(BaseDomain):
__slots__ = ("pagination",)
__api_properties__ = ("pagination",)
__slots__ = __api_properties__

def __init__(self, pagination: Pagination | None = None):
self.pagination = pagination
Expand Down
6 changes: 4 additions & 2 deletions hcloud/datacenters/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Datacenter(BaseDomain, DomainIdentityMixin):
:param server_types: :class:`DatacenterServerTypes <hcloud.datacenters.domain.DatacenterServerTypes>`
"""

__slots__ = ("id", "name", "description", "location", "server_types")
__api_properties__ = ("id", "name", "description", "location", "server_types")
__slots__ = __api_properties__

def __init__(
self,
Expand Down Expand Up @@ -47,7 +48,8 @@ class DatacenterServerTypes(BaseDomain):
All available for migration (change type) server types for this datacenter
"""

__slots__ = ("available", "supported", "available_for_migration")
__api_properties__ = ("available", "supported", "available_for_migration")
__slots__ = __api_properties__

def __init__(
self,
Expand Down
3 changes: 2 additions & 1 deletion hcloud/deprecation/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class DeprecationInfo(BaseDomain):
new servers with this image after the mentioned date.
"""

__slots__ = (
__api_properties__ = (
"announced",
"unavailable_after",
)
__slots__ = __api_properties__

def __init__(
self,
Expand Down
15 changes: 10 additions & 5 deletions hcloud/firewalls/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Firewall(BaseDomain, DomainIdentityMixin):
Point in time when the image was created
"""

__slots__ = ("id", "name", "labels", "rules", "applied_to", "created")
__api_properties__ = ("id", "name", "labels", "rules", "applied_to", "created")
__slots__ = __api_properties__

def __init__(
self,
Expand Down Expand Up @@ -66,14 +67,15 @@ class FirewallRule(BaseDomain):
Short description of the firewall rule
"""

__slots__ = (
__api_properties__ = (
"direction",
"port",
"protocol",
"source_ips",
"destination_ips",
"description",
)
__slots__ = __api_properties__

DIRECTION_IN = "in"
"""Firewall Rule Direction In"""
Expand Down Expand Up @@ -138,7 +140,8 @@ class FirewallResource(BaseDomain):
applied to.
"""

__slots__ = ("type", "server", "label_selector", "applied_to_resources")
__api_properties__ = ("type", "server", "label_selector", "applied_to_resources")
__slots__ = __api_properties__

TYPE_SERVER = "server"
"""Firewall Used By Type Server"""
Expand Down Expand Up @@ -177,7 +180,8 @@ class FirewallResourceAppliedToResources(BaseDomain):
:param server: Server the Firewall is applied to
"""

__slots__ = ("type", "server")
__api_properties__ = ("type", "server")
__slots__ = __api_properties__

def __init__(
self,
Expand Down Expand Up @@ -207,7 +211,8 @@ class CreateFirewallResponse(BaseDomain):
The Action which shows the progress of the Firewall Creation
"""

__slots__ = ("firewall", "actions")
__api_properties__ = ("firewall", "actions")
__slots__ = __api_properties__

def __init__(
self,
Expand Down
6 changes: 4 additions & 2 deletions hcloud/floating_ips/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FloatingIP(BaseDomain, DomainIdentityMixin):
Name of the Floating IP
"""

__slots__ = (
__api_properties__ = (
"id",
"type",
"description",
Expand All @@ -56,6 +56,7 @@ class FloatingIP(BaseDomain, DomainIdentityMixin):
"name",
"created",
)
__slots__ = __api_properties__

def __init__(
self,
Expand Down Expand Up @@ -95,7 +96,8 @@ class CreateFloatingIPResponse(BaseDomain):
The Action which shows the progress of the Floating IP Creation
"""

__slots__ = ("floating_ip", "action")
__api_properties__ = ("floating_ip", "action")
__slots__ = __api_properties__

def __init__(
self,
Expand Down
6 changes: 4 additions & 2 deletions hcloud/images/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Image(BaseDomain, DomainIdentityMixin):
User-defined labels (key-value pairs)
"""

__slots__ = (
__api_properties__ = (
"id",
"name",
"type",
Expand All @@ -70,6 +70,7 @@ class Image(BaseDomain, DomainIdentityMixin):
"created",
"deprecated",
)
__slots__ = __api_properties__

# pylint: disable=too-many-locals
def __init__(
Expand Down Expand Up @@ -120,7 +121,8 @@ class CreateImageResponse(BaseDomain):
The Action which shows the progress of the Floating IP Creation
"""

__slots__ = ("action", "image")
__api_properties__ = ("action", "image")
__slots__ = __api_properties__

def __init__(
self,
Expand Down
3 changes: 2 additions & 1 deletion hcloud/isos/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ class Iso(BaseDomain, DomainIdentityMixin):
deprecated. If it has a value, it is considered deprecated.
"""

__slots__ = (
__api_properties__ = (
"id",
"name",
"type",
"architecture",
"description",
"deprecation",
)
__slots__ = __api_properties__

def __init__(
self,
Expand Down
3 changes: 2 additions & 1 deletion hcloud/load_balancer_types/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LoadBalancerType(BaseDomain, DomainIdentityMixin):
"""

__slots__ = (
__api_properties__ = (
"id",
"name",
"description",
Expand All @@ -35,6 +35,7 @@ class LoadBalancerType(BaseDomain, DomainIdentityMixin):
"max_assigned_certificates",
"prices",
)
__slots__ = __api_properties__

def __init__(
self,
Expand Down
21 changes: 14 additions & 7 deletions hcloud/load_balancers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LoadBalancer(BaseDomain, DomainIdentityMixin):
Free Traffic for the current billing period in bytes
"""

__slots__ = (
__api_properties__ = (
"id",
"name",
"public_net",
Expand All @@ -69,6 +69,7 @@ class LoadBalancer(BaseDomain, DomainIdentityMixin):
"ingoing_traffic",
"included_traffic",
)
__slots__ = __api_properties__

# pylint: disable=too-many-locals
def __init__(
Expand Down Expand Up @@ -422,7 +423,8 @@ class PublicNetwork(BaseDomain):
:param enabled: boolean
"""

__slots__ = ("ipv4", "ipv6", "enabled")
__api_properties__ = ("ipv4", "ipv6", "enabled")
__slots__ = __api_properties__

def __init__(
self,
Expand All @@ -442,7 +444,8 @@ class IPv4Address(BaseDomain):
The IPv4 Address
"""

__slots__ = ("ip", "dns_ptr")
__api_properties__ = ("ip", "dns_ptr")
__slots__ = __api_properties__

def __init__(
self,
Expand All @@ -460,7 +463,8 @@ class IPv6Network(BaseDomain):
The IPv6 Network as CIDR Notation
"""

__slots__ = ("ip", "dns_ptr")
__api_properties__ = ("ip", "dns_ptr")
__slots__ = __api_properties__

def __init__(
self,
Expand All @@ -480,7 +484,8 @@ class PrivateNet(BaseDomain):
The main IP Address of the LoadBalancer in the Network
"""

__slots__ = ("network", "ip")
__api_properties__ = ("network", "ip")
__slots__ = __api_properties__

def __init__(
self,
Expand All @@ -500,7 +505,8 @@ class CreateLoadBalancerResponse(BaseDomain):
Shows the progress of the Load Balancer creation
"""

__slots__ = ("load_balancer", "action")
__api_properties__ = ("load_balancer", "action")
__slots__ = __api_properties__

def __init__(
self,
Expand All @@ -525,7 +531,8 @@ class GetMetricsResponse(BaseDomain):
:param metrics: The Load Balancer metrics
"""

__slots__ = ("metrics",)
__api_properties__ = ("metrics",)
__slots__ = __api_properties__

def __init__(
self,
Expand Down
3 changes: 2 additions & 1 deletion hcloud/locations/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Location(BaseDomain, DomainIdentityMixin):
Name of network zone this location resides in
"""

__slots__ = (
__api_properties__ = (
"id",
"name",
"description",
Expand All @@ -34,6 +34,7 @@ class Location(BaseDomain, DomainIdentityMixin):
"longitude",
"network_zone",
)
__slots__ = __api_properties__

def __init__(
self,
Expand Down
3 changes: 2 additions & 1 deletion hcloud/metrics/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ class Metrics(BaseDomain):
step: float
time_series: TimeSeries

__slots__ = (
__api_properties__ = (
"start",
"end",
"step",
"time_series",
)
__slots__ = __api_properties__

def __init__(
self,
Expand Down
Loading

0 comments on commit 3a83ed7

Please sign in to comment.