Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] Replace conditional ladder in requests utils #237

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
101 changes: 23 additions & 78 deletions evalai/utils/requests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import requests
import sys

Expand All @@ -11,75 +10,31 @@


def make_request(path, method, files=None, data=None):
if method in ["PUT", "PATCH", "DELETE"]:
raise Exception("HTTP method {} is not supported by make_request".format(method))

url = "{}{}".format(get_host_url(), path)
headers = get_request_header()

if method == "GET":
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
validate_token(response.json())
echo(
style(
"\nError: {}\n".format(response.json().get("error")),
fg="red",
bold=True,
)
)
else:
echo(err)
sys.exit(1)
except requests.exceptions.RequestException:
echo(
style(
"\nCould not establish a connection to EvalAI."
" Please check the Host URL.\n",
bold=True,
fg="red",
)
)
sys.exit(1)
return response.json()
elif method == "POST":
if files:
files = {"input_file": open(files, "rb")}
else:
files = None
if method == "POST":
nikochiko marked this conversation as resolved.
Show resolved Hide resolved
files = {"input_file": open(files, "rb")} if files else None
data = {"status": "submitting"}
try:
response = requests.post(
url, headers=headers, files=files, data=data
)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
validate_token(response.json())
echo(
style(
"\nError: {}\n"
"\nUse `evalai challenges` to fetch the active challenges.\n"
"\nUse `evalai challenge CHALLENGE phases` to fetch the "
"active phases.\n".format(response.json()["error"]),
fg="red",
bold=True,
)
)
else:
echo(err)
sys.exit(1)
except requests.exceptions.RequestException:
echo(
style(
"\nCould not establish a connection to EvalAI."
" Please check the Host URL.\n",
bold=True,
fg="red",
)
)
sys.exit(1)
response = json.loads(response.text)

try:
response = requests.request(method, url, data=data, headers=headers, files=files)
response.raise_for_status()
except requests.exceptions.RequestException as e:
if isinstance(e, requests.exceptions.HTTPError) and response.status_code in EVALAI_ERROR_CODES:
validate_token(response.json())
error = response.json().get("error") # Error message is returned by server in this case
e = "\n{}\n" \
"\nUse `evalai challenges` to fetch the active challenges.\n" \
"\nUse `evalai challenge CHALLENGE phases` to fetch the active phases.\n".format(error)

echo("Could not establish a connection to EvalAI with error: {}".format(e))
sys.exit(1)

if method == "POST":
echo(
style(
"\nYour docker file is successfully submitted.\n",
Expand All @@ -90,19 +45,9 @@ def make_request(path, method, files=None, data=None):
echo(
style(
"You can use `evalai submission {}` to view this submission's status.\n".format(
response.get("id")
response.json().get("id")
),
bold=True,
fg="white"
)
)
return response
elif method == "PUT":
# TODO: Add support for PUT request
pass
elif method == "PATCH":
# TODO: Add support for PATCH request
pass
elif method == "DELETE":
# TODO: Add support for DELETE request
pass
return response.json()