Skip to content

Commit

Permalink
Merge pull request #50 from vertical-knowledge/hal-embedded-links
Browse files Browse the repository at this point in the history
Hal embedded links
Embedded resources are now 100% valid resources. In other words, there
is no difference between top level resources and embedded resources
  • Loading branch information
timmartin19 committed Nov 23, 2015
2 parents cbf02c4 + 332e149 commit 26f81b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
29 changes: 20 additions & 9 deletions ripozo/adapters/hal.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,32 @@ def formatted_body(self):
:return: The response body for the resource.
:rtype: unicode
"""
resource_url = self.combine_base_url_with_resource_url(self.resource.url)
parent_properties = self.resource.properties.copy()
response = self._construct_resource(self.resource)
return json.dumps(response)

embedded, links = self.generate_relationship(self.resource.related_resources)
embedded2, links2 = self.generate_relationship(self.resource.linked_resources)
def _construct_resource(self, resource):
"""
Constructs a full resource. This can be used
for either the primary resource or embedded resources
:param ripozo.resources.resource_base.ResourceBase resource: The resource
that will be constructed.
:return: The resource represented according to the
Hal specification
:rtype: dict
"""
resource_url = self.combine_base_url_with_resource_url(resource.url)
parent_properties = resource.properties.copy()

embedded, links = self.generate_relationship(resource.related_resources)
embedded2, links2 = self.generate_relationship(resource.linked_resources)
embedded.update(embedded2)
links.update(links2)
links.update(dict(self=dict(href=resource_url)))

response = dict(_links=links, _embedded=embedded)
response.update(parent_properties)
return json.dumps(response)
return response

def generate_relationship(self, relationship_list):
"""
Expand Down Expand Up @@ -89,10 +103,7 @@ def _generate_relationship(self, relationship, embedded):
if not relationship.has_all_pks:
return
if embedded:
relationship_url = self.combine_base_url_with_resource_url(relationship.url)
properties = relationship.properties.copy()
properties.update(dict(_links=dict(self=dict(href=relationship_url))))
return properties
return self._construct_resource(relationship)
else:
return dict(href=relationship.url)

Expand Down
7 changes: 6 additions & 1 deletion ripozo_tests/unit/dispatch/adapters/hal.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def test_embedded_relationships(self):
class Fake(ResourceBase):
pass
props = dict(
_embedded={},
_links=dict(self=dict(href='/fake')),
val=1, val2=2
)
Expand All @@ -85,10 +86,12 @@ def test_list_relationships(self):
class Fake(ResourceBase):
pass
props = dict(
_embedded={},
_links=dict(self=dict(href='/fake')),
val=1, val2=2
)
props2 = dict(
_embedded={},
_links=dict(self=dict(href='/fake')),
val=3, val4=4
)
Expand Down Expand Up @@ -118,10 +121,12 @@ class Fake(ResourceBase):

adapter = HalAdapter(None)
props1 = dict(
_embedded={},
_links=dict(self=dict(href='/fake/1')),
id=1, val=2
)
props2 = dict(
_embedded={},
_links=dict(self=dict(href='/fake')),
val=1
)
Expand All @@ -142,7 +147,7 @@ class Fake(ResourceBase):
res = Fake(properties=dict(x=1, y=2))

expected_res = res.properties.copy()
expected_res.update(dict(_links=dict(self=dict(href=res.url))))
expected_res.update(dict(_links=dict(self=dict(href=res.url)), _embedded={}))
relation_list = [(res, 'res', True,)]
adapter = HalAdapter(Fake())
embedded, links = adapter.generate_relationship(relation_list)
Expand Down
2 changes: 1 addition & 1 deletion ripozo_tests/unit/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def test_construct_self_referential(self):
when constructing a resource
"""
class Resource(ResourceBase):
_pks = ['id']
pks = 'id',
_relationships = [
Relationship('child', relation='Resource', embedded=True),
Relationship('parent', relation='Resource')
Expand Down

0 comments on commit 26f81b0

Please sign in to comment.