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

Commit

Permalink
use grequests to perform asynchronous HTTP calls for biothings explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinxin90 committed Dec 5, 2017
1 parent 32ac2a1 commit 27d96b2
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions biothings_explorer/api_call_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import requests
import grequests

from .api_registry_parser import RegistryParser
from .jsonld_processor import json2nquads
Expand Down Expand Up @@ -130,11 +131,15 @@ def call_api(self, uri_value_dict, endpoint_name):
results[_para['name']] = _template['template'].replace('{{input}}', json.dumps(uri_value_dict[_template['valueType']]))
else:
results[_para['name']] = list(uri_value_dict.values())[0]
"""
# switch to grquests to handle multiple API calls together
if requests.get(endpoint_name, params=results).status_code == 200:
return requests.get(endpoint_name, params=results)
else:
print('This API call returns no results. The URI given is {}, the endpoint given is {}'.format(uri_value_dict, endpoint_name))
return
"""
return (endpoint_name, rsults)

def preprocess_json_doc(self, json_doc, endpoint_name):
"""
Expand Down Expand Up @@ -227,12 +232,17 @@ def input2output(self, input_type, input_value, endpoint_name, output_type, pred
# preprocess the input
processed_input = self.preprocessing_input(input_value, endpoint_name)
# retrieve json doc
api_call_params = []
for _input_value in processed_input:
uri_value = {input_type: _input_value}
if additional_parameters:
uri_value.update(additional_parameters)
api_call_response = self.call_api(uri_value, endpoint_name)
if api_call_response:
api_call_params.append(self.call_api(uri_value, endpoint_name))
rs = (grequests.get(u, params=v) for (u,v) in api_call_params)
responses = grequests.map(rs)
#api_call_response = self.call_api(uri_value, endpoint_name)
for api_call_response in responses:
if api_call_response.status_code == 200:
json_doc = self.preprocess_json_doc(api_call_response.json(), endpoint_name)
else:
print('This API call returns no response. Endpoint name: {}, Input value type: {}. Input value: {}'
Expand Down

0 comments on commit 27d96b2

Please sign in to comment.