Skip to content
This repository was archived by the owner on Aug 26, 2024. It is now read-only.

Unique link keys #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions hal_codec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import uritemplate


__version__ = "1.0.2"
__version__ = "1.1.0"


def _get_string(item, key):
Expand Down Expand Up @@ -127,19 +127,16 @@ def _parse_link(data, base_url=None):
return Link(url=url, fields=fields)


def _map_to_coreapi_key(key):
# HAL uses 'rel' values to index links and nested resources.
if key.startswith('http://') or key.startswith('https://'):
# Fully qualified URL - just use last portion of the path.
return urlparse.urlsplit(key).path.split('/')[-1]
elif ':' in key:
# A curried 'rel' value. Use the named portion.
return key.split(':', 1)[1]
# A reserved 'rel' value, such as "next".
def _map_to_coreapi_key(key, curies):
if ':' in key:
prefix, suffix = key.split(':', 1)
curie = curies.get(prefix)
if curie is not None and curie['templated'] is True:
key = uritemplate.expand(curie['href'], rel=suffix)
return key


def _parse_document(data, base_url=None):
def _parse_document(data, base_url=None, curies=None):
links = _get_dict(data, '_links')
embedded = _get_dict(data, '_embedded')

Expand All @@ -149,12 +146,19 @@ def _parse_document(data, base_url=None):
title = _get_string(self, 'title')

content = {}
if curies is None:
curies = {}

if 'curies' in links:
for curie in links['curies']:
curies[curie['name']] = curie

for key, value in links.items():

if key in ('self', 'curies'):
continue

key = _map_to_coreapi_key(key)
key = _map_to_coreapi_key(key, curies)

if isinstance(value, list):
if value and 'name' in value[0]:
Expand All @@ -176,11 +180,12 @@ def _parse_document(data, base_url=None):

# Embedded resources.
for key, value in embedded.items():
key = _map_to_coreapi_key(key)
key = _map_to_coreapi_key(key, curies)
if isinstance(value, list):
content[key] = [_parse_document(item, base_url=url) for item in value]
content[key] = [_parse_document(item, base_url=url, curies=curies)
for item in value]
elif isinstance(value, dict):
content[key] = _parse_document(value, base_url=url)
content[key] = _parse_document(value, base_url=url, curies=curies)

# Data.
for key, value in data.items():
Expand Down
14 changes: 7 additions & 7 deletions tests/test_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@
url=u'/orders',
title='',
content={
u'admin': [
u'http://example.com/docs/rels/admin': [
Link(url=u'/admins/2'),
Link(url=u'/admins/5')
],
u'currentlyProcessing': 14,
u'order': [
u'http://example.com/docs/rels/order': [
Document(
url=u'/orders/123',
title='',
content={
u'currency': u'USD',
u'status': u'shipped',
u'total': 30.0,
u'basket': Link(url=u'/baskets/98712'),
u'customer': Link(url=u'/customers/7809')
u'http://example.com/docs/rels/basket': Link(url=u'/baskets/98712'),
u'http://example.com/docs/rels/customer': Link(url=u'/customers/7809')
}
),
Document(
Expand All @@ -73,13 +73,13 @@
u'currency': u'USD',
u'status': u'processing',
u'total': 20.0,
u'basket': Link(url=u'/baskets/97213'),
u'customer': Link(url=u'/customers/12369')
u'http://example.com/docs/rels/basket': Link(url=u'/baskets/97213'),
u'http://example.com/docs/rels/customer': Link(url=u'/customers/12369')
}
)
],
u'shippedToday': 20,
u'find': Link(url=u'/orders{?id}', fields=[Field(u'id', location='path')]),
u'http://example.com/docs/rels/find': Link(url=u'/orders{?id}', fields=[Field(u'id', location='path')]),
u'next': Link(url=u'/orders?page=2')
}
)
Expand Down