From 29b190d1d28a7cbaa186bfcfcbe63b36161d3709 Mon Sep 17 00:00:00 2001 From: Madison May Date: Thu, 11 Jun 2015 15:57:00 -0400 Subject: [PATCH] FIX: multi api calls for private cloud --- indicoio/utils/multi.py | 56 ++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/indicoio/utils/multi.py b/indicoio/utils/multi.py index 31725c3..94a4c7d 100644 --- a/indicoio/utils/multi.py +++ b/indicoio/utils/multi.py @@ -6,8 +6,12 @@ from indicoio.utils.errors import IndicoError CLIENT_SERVER_MAP = dict((api, api.strip().replace("_", "").lower()) for api in API_NAMES) SERVER_CLIENT_MAP = dict((v, k) for k, v in CLIENT_SERVER_MAP.iteritems()) +AVAILABLE_APIS = { + 'text': TEXT_APIS, + 'image': IMAGE_APIS +} -def multi(data, type, apis, available, batch=False, **kwargs): +def multi(data, datatype, apis, batch=False, **kwargs): """ Helper to make multi requests of different types. @@ -22,23 +26,39 @@ def multi(data, type, apis, available, batch=False, **kwargs): :rtype: Dictionary of api responses """ # Client side api name checking - strictly only accept func name api + available = AVAILABLE_APIS.get(datatype) invalid_apis = [api for api in apis if api not in available] if invalid_apis: - raise IndicoError("%s are not valid %s APIs. Please reference the available APIs below:\n%s" - % (", ".join(invalid_apis), type, ", ".join(available)) - ) + raise IndicoError( + "%s are not valid %s APIs. Please reference the available APIs below:\n%s" + % (", ".join(invalid_apis), datatype, ", ".join(available)) + ) + # Convert client api names to server names before sending request apis = map(CLIENT_SERVER_MAP.get, apis) - result = api_handler(data, url_params = {"apis":apis, "batch":batch}, **kwargs) + cloud = kwargs.pop("cloud", None) + api_key = kwargs.pop('api_key', None) + result = api_handler( + data, + cloud=cloud, + api='apis', + url_params={ + "apis":apis, + "batch":batch, + "api_key":api_key + }, + **kwargs + ) return handle_response(result) + def handle_response(result): # Parse out the results to a dicionary of api: result return dict((SERVER_CLIENT_MAP[api], parsed_response(api, res)) for api, res in result.iteritems()) -def predict_text(input_text, apis=TEXT_APIS, cloud=None, batch=False, api_key=None, **kwargs): +def predict_text(input_text, apis=TEXT_APIS, **kwargs): """ Given input text, returns the results of specified text apis. Possible apis include: [ 'text_tags', 'political', 'sentiment', 'language' ] @@ -60,19 +80,22 @@ def predict_text(input_text, apis=TEXT_APIS, cloud=None, batch=False, api_key=No :rtype: Dictionary of api responses """ + cloud = kwargs.pop('cloud', None) + batch = kwargs.pop('batch', False) + api_key = kwargs.pop('api_key', None) + return multi( - api="apis", data=input_text, - type="text", - available = TEXT_APIS, + datatype="text", cloud=cloud, batch=batch, api_key=api_key, apis=apis, - **kwargs) + **kwargs + ) -def predict_image(image, apis=IMAGE_APIS, cloud=None, batch=False, api_key=None, **kwargs): +def predict_image(image, apis=IMAGE_APIS, **kwargs): """ Given input image, returns the results of specified image apis. Possible apis include: ['fer', 'facial_features', 'image_features'] @@ -95,16 +118,19 @@ def predict_image(image, apis=IMAGE_APIS, cloud=None, batch=False, api_key=None, :rtype: Dictionary of api responses """ + cloud = kwargs.pop('cloud', None) + batch = kwargs.pop('batch', False) + api_key = kwargs.pop('api_key', None) + return multi( - api="apis", data=image_preprocess(image, batch=batch), - type="image", - available=IMAGE_APIS, + datatype="image", cloud=cloud, batch=batch, api_key=api_key, apis=apis, - **kwargs) + **kwargs + ) def parsed_response(api, response): result = response.get('results', False)