From fe0032dfc128862ac8eb35e5dcd20a6bef3f52b7 Mon Sep 17 00:00:00 2001 From: ShravanDoda Date: Sun, 3 Mar 2019 09:37:29 +0530 Subject: [PATCH 1/3] Added a check for trailing slash in url_input. Fixes the issue when the user enters an extra trailing slash and the client requests to api_url//vocab instead of api_url/vocab --- hydra_agent/querying_mechanism.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hydra_agent/querying_mechanism.py b/hydra_agent/querying_mechanism.py index 4e72fb7..3365640 100644 --- a/hydra_agent/querying_mechanism.py +++ b/hydra_agent/querying_mechanism.py @@ -3,6 +3,7 @@ import logging from hydra_agent.hydra_graph import InitialGraph import urllib.request +from urllib.parse import urljoin import json from hydra_python_core import doc_maker from urllib.error import URLError, HTTPError @@ -685,7 +686,9 @@ def main(): if url == "exit": print("exit...") return 0 - apidoc = handle_data.load_data(url + "/vocab") + url = url.rstrip('/') + '/' + url = urljoin(url, 'vocab') + apidoc = handle_data.load_data(url) else: break return query(apidoc, url) From 299aa590f6184b5381b9ed253e537c6a75581c61 Mon Sep 17 00:00:00 2001 From: ShravanDoda Date: Sun, 3 Mar 2019 09:45:42 +0530 Subject: [PATCH 2/3] urllib request should be used with a context manager. Added a context manager to the load_data method --- hydra_agent/querying_mechanism.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hydra_agent/querying_mechanism.py b/hydra_agent/querying_mechanism.py index 3365640..1cfdbd3 100644 --- a/hydra_agent/querying_mechanism.py +++ b/hydra_agent/querying_mechanism.py @@ -30,7 +30,8 @@ def load_data(self, url): :return: loaded data """ try: - response = urllib.request.urlopen(url) + with urllib.request.urlopen(url) as response: + return json.loads(response.read().decode('utf-8')) except HTTPError as e: logger.info('Error code: ', e.code) return RequestError("error") @@ -40,8 +41,6 @@ def load_data(self, url): except ValueError as e: logger.info("value error:", e) return RequestError("error") - else: - return json.loads(response.read().decode('utf-8')) def show_data(self, get_data): """ From 2eb8864d6427957a95efb7087e20847861e1ccef Mon Sep 17 00:00:00 2001 From: ShravanDoda Date: Sun, 3 Mar 2019 09:49:54 +0530 Subject: [PATCH 3/3] Corrected the logger format. Changed logger.info to logger.error. Corrected string formatting --- hydra_agent/querying_mechanism.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hydra_agent/querying_mechanism.py b/hydra_agent/querying_mechanism.py index 1cfdbd3..bca3264 100644 --- a/hydra_agent/querying_mechanism.py +++ b/hydra_agent/querying_mechanism.py @@ -33,13 +33,13 @@ def load_data(self, url): with urllib.request.urlopen(url) as response: return json.loads(response.read().decode('utf-8')) except HTTPError as e: - logger.info('Error code: ', e.code) + logger.error('Error Code: {}'.format(e.code)) return RequestError("error") except URLError as e: - logger.info('Reason: ', e.reason) + logger.error('Reason: {}'.format(e.reason)) return RequestError("error") except ValueError as e: - logger.info("value error:", e) + logger.error("Value Error: {}".format(e)) return RequestError("error") def show_data(self, get_data):