Skip to content

Commit 0d8b2f6

Browse files
committed
Forward changes from JulienPalard's digitalbazaar#141
1 parent 316fbc2 commit 0d8b2f6

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

lib/pyld/documentloader/requests.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
11
"""
22
Remote document loader using Requests.
3-
43
.. module:: jsonld.documentloader.requests
54
:synopsis: Remote document loader using Requests
6-
75
.. moduleauthor:: Dave Longley
86
.. moduleauthor:: Mike Johnson
97
.. moduleauthor:: Tim McNamara <tim.mcnamara@okfn.org>
108
.. moduleauthor:: Olaf Conradi <olaf@conradi.org>
119
"""
1210
import string
11+
import re
1312
import urllib.parse as urllib_parse
1413

15-
from pyld.jsonld import (JsonLdError, parse_link_header, LINK_HEADER_REL)
14+
from pyld.jsonld import (JsonLdError, parse_link_header, LINK_HEADER_REL, prepend_base)
1615

1716

1817
def requests_document_loader(secure=False, **kwargs):
1918
"""
2019
Create a Requests document loader.
21-
2220
Can be used to setup extra Requests args such as verify, cert, timeout,
2321
or others.
24-
2522
:param secure: require all requests to use HTTPS (default: False).
2623
:param **kwargs: extra keyword args for Requests get() call.
27-
2824
:return: the RemoteDocument loader function.
2925
"""
3026
import requests
3127

3228
def loader(url, options={}):
3329
"""
3430
Retrieves JSON-LD at the given URL.
35-
3631
:param url: the URL to retrieve.
37-
3832
:return: the RemoteDocument.
3933
"""
4034
try:
@@ -69,30 +63,31 @@ def loader(url, options={}):
6963
'contentType': content_type,
7064
'contextUrl': None,
7165
'documentUrl': response.url,
72-
'document': response.json()
7366
}
7467
link_header = response.headers.get('link')
7568
if link_header:
7669
linked_context = parse_link_header(link_header).get(
7770
LINK_HEADER_REL)
7871
# only 1 related link header permitted
7972
if linked_context and content_type != 'application/ld+json':
80-
if isinstance(linked_context, list):
81-
raise JsonLdError(
82-
'URL could not be dereferenced, '
83-
'it has more than one '
84-
'associated HTTP Link Header.',
85-
'jsonld.LoadDocumentError',
86-
{'url': url},
87-
code='multiple context link headers')
88-
doc['contextUrl'] = linked_context['target']
73+
if isinstance(linked_context, list):
74+
raise JsonLdError(
75+
"URL could not be dereferenced, "
76+
"it has more than one "
77+
"associated HTTP Link Header.",
78+
"jsonld.LoadDocumentError",
79+
{"url": url},
80+
code="multiple context link headers")
81+
doc["contextUrl"] = linked_context["target"]
8982
linked_alternate = parse_link_header(link_header).get('alternate')
9083
# if not JSON-LD, alternate may point there
9184
if (linked_alternate and
9285
linked_alternate.get('type') == 'application/ld+json' and
9386
not re.match(r'^application\/(\w*\+)?json$', content_type)):
9487
doc['contentType'] = 'application/ld+json'
95-
doc['documentUrl'] = jsonld.prepend_base(url, linked_alternate['target'])
88+
doc['documentUrl'] = prepend_base(url, linked_alternate['target'])
89+
return loader(doc['documentUrl'], options=options)
90+
doc["document"] = response.json()
9691
return doc
9792
except JsonLdError as e:
9893
raise e
@@ -102,4 +97,4 @@ def loader(url, options={}):
10297
'jsonld.LoadDocumentError', code='loading document failed',
10398
cause=cause)
10499

105-
return loader
100+
return loader

0 commit comments

Comments
 (0)