Skip to content

Commit

Permalink
Add support to linked_data
Browse files Browse the repository at this point in the history
All requests returns related entities within the payload
  • Loading branch information
joaodaher committed May 22, 2019
1 parent 82659a4 commit 17048d9
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions glassfrog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
class BaseModel:
_RESOURCE_NAME = None

def __init__(self, data):
def __init__(self, data, linked_data=None):
if not isinstance(data, dict):
raise exceptions.UnexpectedDataFormat()

self._data = data
self._linked_data = linked_data

@property
def id(self):
Expand All @@ -26,32 +27,52 @@ def _get(self, value):
def _build_item_from_link(self, link_name, model_klass):
links = self._get('links')
item_id = links[link_name]
return model_klass.get(id=item_id)
if item_id:
try:
return model_klass.get(id=item_id)
except exceptions.UnsupportedModelException:
return model_klass.build(id=item_id, linked_data=self._linked_data)
else:
return None

def _build_items_from_link(self, link_name, model_klass):
links = self._get('links')
for item_id in links[link_name]:
yield model_klass.get(id=item_id)
try:
yield model_klass.get(id=item_id)
except exceptions.UnsupportedModelException:
yield model_klass.build(id=item_id, linked_data=self._linked_data)

@classmethod
def build(cls, id, linked_data):
if linked_data:
for data in linked_data[cls._RESOURCE_NAME]:
if data['id'] == id:
return cls(data=data, linked_data=None)
return cls(data={'id': id})

@classmethod
def get(cls, id):
data = GlassFrogClient.get(resource=cls._RESOURCE_NAME, id=id)
return cls(data=data[cls._RESOURCE_NAME][0])
linked_data = data.get('linked', None)
return cls(data=data[cls._RESOURCE_NAME][0], linked_data=linked_data)

@classmethod
def list(cls):
data = GlassFrogClient.get(resource=cls._RESOURCE_NAME)
linked_data = data.get('linked', None)
for item in data[cls._RESOURCE_NAME]:
yield cls(data=item)
yield cls(data=item, linked_data=linked_data)

def _detail(self, resource_class):
data = GlassFrogClient.get(
resource=resource_class._RESOURCE_NAME,
id=self.id,
from_resource=self._RESOURCE_NAME,
)
linked_data = data.get('linked', None)
for item in data[resource_class._RESOURCE_NAME]:
yield resource_class(data=item)
yield resource_class(data=item, linked_data=linked_data)


class UnsupportedModelMixin:
Expand Down Expand Up @@ -85,7 +106,7 @@ def strategy(self):
@property
def organization(self):
organization_id = self._get('organization_id')
return Organization.get(id=organization_id)
return Organization.build(id=organization_id, linked_data=self._linked_data)

@property
def roles(self):
Expand Down Expand Up @@ -164,7 +185,7 @@ def short_name(self):
@property
def organization(self):
organization_id = self._get('organization_id')
return Organization.get(id=organization_id)
return Organization.build(id=organization_id, linked_data=self._linked_data)

@property
def is_core(self):
Expand Down

0 comments on commit 17048d9

Please sign in to comment.