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

Commit fd8361a

Browse files
author
Florent Cayré
committed
Generate unique keys when parsing document links (API change)
- do no more use URL path as a key, but the complete URL - expand curies to the full URL to avoid collisions too Note that this changes the API, as the keys to retrieve the links of a document have changed.
1 parent 26131f3 commit fd8361a

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

hal_codec/__init__.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,16 @@ def _parse_link(data, base_url=None):
127127
return Link(url=url, fields=fields)
128128

129129

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

141138

142-
def _parse_document(data, base_url=None):
139+
def _parse_document(data, base_url=None, curies=None):
143140
links = _get_dict(data, '_links')
144141
embedded = _get_dict(data, '_embedded')
145142

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

151148
content = {}
149+
if curies is None:
150+
curies = {}
152151

153152
for key, value in links.items():
154-
if key in ('self', 'curies'):
153+
154+
if key == 'self':
155+
continue
156+
elif key == 'curies':
157+
for curie in value:
158+
curies[curie['name']] = curie
155159
continue
156160

157-
key = _map_to_coreapi_key(key)
161+
key = _map_to_coreapi_key(key, curies)
158162

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

177181
# Embedded resources.
178182
for key, value in embedded.items():
179-
key = _map_to_coreapi_key(key)
183+
key = _map_to_coreapi_key(key, curies)
180184
if isinstance(value, list):
181-
content[key] = [_parse_document(item, base_url=url) for item in value]
185+
content[key] = [_parse_document(item, base_url=url, curies=curies)
186+
for item in value]
182187
elif isinstance(value, dict):
183-
content[key] = _parse_document(value, base_url=url)
188+
content[key] = _parse_document(value, base_url=url, curies=curies)
184189

185190
# Data.
186191
for key, value in data.items():

tests/test_codec.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,21 @@
4949
url=u'/orders',
5050
title='',
5151
content={
52-
u'admin': [
52+
u'http://example.com/docs/rels/admin': [
5353
Link(url=u'/admins/2'),
5454
Link(url=u'/admins/5')
5555
],
5656
u'currentlyProcessing': 14,
57-
u'order': [
57+
u'http://example.com/docs/rels/order': [
5858
Document(
5959
url=u'/orders/123',
6060
title='',
6161
content={
6262
u'currency': u'USD',
6363
u'status': u'shipped',
6464
u'total': 30.0,
65-
u'basket': Link(url=u'/baskets/98712'),
66-
u'customer': Link(url=u'/customers/7809')
65+
u'http://example.com/docs/rels/basket': Link(url=u'/baskets/98712'),
66+
u'http://example.com/docs/rels/customer': Link(url=u'/customers/7809')
6767
}
6868
),
6969
Document(
@@ -73,13 +73,13 @@
7373
u'currency': u'USD',
7474
u'status': u'processing',
7575
u'total': 20.0,
76-
u'basket': Link(url=u'/baskets/97213'),
77-
u'customer': Link(url=u'/customers/12369')
76+
u'http://example.com/docs/rels/basket': Link(url=u'/baskets/97213'),
77+
u'http://example.com/docs/rels/customer': Link(url=u'/customers/12369')
7878
}
7979
)
8080
],
8181
u'shippedToday': 20,
82-
u'find': Link(url=u'/orders{?id}', fields=[Field(u'id', location='path')]),
82+
u'http://example.com/docs/rels/find': Link(url=u'/orders{?id}', fields=[Field(u'id', location='path')]),
8383
u'next': Link(url=u'/orders?page=2')
8484
}
8585
)

0 commit comments

Comments
 (0)