From dc64e985d0344a5608acac51965726842b09dcc4 Mon Sep 17 00:00:00 2001 From: miquelduranfrigola Date: Mon, 31 Oct 2022 16:27:46 +0200 Subject: [PATCH] requests requirement and wrong input file exception --- ersilia/serve/api.py | 17 +++++++++++++++++ .../utils/exceptions_utils/api_exceptions.py | 9 +++++++++ requirements.txt | 1 + setup.py | 5 +---- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/ersilia/serve/api.py b/ersilia/serve/api.py index c4873c5d7..982294597 100644 --- a/ersilia/serve/api.py +++ b/ersilia/serve/api.py @@ -13,6 +13,8 @@ from .. import ErsiliaBase from .schema import ApiSchema +from ..utils.exceptions_utils.api_exceptions import InputFileNotFoundError + class Api(object): def __init__(self, model_id, url, api_name, save_to_lake, config_json): @@ -294,7 +296,22 @@ def post_unique_input(self, input, output, batch_size): for res in self.post_amenable_to_h5(input, output, batch_size): yield res + def _is_input_file(self, input): + if type(input) is str: + if input.endswith(".csv"): + return True + if input.endswith(".tst"): + return True + if input.endswith(".json"): + return True + if input.endswith(".txt"): + return True + return False + def post(self, input, output, batch_size): + if self._is_input_file(input): + if not os.path.exists(input): + raise InputFileNotFoundError(file_name=input) self.logger.debug("Posting to {0}".format(self.api_name)) self.logger.debug("Batch size {0}".format(batch_size)) unique_input, mapping = self._unique_input(input) diff --git a/ersilia/utils/exceptions_utils/api_exceptions.py b/ersilia/utils/exceptions_utils/api_exceptions.py index 9b053346a..bdaafa97e 100644 --- a/ersilia/utils/exceptions_utils/api_exceptions.py +++ b/ersilia/utils/exceptions_utils/api_exceptions.py @@ -6,3 +6,12 @@ def __init__(self): self.message = "Error occured while running api command" self.hints = "" super().__init__(self.message, self.hints) + + +class InputFileNotFoundError(ErsiliaError): + def __init__(self, file_name): + self.file_name = file_name + self.message = "Input file {0} does not exist".format(self.file_name) + self.hints = "Please be make sure that you are passing a valid input file. Accepted formats are .csv, .tsv and .json\n" + self.hints += "- Check that the file path is correct" + super().__init__(self.message, self.hints) diff --git a/requirements.txt b/requirements.txt index e9bcbf9d2..edaf4cdd5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,6 +11,7 @@ h5py==3.7.0 PyDrive2==1.14.0 inputimeout==1.0.4 protobuf==3.19.5 +requests<=2.24 # doc_builder sphinx==4.5.0 jinja2==3.1.2 diff --git a/setup.py b/setup.py index e7faee840..7a3096255 100644 --- a/setup.py +++ b/setup.py @@ -48,13 +48,10 @@ def _filter_requires(names, requires): "PyDrive2", "inputimeout", "protobuf", + "requests", ] slim_requires = _filter_requires(slim, install_requires) -# Web app requirements -webapp = slim + ["streamlit"] -webapp_requires = _filter_requires(webapp, install_requires) - # Doc builder requirements doc_builder = slim + ["sphinx", "jinja2"] doc_builder_requires = _filter_requires(doc_builder, install_requires)