Skip to content

Commit 6c7562f

Browse files
shravandodaMec-iS
authored andcommitted
Corrected logger format. Improved URL processing (#90)
* Added a check for trailing slash in url_input. * Added a context manager to the load_data method * Corrected the logger format.
1 parent f0caf17 commit 6c7562f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

hydra_agent/querying_mechanism.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
from hydra_agent.hydra_graph import InitialGraph
55
import urllib.request
6+
from urllib.parse import urljoin
67
import json
78
from hydra_python_core import doc_maker
89
from urllib.error import URLError, HTTPError
@@ -29,18 +30,17 @@ def load_data(self, url):
2930
:return: loaded data
3031
"""
3132
try:
32-
response = urllib.request.urlopen(url)
33+
with urllib.request.urlopen(url) as response:
34+
return json.loads(response.read().decode('utf-8'))
3335
except HTTPError as e:
34-
logger.info('Error code: ', e.code)
36+
logger.error('Error Code: {}'.format(e.code))
3537
return RequestError("error")
3638
except URLError as e:
37-
logger.info('Reason: ', e.reason)
39+
logger.error('Reason: {}'.format(e.reason))
3840
return RequestError("error")
3941
except ValueError as e:
40-
logger.info("value error:", e)
42+
logger.error("Value Error: {}".format(e))
4143
return RequestError("error")
42-
else:
43-
return json.loads(response.read().decode('utf-8'))
4444

4545
def show_data(self, get_data):
4646
"""
@@ -685,7 +685,9 @@ def main():
685685
if url == "exit":
686686
print("exit...")
687687
return 0
688-
apidoc = handle_data.load_data(url + "/vocab")
688+
url = url.rstrip('/') + '/'
689+
url = urljoin(url, 'vocab')
690+
apidoc = handle_data.load_data(url)
689691
else:
690692
break
691693
return query(apidoc, url)

0 commit comments

Comments
 (0)