Skip to content
This repository has been archived by the owner on Nov 8, 2019. It is now read-only.

Commit

Permalink
update jsonld processor to support grequests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinxin90 committed Dec 5, 2017
1 parent 27d96b2 commit a131c60
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions biothings_explorer/jsonld_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import re
import requests
import grequests
from collections import defaultdict

from .utils import readFile
Expand All @@ -10,7 +11,7 @@
t = jsonld.JsonLdProcessor()


def jsonld2nquads(jsonld_doc):
def jsonld2nquads(jsonld_doc, mode='request'):
"""
Given a JSON-LD annotated document,
Fetch it's corresponding NQUADs file from JSON-LD playground
Expand All @@ -27,11 +28,17 @@ def jsonld2nquads(jsonld_doc):
JSON-LD annotated document
"""
# need to skip html escapes
nquads = requests.post('http://jsonld.biothings.io/?action=nquads', data={'doc': json.dumps(jsonld_doc).replace('>', ">").replace(' ', '')})
if nquads.status_code != 413:
# remove the log line from the nquads
nquads = re.sub('Parsed .*second.\n', '', nquads.json()['output'])
return t.parse_nquads(nquads)
if mode == 'request':
nquads = requests.post('http://jsonld.biothings.io/?action=nquads', data={'doc': json.dumps(jsonld_doc).replace('>', ">").replace(' ', '')})
if nquads.status_code != 413:
# remove the log line from the nquads
nquads = re.sub('Parsed .*second.\n', '', nquads.json()['output'])
return t.parse_nquads(nquads)
elif mode == 'grequest':
responses = []
for _jsonld_doc in jsonld_doc:
responses.append(grequest.post('http://jsonld.biothings.io/?action=nquads', data={'doc': json.dumps(jsonld_doc).replace('>', ">").replace(' ', '')}))
return grequests.map(iter(responses))

def fetchvalue(nquads, object_uri, predicate=None):
"""
Expand Down

0 comments on commit a131c60

Please sign in to comment.