From 6e0ac4cd5d466452f3eb3c5a5cbec3153de05d92 Mon Sep 17 00:00:00 2001 From: Chris Lee Date: Fri, 5 Jun 2015 13:50:13 -0400 Subject: [PATCH] ADD: url params for apis for multi api req --- indicoio/images/features.py | 4 ++-- indicoio/images/fer.py | 2 +- indicoio/text/lang.py | 2 +- indicoio/text/sentiment.py | 4 ++-- indicoio/text/tagging.py | 2 +- indicoio/utils/__init__.py | 11 +++++------ indicoio/utils/multi.py | 2 +- 7 files changed, 13 insertions(+), 14 deletions(-) diff --git a/indicoio/images/features.py b/indicoio/images/features.py index 3c20010..fc2fd10 100644 --- a/indicoio/images/features.py +++ b/indicoio/images/features.py @@ -25,7 +25,7 @@ def facial_features(image, cloud=None, batch=False, api_key=None, **kwargs): :rtype: List containing feature responses """ image = image_preprocess(image, batch=batch) - return api_handler(image, cloud=cloud, api="facialfeatures", batch=batch, api_key=api_key, **kwargs) + return api_handler(image, cloud=cloud, api="facialfeatures", url_params={"batch":batch, "api_key":api_key}, **kwargs) def image_features(image, cloud=None, batch=False, api_key=None, **kwargs): """ @@ -58,4 +58,4 @@ def image_features(image, cloud=None, batch=False, api_key=None, **kwargs): :rtype: List containing features """ image = image_preprocess(image, batch=batch, size=(64,64)) - return api_handler(image, cloud=cloud, api="imagefeatures", batch=batch, api_key=api_key, **kwargs) + return api_handler(image, cloud=cloud, api="imagefeatures", url_params={"batch":batch, "api_key":api_key}, **kwargs) diff --git a/indicoio/images/fer.py b/indicoio/images/fer.py index 642f086..9a85266 100644 --- a/indicoio/images/fer.py +++ b/indicoio/images/fer.py @@ -27,4 +27,4 @@ def fer(image, cloud=None, batch=False, api_key=None, **kwargs): :rtype: Dictionary containing emotion probability pairs """ image = image_preprocess(image, batch=batch) - return api_handler(image, cloud=cloud, api="fer", batch=batch, api_key=api_key, **kwargs) + return api_handler(image, cloud=cloud, api="fer", url_params={"batch":batch, "api_key":api_key}, **kwargs) diff --git a/indicoio/text/lang.py b/indicoio/text/lang.py index 547c18b..d4c42d2 100644 --- a/indicoio/text/lang.py +++ b/indicoio/text/lang.py @@ -24,4 +24,4 @@ def language(text, cloud=None, batch=False, api_key=None, **kwargs): :rtype: Dictionary of language probability pairs """ - return api_handler(text, cloud=cloud, api="language", batch=batch, api_key=api_key, **kwargs) + return api_handler(text, cloud=cloud, api="language", url_params={"batch":batch, "api_key":api_key}, **kwargs) diff --git a/indicoio/text/sentiment.py b/indicoio/text/sentiment.py index 1464943..d61f584 100644 --- a/indicoio/text/sentiment.py +++ b/indicoio/text/sentiment.py @@ -26,7 +26,7 @@ def political(text, cloud=None, batch=False, api_key=None, **kwargs): :rtype: Dictionary of party probability pairs """ - return api_handler(text, cloud=cloud, api="political", batch=batch, api_key=api_key, **kwargs) + return api_handler(text, cloud=cloud, api="political", url_params={"batch":batch, "api_key":api_key}, **kwargs) def posneg(text, cloud=None, batch=False, api_key=None, **kwargs): """ @@ -49,4 +49,4 @@ def posneg(text, cloud=None, batch=False, api_key=None, **kwargs): :rtype: Float """ - return api_handler(text, cloud=cloud, api="sentiment", batch=batch, api_key=api_key, **kwargs) + return api_handler(text, cloud=cloud, api="sentiment", url_params={"batch":batch, "api_key":api_key}, **kwargs) diff --git a/indicoio/text/tagging.py b/indicoio/text/tagging.py index 13a7246..8be1f75 100644 --- a/indicoio/text/tagging.py +++ b/indicoio/text/tagging.py @@ -23,4 +23,4 @@ def text_tags(text, cloud=None, batch=False, api_key=None, **kwargs): :rtype: Dictionary of class probability pairs """ - return api_handler(text, cloud=cloud, api="texttags", batch=batch, api_key=api_key, **kwargs) + return api_handler(text, cloud=cloud, api="texttags", url_params={"batch":batch, "api_key":api_key}, **kwargs) diff --git a/indicoio/utils/__init__.py b/indicoio/utils/__init__.py index 86cfa96..89f9a3e 100644 --- a/indicoio/utils/__init__.py +++ b/indicoio/utils/__init__.py @@ -8,7 +8,7 @@ from indicoio import config B64_PATTERN = re.compile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)") -def api_handler(arg, cloud, api, batch=False, api_key=None, **kwargs): +def api_handler(arg, cloud, api, url_params = {"batch":False, "api_key":None}, **kwargs): data = {'data': arg} data.update(**kwargs) json_data = json.dumps(data) @@ -21,12 +21,11 @@ def api_handler(arg, cloud, api, batch=False, api_key=None, **kwargs): # default to indico public cloud host = config.PUBLIC_API_HOST - if not api_key: - api_key = config.api_key - url = config.url_protocol + "//%s/%s" % (host, api) - url = url + "/batch" if batch else url - url += "?key=%s" % api_key + url = url + "/batch" if url_params.get("batch", False) else url + url += "?key=%s" % (url_params.get("api_key", None) or config.api_key) + if "apis" in url_params: + url += "&apis=%s" % ",".join(url_params["apis"]) response = requests.post(url, data=json_data, headers=JSON_HEADERS) if response.status_code == 503 and cloud != None: diff --git a/indicoio/utils/multi.py b/indicoio/utils/multi.py index c023e62..e8f033f 100644 --- a/indicoio/utils/multi.py +++ b/indicoio/utils/multi.py @@ -27,7 +27,7 @@ def multi(data, type, apis, available, batch=False, **kwargs): ) # Convert client api names to server names before sending request apis = map(CLIENT_SERVER_MAP.get, apis) - result = api_handler(data, apis=apis, batch=batch, **kwargs) + result = api_handler(data, url_params = {"apis":apis, "batch":batch}, **kwargs) return handle_response(result) def handle_response(result):