-
Notifications
You must be signed in to change notification settings - Fork 66
[refactor] Replace conditional ladder in requests utils #237
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
base: master
Are you sure you want to change the base?
Changes from 15 commits
562eab5
4a121dc
964652a
c696825
5ac3f5b
3f3563e
76cb049
52fa64f
549fb29
561bcfa
d8e4ec7
1f3f431
a566886
4359f61
6f158d2
667926a
a0fac77
611188b
57fa1cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| import json | ||
| import requests | ||
| import sys | ||
|
|
||
|
|
@@ -11,75 +10,51 @@ | |
|
|
||
|
|
||
| def make_request(path, method, files=None, data=None): | ||
| if method in ["PUT", "PATCH", "DELETE"]: | ||
| raise ValueError("Unsupported method: {}".format(method)) | ||
|
|
||
| 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, headers=headers, 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")), | ||
nikochiko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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", | ||
|
|
@@ -90,19 +65,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() | ||
Uh oh!
There was an error while loading. Please reload this page.