diff --git a/cli/auth.py b/cli/auth.py index 37d136b..c15db82 100644 --- a/cli/auth.py +++ b/cli/auth.py @@ -27,8 +27,9 @@ def token(self): 'scope': 'openid' } response = requests.post(self.device_authorization_url, data=payload) - response_json = response.json() + response.raise_for_status() + response_json = response.json() device_code = response_json['device_code'] user_code = response_json['user_code'] verification_uri = response_json['verification_uri'] diff --git a/cli/submit.py b/cli/submit.py index c1f500e..def5885 100644 --- a/cli/submit.py +++ b/cli/submit.py @@ -1,9 +1,12 @@ #!/usr/bin/env python import os +from urllib.parse import urljoin import requests import yaml -from ebi_eva_common_pyutils.logger import logging_config + +from ebi_eva_common_pyutils.logger import logging_config, AppLogger +from retry import retry from cli.auth import get_auth @@ -15,9 +18,11 @@ class StudySubmitter: - def __init__(self, submission_initiate_url=SUBMISSION_INITIATE_URL): + def __init__(self, vcf_files, metadata_file, submission_initiate_url=SUBMISSION_INITIATE_URL): self.auth = get_auth() self.submission_initiate_url = submission_initiate_url + self.vcf_files = vcf_files + self.metadata_file = metadata_file def create_submission_config_file(self, submission_dir, submission_id, submission_upload_url): submission_config_file = os.path.join(submission_dir, SUB_CLI_CONFIG_FILE) @@ -38,11 +43,19 @@ def get_submission_id_and_upload_url(self, submission_dir): else: raise FileNotFoundError(f'Could not upload. No config file found for the submission in {submission_dir}') - # TODO - def upload_submission(self, submission_dir, submission_id=None, submission_upload_url=None): - if not submission_id or not submission_upload_url: + def upload_submission(self, submission_upload_url, submission_dir): + if not submission_upload_url: submission_id, submission_upload_url = self.get_submission_id_and_upload_url(submission_dir) - pass + + for f in self.vcf_files: + self.upload_file(submission_upload_url, f) + self.upload_file(submission_upload_url, self.metadata_file) + + @retry(tries=5, delay=10, backoff=5, logger=logger) + def upload_file(self, submission_upload_url, input_file): + base_name = os.path.basename(input_file) + r = requests.put(urljoin(submission_upload_url, base_name), data=open(input_file, 'rb')) + r.raise_for_status() def verify_submission_dir(self, submission_dir): if not os.path.exists(submission_dir): @@ -50,7 +63,6 @@ def verify_submission_dir(self, submission_dir): if not os.access(submission_dir, os.W_OK): raise Exception(f"The directory '{submission_dir}' does not have write permissions.") - def submit(self, submission_dir): self.verify_submission_dir(submission_dir) response = requests.post(self.submission_initiate_url, diff --git a/tests/test_submit.py b/tests/test_submit.py index bec7560..fc350a8 100644 --- a/tests/test_submit.py +++ b/tests/test_submit.py @@ -1,18 +1,20 @@ import json +import os import unittest -from unittest.mock import MagicMock, patch, Mock, PropertyMock +from unittest.mock import MagicMock, patch, Mock -from cli import LSRI_CLIENT_ID -from cli.auth import WebinAuth, LSRIAuth from cli.submit import StudySubmitter class TestSubmit(unittest.TestCase): + resource_dir = os.path.join(os.path.dirname(__file__), 'resources') def setUp(self) -> None: self.token = 'a token' with patch('cli.submit.get_auth', return_value=Mock(token=self.token)): - self.submitter = StudySubmitter() + vcf_files = [os.path.join(self.resource_dir, 'vcf_files', 'example2.vcf.gz')] + metadata_file = [os.path.join(self.resource_dir, 'EVA_Submission_template.V1.1.4.xlsx')] + self.submitter = StudySubmitter(vcf_files=vcf_files, metadata_file=metadata_file) def test_submit(self): # Mock the response for post-authentication response from eva-submission-ws @@ -30,3 +32,10 @@ def test_submit(self): headers={'Accept': 'application/hal+json', 'Authorization': 'Bearer a token'}) # TODO: Check that upload_submission was called with submission id + + def test_upload_file(self): + resource_dir = os.path.join(os.path.dirname(__file__), 'resources') + + file_to_upload = os.path.join(resource_dir, 'EVA_Submission_template.V1.1.4.xlsx') + self.submitter.upload_file(submission_upload_url='', + input_file=file_to_upload)