Skip to content

Commit

Permalink
requests requirement and wrong input file exception
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelduranfrigola committed Oct 31, 2022
1 parent 7513e43 commit dc64e98
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
17 changes: 17 additions & 0 deletions ersilia/serve/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions ersilia/utils/exceptions_utils/api_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dc64e98

Please sign in to comment.