Skip to content

Commit 07a9d52

Browse files
authored
feat: adds client dependency injection to model (#8)
* feat: adds dependency client injection * style: Improves code readability with black
1 parent 172d86a commit 07a9d52

11 files changed

Lines changed: 178 additions & 153 deletions

File tree

hostingde/hostingde.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _request(self, url: str, model: Optional[Model] = None, **kwargs: dict) -> R
5555
if error_check.get('status', 'error') == 'error':
5656
raise ApiException(error_check)
5757
except json.decoder.JSONDecodeError:
58-
raise ClientException('Error while reading response from server. ' 'Is your endpoint configured correctly?')
58+
raise ClientException('Error while reading response from server. Is your endpoint configured correctly?')
5959

6060
return response
6161

hostingde/model/__init__.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Any, Optional, Type, TypeVar
22

33
import marshmallow_dataclass
4-
from marshmallow import post_dump, Schema
4+
from marshmallow import post_dump, post_load, Schema
55
from marshmallow.fields import Field
66

77
import hostingde
@@ -26,6 +26,11 @@ def on_bind_field(self, field_name: str, field_obj: Field) -> None:
2626
"""Use lower-camel-casing as external representation"""
2727
field_obj.data_key = camelcase(field_obj.data_key or field_name)
2828

29+
@post_load
30+
def post_load(self, data: Any, **kwargs: Any) -> Any:
31+
data["context"] = self.context.get('client', None)
32+
return data
33+
2934
@post_dump
3035
def remove_skip_values(self, data: dict, **kwargs: dict) -> dict:
3136
"""
@@ -69,18 +74,6 @@ def __init__(self, **kwargs: Any):
6974
if "context" in kwargs and isinstance(kwargs["context"], hostingde.HostingDeClient):
7075
self.client = kwargs["context"]
7176

72-
def get_client(self) -> 'hostingde.HostingDeClient':
73-
"""
74-
Get the client used to create this object.
75-
76-
Throws an attribute error if no client is set.
77-
:return: an initialized client
78-
"""
79-
if not self.client:
80-
raise AttributeError("Context not set")
81-
82-
return self.client
83-
8477
@classmethod
8578
def from_json(cls: Type[T], data: dict, client: 'hostingde.HostingDeClient' = None) -> T:
8679
"""
@@ -89,7 +82,6 @@ def from_json(cls: Type[T], data: dict, client: 'hostingde.HostingDeClient' = No
8982
:param client: The client to use for this object
9083
:return: New instance of the class
9184
"""
92-
inst = marshmallow_dataclass.class_schema(cls, base_schema=CamelCaseSchema)().load(data)
93-
inst.client = client
94-
95-
return inst
85+
return marshmallow_dataclass.class_schema(cls, base_schema=CamelCaseSchema)(context={'client': client}).load(
86+
data
87+
)

hostingde/model/account.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Optional
2+
from typing import Any, Optional
33

44
from hostingde.model import Model
55

@@ -17,12 +17,13 @@ class Account(Model):
1717
name: Optional[str]
1818
id: Optional[str]
1919

20-
def __init__(self, name: Optional[str], id: Optional[str]):
20+
def __init__(self, name: Optional[str], id: Optional[str], **kwargs: Any):
2121
"""
2222
The Account object a object.
2323
:param name: Namw of this Account
2424
2525
:param id: ID of this Account.
2626
"""
27+
super().__init__(**kwargs)
2728
self.id: Optional[str] = id
2829
self.name: Optional[str] = name

hostingde/model/job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(
5858
:param status: The current status of the job, e.g. 'inProgress', 'successfull'
5959
:param kwargs:
6060
"""
61-
super().__init__()
61+
super().__init__(**kwargs)
6262
self.account_id = account_id
6363
self.action = action
6464
self.add_date = add_date

hostingde/model/record.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def __init__(
6161
ttl: Optional[int] = None,
6262
priority: Optional[int] = None,
6363
last_change_date: Optional[str] = None,
64+
**kwargs: Any,
6465
):
6566
"""
6667
The DNS Record object is part of a zone. It is used to manage DNS resource records.
@@ -94,7 +95,7 @@ def __init__(
9495
9596
:param last_change_date: Date and time of last record modification
9697
"""
97-
super().__init__()
98+
super().__init__(**kwargs)
9899
self.id = id
99100
self.zone_id = zone_id
100101
self.record_template_id = record_template_id

hostingde/model/soa_values.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass, field
2+
from typing import Any
23

34
from hostingde.model import Model
45

@@ -19,6 +20,7 @@ def __init__(
1920
expire: int = 3600000,
2021
ttl: int = 172800,
2122
negative_ttl: int = 3600,
23+
**kwargs: Any
2224
):
2325
"""
2426
The SOA values object contains the time (seconds) used in a zone's SOA record. The maximum number of seconds is
@@ -31,7 +33,7 @@ def __init__(
3133
:param ttl: TTL for the SOA record. Default: 172800, minimum: 60.
3234
:param negative_ttl: Negative TTL for the SOA record. Default: 3600, minimum: 60.
3335
"""
34-
super().__init__()
36+
super().__init__(**kwargs)
3537
self.refresh: int = refresh
3638
self.retry: int = retry
3739
self.expire: int = expire

hostingde/model/sort.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from dataclasses import dataclass
22
from enum import Enum
3+
from typing import Any
34

45
from marshmallow_enum import EnumField
56

@@ -20,13 +21,13 @@ class SortConfiguration(Model):
2021
field: str
2122
order: SortOrder = EnumField(SortOrder)
2223

23-
def __init__(self, field: str, order: SortOrder):
24+
def __init__(self, field: str, order: SortOrder, **kwargs: Any):
2425
"""
2526
The field you want to order on.
2627
2728
:param field: The field name
2829
:param order: The ordering of the results. Either 'ASC' or 'DESC'
2930
"""
30-
super().__init__()
31+
super().__init__(**kwargs)
3132
self.field: str = field
3233
self.order: SortOrder = order

hostingde/model/zone.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from dataclasses import dataclass, field
1+
from dataclasses import field
22
from typing import List
33

4+
from marshmallow_dataclass import dataclass
5+
46
from hostingde.model import Model
57
from hostingde.model.record import Record
68
from hostingde.model.zone_config import ZoneConfig
@@ -15,5 +17,8 @@ class Zone(Model):
1517
zone_config: ZoneConfig
1618
records: List[Record] = field(default_factory=list)
1719

20+
def __init__(self, **kwargs):
21+
super().__init__(**kwargs)
22+
1823
def __str__(self):
1924
return f"Name: {self.zone_config.name}; Type: {self.zone_config.type.value}; Status: {self.zone_config.status}"

hostingde/model/zone_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __init__(
124124
:param soa_values: Values for the SOA record of the zone. If it is left empty an SOA record with default values
125125
will be created.
126126
"""
127-
super().__init__()
127+
super().__init__(**kwargs)
128128
self.id: Optional[str] = id
129129
self.account_id: Optional[str] = account_id
130130
self.status: Optional[str] = status

0 commit comments

Comments
 (0)