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

Added cli compatibility check #206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions evalai/submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)
from evalai.utils.urls import URLS
from evalai.utils.config import EVALAI_HOST_URLS, HOST_URL_FILE_PATH
from evalai.utils.requests import check_compatibility


class Submission(object):
Expand Down Expand Up @@ -215,6 +216,7 @@ def download_file(url):
file_name = key[0].split("/")[-1]
try:
response = requests.get(signed_url, stream=True)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
echo(err)
Expand Down
2 changes: 2 additions & 0 deletions evalai/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ def get_user_auth_token_by_login(username, password):
"""
Returns user auth token by login.
"""
from evalai.utils.requests import check_compatibility
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DeeJayBro can we place this import with all other imports at the start of the file

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, because it would lead to circular imports

url = "{}{}".format(get_host_url(), URLS.login.value)
try:
payload = {"username": username, "password": password}
response = requests.post(url, data=payload)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down
10 changes: 10 additions & 0 deletions evalai/utils/challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from evalai.utils.config import EVALAI_ERROR_CODES
from evalai.utils.urls import URLS
from evalai.utils.requests import check_compatibility


requests.packages.urllib3.disable_warnings()
Expand Down Expand Up @@ -61,6 +62,7 @@ def display_challenges(url):
header = get_request_header()
try:
response = requests.get(url, headers=header)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code == 401:
Expand Down Expand Up @@ -112,6 +114,7 @@ def display_ongoing_challenge_list():
header = get_request_header()
try:
response = requests.get(url, headers=header)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code == 401:
Expand Down Expand Up @@ -165,6 +168,7 @@ def get_participant_or_host_teams(url):

try:
response = requests.get(url, headers=header)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code == 401:
Expand Down Expand Up @@ -196,6 +200,7 @@ def get_participant_or_host_team_challenges(url, teams):
header = get_request_header()
try:
response = requests.get(url.format(team["id"]), headers=header)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code == 401:
Expand Down Expand Up @@ -317,6 +322,7 @@ def display_challenge_details(challenge):
header = get_request_header()
try:
response = requests.get(url, headers=header)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down Expand Up @@ -384,6 +390,7 @@ def display_challenge_phase_list(challenge_id):
headers = get_request_header()
try:
response = requests.get(url, headers=headers)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down Expand Up @@ -500,6 +507,7 @@ def display_challenge_phase_detail(challenge_id, phase_id, is_json):

try:
response = requests.get(url, headers=headers)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down Expand Up @@ -568,6 +576,7 @@ def display_challenge_phase_split_list(challenge_id):
headers = get_request_header()
try:
response = requests.get(url, headers=headers)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down Expand Up @@ -634,6 +643,7 @@ def display_leaderboard(challenge_id, phase_split_id):
headers = get_request_header()
try:
response = requests.get(url, headers=headers)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down
22 changes: 21 additions & 1 deletion evalai/utils/requests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import json
import requests
import sys
import pkg_resources
import semver

from click import echo, style

from evalai.utils.config import EVALAI_ERROR_CODES
from evalai.utils.config import EVALAI_ERROR_CODES, API_HOST_URL
from evalai.utils.common import validate_token

from .auth import get_request_header, get_host_url
Expand All @@ -17,6 +19,7 @@ def make_request(path, method, files=None, data=None):
if method == "GET":
try:
response = requests.get(url, headers=headers)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down Expand Up @@ -52,6 +55,7 @@ def make_request(path, method, files=None, data=None):
response = requests.post(
url, headers=headers, files=files, data=data
)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down Expand Up @@ -105,3 +109,19 @@ def make_request(path, method, files=None, data=None):
elif method == "DELETE":
# TODO: Add support for DELETE request
pass


def check_compatibility(response):
if "Minimum-CLI-Version" in response.headers:
version = pkg_resources.get_distribution("evalai").version
if semver.compare(version, response.headers["Minimum-CLI-Version"]) is -1:
echo(
style(
"\nCurrent CLI is not compatible with EvalAI hosted at {0}." +
" It needs to be updated from {1} to {2}\n"
.format(API_HOST_URL, version, response.headers["Minimum-CLI-Version"]),
fg="red",
bold=True,
)
)
sys.exit(1)
4 changes: 4 additions & 0 deletions evalai/utils/submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
validate_date_format,
convert_UTC_date_to_local,
)
from evalai.utils.requests import check_compatibility


requests.packages.urllib3.disable_warnings()
Expand All @@ -34,6 +35,7 @@ def make_submission(challenge_id, phase_id, file, submission_metadata={}):
response = requests.post(
url, headers=headers, files=input_file, data=data
)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down Expand Up @@ -151,6 +153,7 @@ def display_my_submission_details(

try:
response = requests.get(url, headers=headers)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down Expand Up @@ -222,6 +225,7 @@ def submission_details_request(submission_id):
headers = get_request_header()
try:
response = requests.get(url, headers=headers)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down
4 changes: 4 additions & 0 deletions evalai/utils/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from evalai.utils.common import validate_token
from evalai.utils.urls import URLS
from evalai.utils.config import EVALAI_ERROR_CODES
from evalai.utils.requests import check_compatibility


requests.packages.urllib3.disable_warnings()
Expand Down Expand Up @@ -60,6 +61,7 @@ def display_teams(is_host):

try:
response = requests.get(url, headers=headers)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down Expand Up @@ -114,6 +116,7 @@ def create_team(team_name, team_url, is_host):
data = json.dumps(data)
try:
response = requests.post(url, headers=headers, data=data)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down Expand Up @@ -185,6 +188,7 @@ def participate_in_a_challenge(challenge_id, participant_team_id):
headers["Content-Type"] = "application/json"
try:
response = requests.post(url, headers=headers)
check_compatibility(response)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ requests-toolbelt==0.8.0
responses==0.9.0
validators==0.12.2
termcolor==1.1.0
semver==2.9.0
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"responses==0.9.0",
"validators==0.12.2",
"termcolor==1.1.0",
"semver==2.9.0",
]

setup(
Expand Down
25 changes: 25 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import json

import mock
import pytest
import requests
import responses

from click.testing import CliRunner
Expand All @@ -9,9 +13,11 @@
from evalai.submissions import submission
from evalai.utils.urls import URLS
from evalai.utils.config import API_HOST_URL
from evalai.utils.requests import check_compatibility

from .base import BaseTestClass
from tests.data import challenge_response, teams_response
from types import SimpleNamespace


class TestHTTPErrorRequests(BaseTestClass):
Expand Down Expand Up @@ -895,3 +901,22 @@ def test_display_challenge_details_for_request_exception(self):
runner = CliRunner()
result = runner.invoke(challenge, ["1"])
assert result.exit_code == 1

@responses.activate
@mock.patch("pkg_resources.get_distribution", return_value=SimpleNamespace(version="1.0.0"))
def test_check_compatibility(self, mock_get_distribution):
url = "{}{}"
responses.add(
responses.GET,
url.format(
API_HOST_URL, "check_this"
),
headers={
"Minimum-CLI-Version": "2.0.0"
}
)
response = requests.get(url=url.format(API_HOST_URL, "check_this"))
with pytest.raises(SystemExit) as pytest_wrapped_e:
check_compatibility(response)
assert pytest_wrapped_e.type == SystemExit
assert pytest_wrapped_e.value.code == 1