Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request JSON-LD from Link rel=alternate #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions lib/pyld/documentloader/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import string
import urllib.parse as urllib_parse

from pyld.jsonld import (JsonLdError, parse_link_header, LINK_HEADER_REL)

from pyld.jsonld import (JsonLdError, parse_link_header, prepend_base, LINK_HEADER_REL)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from pyld.jsonld import (JsonLdError, parse_link_header, prepend_base, LINK_HEADER_REL)
from pyld.jsonld import JsonLdError, parse_link_header, prepend_base, LINK_HEADER_REL

I believe the braces are redundant.


def requests_document_loader(secure=False, **kwargs):
"""
Expand All @@ -28,6 +27,7 @@ def requests_document_loader(secure=False, **kwargs):
:return: the RemoteDocument loader function.
"""
import requests
import re

def loader(url, options={}):
"""
Expand Down Expand Up @@ -69,7 +69,7 @@ def loader(url, options={}):
'contentType': content_type,
'contextUrl': None,
'documentUrl': response.url,
'document': response.json()
'document': response.json() if content_type in headers['Accept'] else None
}
link_header = response.headers.get('link')
if link_header:
Expand All @@ -92,7 +92,10 @@ def loader(url, options={}):
linked_alternate.get('type') == 'application/ld+json' and
not re.match(r'^application\/(\w*\+)?json$', content_type)):
doc['contentType'] = 'application/ld+json'
doc['documentUrl'] = jsonld.prepend_base(url, linked_alternate['target'])
doc['documentUrl'] = prepend_base(url, linked_alternate['target'])
if content_type not in headers['Accept']:
# Original was not JSON/JSON-LD; fetch linked JSON-LD
return loader(doc['documentUrl'], options=options)
return doc
except JsonLdError as e:
raise e
Expand Down