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

Fix download_file API and added tests #173

Open
wants to merge 6 commits 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
51 changes: 33 additions & 18 deletions evalai/submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@

from click import echo, style

from evalai.utils.common import notify_user
from evalai.utils.common import notify_user, validate_token
from evalai.utils.requests import make_request
from evalai.utils.submissions import (
display_submission_details,
display_submission_result,
convert_bytes_to,
)
from evalai.utils.urls import URLS
from evalai.utils.config import EVALAI_HOST_URLS, HOST_URL_FILE_PATH
from evalai.utils.config import EVALAI_API_URLS, EVALAI_ERROR_CODES
from evalai.utils.auth import get_host_url, get_request_header


class Submission(object):
Expand Down Expand Up @@ -183,18 +184,21 @@ def push(image, phase):
@click.command()
@click.argument("URL", nargs=1)
def download_file(url):
"""
Download the file using URL with bucket and key
"""
"""
Invoked by `evalai download_file URL`.
"""
parsed_url = urlparse.urlparse(url)
parsed_host_url = "{parsed_url.scheme}://{parsed_url.netloc}".format(
parsed_url=parsed_url
)
is_correct_host = False
# TODO: Replace the hardcoded host url with cli's host url
with open(HOST_URL_FILE_PATH, "r") as file:
host_url = file.read()
if parsed_host_url in EVALAI_HOST_URLS:
is_correct_host = True
if parsed_host_url == host_url:
is_correct_host = True
curr_host = get_host_url()
if parsed_host_url in EVALAI_API_URLS:
if parsed_host_url == curr_host:
is_correct_host = True

if is_correct_host:
bucket = urlparse.parse_qs(parsed_url.query).get("bucket")
Expand All @@ -208,22 +212,33 @@ def download_file(url):
)
)
sys.exit(1)
request_path = URLS.download_file.value
request_path = request_path.format(bucket[0], key[0])
response = make_request(request_path, "GET")
signed_url = response.get("signed_url")

headers = get_request_header()
file_name = key[0].split("/")[-1]
URL = URLS.download_file.value
URL = "{}{}".format(get_host_url(), URL)
URL = URL.format(bucket[0], key[0])
try:
response = requests.get(signed_url, stream=True)
response = requests.get(URL, headers=headers)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
echo(err)
if response.status_code in EVALAI_ERROR_CODES:
validate_token(response.json())
echo(
style(
"\nError: {}\n".format(response.json()["error"]),
fg="red",
bold=True,
)
)
else:
echo(err)
sys.exit(1)
except requests.exceptions.RequestException:
except requests.exceptions.RequestException as e:
echo(
style(
"\nCould not establish a connection to EvalAI."
" Please check the Host URL.\n",
"\nCould not connect to EvalAI API. Please check"
" the URL or if server is running\n",
bold=True,
fg="red",
)
Expand Down
2 changes: 1 addition & 1 deletion evalai/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ def get_host_url():
with open(HOST_URL_FILE_PATH, "r") as fr:
try:
data = fr.read()
return str(data)
return str(data.rstrip('\n'))
except (OSError, IOError) as e:
echo(e)
5 changes: 5 additions & 0 deletions evalai/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@
"https://evalai-staging.cloudcv.org",
"http://localhost:8888",
]

EVALAI_API_URLS = [
"https://evalapi.cloudcv.org",
"http://localhost:8000",
]
75 changes: 74 additions & 1 deletion tests/test_submissions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import json
import responses
import os

from click.testing import CliRunner
from datetime import datetime
from dateutil import tz

from evalai.challenges import challenge
from evalai.submissions import submission
from evalai.submissions import submission, download_file
from tests.data import submission_response

from evalai.utils.config import API_HOST_URL
Expand Down Expand Up @@ -97,6 +98,78 @@ def test_display_submission_result(self):
assert response == expected


class TestDownloadFile(BaseTestClass):
def setup(self):
'''
#TODO Holding until responses doesn;t support multipart/form-data
url = "{}{}"
responses.add(
responses.GET,
url.format(API_HOST_URL, URLS.download_file.value).format(
"submission_9",
"result"
),
json=self.file,
content_type="multipart/form-data",
status=200,
)
'''
pass

def teardown(self):
if os.path.exists("./result"):
os.remove("./result")

@responses.activate
def test_download_file_when_url_invalid(self):
expected = "\nThe url doesn't match the EvalAI url. Please check the url.\n\n"

runner = CliRunner()

result = runner.invoke(download_file, ["https://falseurl.com"])
response = result.output
assert response == expected

@responses.activate
def test_download_file_when_bucket_or_key_missing(self):
expected = "\nThe bucket or key is missing in the url.\n\n"

runner = CliRunner()

result = runner.invoke(
download_file,
[API_HOST_URL + "/api/jobs/submission_files/?bucket=submission_9"])
response = result.output
assert response == expected

@responses.activate
def test_download_file_when_cannot_connect_EvalAI(self):
expected = "\nCould not connect to EvalAI API. Please check" \
" the URL or if server is running\n\n"

runner = CliRunner()

result = runner.invoke(
download_file,
[API_HOST_URL + "/api/jobs/submissio_files/?bucket=submission_9&key=result"])
response = result.output
assert response == expected

'''
@responses.activate
def test_download_file_when_download_success(self):
expected = "\nYour file {} is successfully downloaded.\n".format(
"result.json"
)
runner = CliRunner()

result = runner.invoke(download_file,
[API_HOST_URL + "/api/jobs/submission_files/?bucket=submission_9&key=result"])
response = result.output
assert response == expected
'''


class TestMakeSubmission(BaseTestClass):
def setup(self):
self.submission = json.loads(submission_response.submission_result)
Expand Down