1
1
"""
2
2
Remote document loader using Requests.
3
-
4
3
.. module:: jsonld.documentloader.requests
5
4
:synopsis: Remote document loader using Requests
6
-
7
5
.. moduleauthor:: Dave Longley
8
6
.. moduleauthor:: Mike Johnson
9
7
.. moduleauthor:: Tim McNamara <tim.mcnamara@okfn.org>
10
8
.. moduleauthor:: Olaf Conradi <olaf@conradi.org>
11
9
"""
12
10
import string
11
+ import re
13
12
import urllib .parse as urllib_parse
14
13
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 )
16
15
17
16
18
17
def requests_document_loader (secure = False , ** kwargs ):
19
18
"""
20
19
Create a Requests document loader.
21
-
22
20
Can be used to setup extra Requests args such as verify, cert, timeout,
23
21
or others.
24
-
25
22
:param secure: require all requests to use HTTPS (default: False).
26
23
:param **kwargs: extra keyword args for Requests get() call.
27
-
28
24
:return: the RemoteDocument loader function.
29
25
"""
30
26
import requests
31
27
32
28
def loader (url , options = {}):
33
29
"""
34
30
Retrieves JSON-LD at the given URL.
35
-
36
31
:param url: the URL to retrieve.
37
-
38
32
:return: the RemoteDocument.
39
33
"""
40
34
try :
@@ -69,30 +63,31 @@ def loader(url, options={}):
69
63
'contentType' : content_type ,
70
64
'contextUrl' : None ,
71
65
'documentUrl' : response .url ,
72
- 'document' : response .json ()
73
66
}
74
67
link_header = response .headers .get ('link' )
75
68
if link_header :
76
69
linked_context = parse_link_header (link_header ).get (
77
70
LINK_HEADER_REL )
78
71
# only 1 related link header permitted
79
72
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" ]
89
82
linked_alternate = parse_link_header (link_header ).get ('alternate' )
90
83
# if not JSON-LD, alternate may point there
91
84
if (linked_alternate and
92
85
linked_alternate .get ('type' ) == 'application/ld+json' and
93
86
not re .match (r'^application\/(\w*\+)?json$' , content_type )):
94
87
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 ()
96
91
return doc
97
92
except JsonLdError as e :
98
93
raise e
@@ -102,4 +97,4 @@ def loader(url, options={}):
102
97
'jsonld.LoadDocumentError' , code = 'loading document failed' ,
103
98
cause = cause )
104
99
105
- return loader
100
+ return loader
0 commit comments