From 562eab5a52455064b97be57f08b3e242c4b81acf Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 11:32:28 +0530 Subject: [PATCH 01/19] Replace conditional ladder --- evalai/utils/requests.py | 100 +++++++++++++-------------------------- 1 file changed, 33 insertions(+), 67 deletions(-) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index 99b7b0d5d..40d6ce3b4 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -13,73 +13,46 @@ def make_request(path, method, files=None, data=None): url = "{}{}".format(get_host_url(), path) headers = get_request_header() + if method == "POST": + files = {"input_file": open(files, "rb")} if files else None + data = {"status": "submitting"} - 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: + try: + response = requests.request(method, url, data=data, files=files) + response.raise_for_status() + except requests.exceptions.HTTPError as e: + if response.status_code in EVALAI_ERROR_CODES: + validate_token(response.json()) echo( style( - "\nCould not establish a connection to EvalAI." - " Please check the Host URL.\n", - bold=True, + "\nError: {}\n".format(response.json().get("error")), fg="red", + bold=True, ) ) - sys.exit(1) - return response.json() - elif method == "POST": - if files: - files = {"input_file": open(files, "rb")} - else: - files = 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", + "Use `evalai challenges` to fetch the active challenges." + "Use `evalai challenge CHALLENGE phases` to fetch the " + "active phases.", bold=True, - fg="red", ) ) - sys.exit(1) - response = json.loads(response.text) + else: + echo(e) + 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) + + if method == "POST": echo( style( "\nYour docker file is successfully submitted.\n", @@ -93,16 +66,9 @@ def make_request(path, method, files=None, data=None): response.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 + # TODO: Add support for PUT request + # TODO: Add support for PATCH request + # TODO: Add support for DELETE request + return response.json() From 4a121dc7312d6387f026f6e438dc11b9a3be2915 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 11:37:10 +0530 Subject: [PATCH 02/19] Fix unused variables --- evalai/utils/requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index 40d6ce3b4..f38ad8f19 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -18,7 +18,7 @@ def make_request(path, method, files=None, data=None): data = {"status": "submitting"} try: - response = requests.request(method, url, data=data, files=files) + response = requests.request(method, url, data=data, headers=headers, files=files) response.raise_for_status() except requests.exceptions.HTTPError as e: if response.status_code in EVALAI_ERROR_CODES: From 964652a79e1740890cedcdcdcb0cfa5504ce4570 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 11:37:26 +0530 Subject: [PATCH 03/19] Fix unused import --- evalai/utils/requests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index f38ad8f19..c7d0ef3fe 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -1,4 +1,3 @@ -import json import requests import sys From c69682524accb466ec6a3f5f9ac4ab85a25abf10 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 12:06:23 +0530 Subject: [PATCH 04/19] Add debugging block --- evalai/submissions.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/evalai/submissions.py b/evalai/submissions.py index ac8bccf62..d61c4e17d 100644 --- a/evalai/submissions.py +++ b/evalai/submissions.py @@ -109,6 +109,9 @@ def push(image, phase, url): request_path = URLS.challenge_details.value request_path = request_path.format(challenge_pk) response = make_request(request_path, "GET") + # DUBUG + raise Exception('Response: {}\nContent: {}'.format(response, response.content)) + # DEBUG max_docker_image_size = response.get("max_docker_image_size") docker_image_size = docker_image.__dict__.get("attrs").get("VirtualSize") From 5ac3f5b4de64af6871f1f11ac48bb7e568e96607 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 12:08:25 +0530 Subject: [PATCH 05/19] Debug further --- evalai/submissions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evalai/submissions.py b/evalai/submissions.py index d61c4e17d..062517bf3 100644 --- a/evalai/submissions.py +++ b/evalai/submissions.py @@ -110,7 +110,7 @@ def push(image, phase, url): request_path = request_path.format(challenge_pk) response = make_request(request_path, "GET") # DUBUG - raise Exception('Response: {}\nContent: {}'.format(response, response.content)) + raise Exception('Response: {}'.format(response)) # DEBUG max_docker_image_size = response.get("max_docker_image_size") From 3f3563e4947d50da14faff759fc915c4936fda0c Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 12:10:51 +0530 Subject: [PATCH 06/19] Remove debug block --- evalai/submissions.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/evalai/submissions.py b/evalai/submissions.py index 062517bf3..ac8bccf62 100644 --- a/evalai/submissions.py +++ b/evalai/submissions.py @@ -109,9 +109,6 @@ def push(image, phase, url): request_path = URLS.challenge_details.value request_path = request_path.format(challenge_pk) response = make_request(request_path, "GET") - # DUBUG - raise Exception('Response: {}'.format(response)) - # DEBUG max_docker_image_size = response.get("max_docker_image_size") docker_image_size = docker_image.__dict__.get("attrs").get("VirtualSize") From 76cb0491d45a549fa2093297c69a04bd28ed1da8 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 12:29:58 +0530 Subject: [PATCH 07/19] DEBUG --- evalai/submissions.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/evalai/submissions.py b/evalai/submissions.py index ac8bccf62..c879477a2 100644 --- a/evalai/submissions.py +++ b/evalai/submissions.py @@ -103,6 +103,9 @@ def push(image, phase, url): request_path = URLS.phase_details_using_slug.value request_path = request_path.format(phase) response = make_request(request_path, "GET") + # DEBUG: + raise Exception("Response: {}, content: {}".foramt(response, response.content)) + # challenge_pk = response.get("challenge") phase_pk = response.get("id") From 52fa64faa5fb4f1ba775eb7e956d14783a2ddbbc Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 12:34:43 +0530 Subject: [PATCH 08/19] Fix typo --- evalai/submissions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evalai/submissions.py b/evalai/submissions.py index c879477a2..1f112abb8 100644 --- a/evalai/submissions.py +++ b/evalai/submissions.py @@ -104,7 +104,7 @@ def push(image, phase, url): request_path = request_path.format(phase) response = make_request(request_path, "GET") # DEBUG: - raise Exception("Response: {}, content: {}".foramt(response, response.content)) + raise Exception("Response: {}, content: {}".format(response, response.content)) # challenge_pk = response.get("challenge") phase_pk = response.get("id") From 549fb2953c5441fb0d4e6b5a37e8ab82a5e782e9 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 12:39:29 +0530 Subject: [PATCH 09/19] Edit debug block --- evalai/submissions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evalai/submissions.py b/evalai/submissions.py index 1f112abb8..a7e010d59 100644 --- a/evalai/submissions.py +++ b/evalai/submissions.py @@ -104,7 +104,7 @@ def push(image, phase, url): request_path = request_path.format(phase) response = make_request(request_path, "GET") # DEBUG: - raise Exception("Response: {}, content: {}".format(response, response.content)) + raise Exception("Response: {}, content: {}".format(response)) # challenge_pk = response.get("challenge") phase_pk = response.get("id") From 561bcfa0d91b3d85649b3773d56a5bc9a3391ecc Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 12:51:52 +0530 Subject: [PATCH 10/19] Fix debug --- evalai/submissions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evalai/submissions.py b/evalai/submissions.py index a7e010d59..732b3c9b2 100644 --- a/evalai/submissions.py +++ b/evalai/submissions.py @@ -104,7 +104,7 @@ def push(image, phase, url): request_path = request_path.format(phase) response = make_request(request_path, "GET") # DEBUG: - raise Exception("Response: {}, content: {}".format(response)) + raise Exception(response) # challenge_pk = response.get("challenge") phase_pk = response.get("id") From d8e4ec74c9b00a3ca7bfc96c61efa7b63104c483 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Mon, 30 Dec 2019 12:58:40 +0530 Subject: [PATCH 11/19] Fix error --- evalai/submissions.py | 3 --- evalai/utils/requests.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/evalai/submissions.py b/evalai/submissions.py index 732b3c9b2..ac8bccf62 100644 --- a/evalai/submissions.py +++ b/evalai/submissions.py @@ -103,9 +103,6 @@ def push(image, phase, url): request_path = URLS.phase_details_using_slug.value request_path = request_path.format(phase) response = make_request(request_path, "GET") - # DEBUG: - raise Exception(response) - # challenge_pk = response.get("challenge") phase_pk = response.get("id") diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index c7d0ef3fe..931c45b46 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -62,7 +62,7 @@ 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, ) From 1f3f431706bd2b37eec07c76dca9534bf539a8ad Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Mon, 30 Dec 2019 16:39:42 +0530 Subject: [PATCH 12/19] Remove TODO --- evalai/utils/requests.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index 931c45b46..174596872 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -67,7 +67,4 @@ def make_request(path, method, files=None, data=None): bold=True, ) ) - # TODO: Add support for PUT request - # TODO: Add support for PATCH request - # TODO: Add support for DELETE request return response.json() From a566886c3bbf5ddc75a19fd3a32041f4021542bb Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Tue, 31 Dec 2019 11:06:37 +0530 Subject: [PATCH 13/19] Exit when passed method is unsupoorted --- evalai/utils/requests.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index 174596872..e7525695e 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -10,6 +10,16 @@ def make_request(path, method, files=None, data=None): + if method in ["PUT", "PATCH", "DELETE"]: + echo( + style( + "\nError: HTTP Method is not supported: {}\n".format(method), + bold=True, + fg="red", + ) + ) + sys.exit(1) + url = "{}{}".format(get_host_url(), path) headers = get_request_header() if method == "POST": From 4359f61040a630351b9517acf0a589fa8c503ea8 Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Tue, 31 Dec 2019 11:11:54 +0530 Subject: [PATCH 14/19] Fix linting --- evalai/utils/requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index e7525695e..b699c43b1 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -10,7 +10,7 @@ def make_request(path, method, files=None, data=None): - if method in ["PUT", "PATCH", "DELETE"]: + if method in ["PUT", "PATCH", "DELETE"]: echo( style( "\nError: HTTP Method is not supported: {}\n".format(method), From 6f158d2ee1f749e9772c4f5f6c1a0d4582ce5554 Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Tue, 31 Dec 2019 14:21:37 +0530 Subject: [PATCH 15/19] Change echo + SystemExit -> ValueError --- evalai/utils/requests.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index b699c43b1..d1a55efb1 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -11,14 +11,7 @@ def make_request(path, method, files=None, data=None): if method in ["PUT", "PATCH", "DELETE"]: - echo( - style( - "\nError: HTTP Method is not supported: {}\n".format(method), - bold=True, - fg="red", - ) - ) - sys.exit(1) + raise ValueError("Unsupported method: {}".format(method)) url = "{}{}".format(get_host_url(), path) headers = get_request_header() From 667926a45aad036aa31537eeabef596a231883c0 Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Wed, 1 Jan 2020 11:14:08 +0530 Subject: [PATCH 16/19] Add new line --- evalai/utils/requests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index d1a55efb1..07b4a556f 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -15,6 +15,7 @@ def make_request(path, method, files=None, data=None): url = "{}{}".format(get_host_url(), path) headers = get_request_header() + if method == "POST": files = {"input_file": open(files, "rb")} if files else None data = {"status": "submitting"} From a0fac77ed6673bad8045ae2e467e22063957c12c Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Wed, 1 Jan 2020 12:42:34 +0530 Subject: [PATCH 17/19] Update Exception for unsupported method --- evalai/utils/requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index 07b4a556f..9288cb9d2 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -11,7 +11,7 @@ def make_request(path, method, files=None, data=None): if method in ["PUT", "PATCH", "DELETE"]: - raise ValueError("Unsupported method: {}".format(method)) + raise Exception("HTTP method {} is not supported by make_request".format(method)) url = "{}{}".format(get_host_url(), path) headers = get_request_header() From 611188b14988e7795d3b20847d2a334856aeaccf Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Wed, 1 Jan 2020 13:53:35 +0530 Subject: [PATCH 18/19] Update error handling --- evalai/utils/requests.py | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index 9288cb9d2..95af13090 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -23,36 +23,11 @@ def make_request(path, method, files=None, data=None): try: response = requests.request(method, url, data=data, headers=headers, files=files) response.raise_for_status() - except requests.exceptions.HTTPError as e: - if response.status_code in EVALAI_ERROR_CODES: + except requests.exceptions.RequestException as e: + if isinstance(e, requests.exceptions.HTTPError) and response.status_code in EVALAI_ERROR_CODES: validate_token(response.json()) - echo( - style( - "\nError: {}\n".format(response.json().get("error")), - fg="red", - bold=True, - ) - ) - echo( - style( - "Use `evalai challenges` to fetch the active challenges." - "Use `evalai challenge CHALLENGE phases` to fetch the " - "active phases.", - bold=True, - ) - ) - else: - echo(e) - 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", - ) - ) + e = response.json().get("error") # In this case, the error message is returned by the server + echo("Could not establish a connection to EvalAI with error: {}".format(e)) sys.exit(1) if method == "POST": From 57fa1cc1741640f1009d922c5c4d03d230358fd9 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Wed, 1 Jan 2020 14:56:45 +0530 Subject: [PATCH 19/19] Generalize the handling message --- evalai/utils/requests.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/evalai/utils/requests.py b/evalai/utils/requests.py index 95af13090..186843cd2 100644 --- a/evalai/utils/requests.py +++ b/evalai/utils/requests.py @@ -26,7 +26,11 @@ def make_request(path, method, files=None, data=None): except requests.exceptions.RequestException as e: if isinstance(e, requests.exceptions.HTTPError) and response.status_code in EVALAI_ERROR_CODES: validate_token(response.json()) - e = response.json().get("error") # In this case, the error message is returned by the server + 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)