Skip to content

Commit 4639b16

Browse files
committed
Ignore unknown fields in API response
1 parent 3013f3b commit 4639b16

11 files changed

Lines changed: 92 additions & 4 deletions

File tree

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
History
33
=======
44

5+
1.0.1 (2019-02-21)
6+
------------------
7+
8+
Fix: Ignore unknown fields in API response instead of raising an error
9+
510
1.0.0 (2019-02-21)
611
------------------
712

hcloud/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = '1.0.0'
1+
VERSION = '1.0.1'

hcloud/actions/domain.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Action(BaseDomain):
3535
"error",
3636
)
3737

38+
supported_fields = ("started", "finished")
39+
3840
def __init__(
3941
self,
4042
id,

hcloud/core/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def __init__(self, client, data={}, complete=True):
9393
"""
9494
self._client = client
9595
self.complete = complete
96-
self.data_model = self.model(**data)
96+
self.data_model = self.model.from_dict(data)
9797

9898
def __getattr__(self, name):
9999
"""Allow magical access to the properties of the model

hcloud/core/domain.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
class BaseDomain(object):
66
__slots__ = ()
77

8+
supported_fields = ()
9+
10+
@classmethod
11+
def get_supported_fields(cls):
12+
return set(cls.__slots__ + cls.supported_fields)
13+
14+
@classmethod
15+
def from_dict(cls, data):
16+
supported_fields = cls.get_supported_fields()
17+
supported_data = {k: v for k, v in data.items() if k in supported_fields}
18+
return cls(**supported_data)
19+
820

921
class DomainIdentityMixin(object):
1022
__slots__ = ()

hcloud/images/domain.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class Image(BaseDomain, DomainIdentityMixin):
5959
"labels"
6060
)
6161

62+
supported_fields = ("created", "deprecated")
63+
6264
def __init__(
6365
self,
6466
id=None,

hcloud/isos/domain.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Iso(BaseDomain, DomainIdentityMixin):
2626
"description"
2727
)
2828

29+
supported_fields = ("deprecated", )
30+
2931
def __init__(
3032
self,
3133
id=None,

hcloud/servers/domain.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class Server(BaseDomain):
6262

6363
created = ISODateTime()
6464

65+
supported_fields = ("created", )
66+
6567
def __init__(
6668
self,
6769
id,

hcloud/volumes/domain.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Volume(BaseDomain, DomainIdentityMixin):
4444
"status"
4545
)
4646

47+
supported_fields = ("created", )
48+
4749
def __init__(
4850
self,
4951
id,

tests/unit/core/test_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
import pytest
33

44
from hcloud.core.client import BoundModelBase, ClientEntityBase, GetEntityByNameMixin
5-
from hcloud.core.domain import add_meta_to_result
5+
from hcloud.core.domain import add_meta_to_result, BaseDomain
66

77

88
class TestBoundModelBase():
99

1010
@pytest.fixture()
1111
def bound_model_class(self):
12-
class Model(object):
12+
class Model(BaseDomain):
1313
__slots__ = ("id", "name", "description")
1414

1515
def __init__(self, id, name="", description=""):

0 commit comments

Comments
 (0)