diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index 99b7b0d5d..186843cd2 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -1,4 +1,3 @@ -import json import requests import sys @@ -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": + 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", @@ -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()