Skip to content

Commit

Permalink
Merge 3c33478 into 964146f
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego committed Oct 15, 2019
2 parents 964146f + 3c33478 commit a9c92b9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
63 changes: 39 additions & 24 deletions reana_client/api/client.py
Expand Up @@ -13,16 +13,22 @@
import traceback
from functools import partial

import requests
from bravado.exception import HTTPError
from reana_client.config import ERROR_MESSAGES, WORKFLOW_ENGINES
from reana_client.errors import FileDeletionError, FileUploadError
from reana_client.utils import (_validate_reana_yaml, get_workflow_root,
is_uuid_v4)
from reana_commons.api_client import get_current_api_client
from reana_commons.errors import REANASecretAlreadyExists, \
REANASecretDoesNotExist
from reana_commons.errors import (REANASecretAlreadyExists,
REANASecretDoesNotExist)
from werkzeug.local import LocalProxy

try:
from urllib.parse import urljoin
except ImportError:
from urlparse import urljoin

current_rs_api_client = LocalProxy(
partial(get_current_api_client, component='reana-server'))

Expand Down Expand Up @@ -245,29 +251,38 @@ def upload_file(workflow_id, file_, file_name, access_token):
:param access_token: access token of the current user.
"""
try:
(response,
http_response) = current_rs_api_client.api.upload_file(
workflow_id_or_name=workflow_id,
file_content=file_,
file_name=file_name,
access_token=access_token).result()

if http_response.status_code == 200:
return response
else:
raise Exception(
"Expected status code 200 but replied with "
"{status_code}".format(
status_code=http_response.status_code))

except HTTPError as e:
api_url = current_rs_api_client.swagger_spec.__dict__.get('api_url')
endpoint = \
current_rs_api_client.api.upload_file.operation.path_name.format(
workflow_id_or_name=workflow_id)
http_response = requests.post(urljoin(api_url, endpoint),
data=file_,
params={'file_name': file_name,
'access_token': access_token},
headers={'Content-Type':
'application/octet-stream'},
verify=False)
return http_response.json()
except requests.exceptions.ConnectionError:
logging.debug(
'File could not be uploaded: '
'\nStatus: {}\nReason: {}\n'
'Message: {}'.format(e.response.status_code,
e.response.reason,
e.response.json()['message']))
raise Exception(e.response.json()['message'])
'File could not be uploaded.', exc_info=True)
raise Exception(
'Could not connect with the server at {}'.format(api_url))
except requests.exceptions.HTTPError as e:
logging.debug(
'The server responded with an HTTP error code.', exc_info=True)
raise Exception(
'Unexpected response from the server: \n{}'.format(e.response))
except requests.exceptions.Timeout:
logging.debug(
'Timeout while trying to stablish connection.', exc_info=True)
raise Exception('The request to the server has timed out.')
except requests.exceptions.RequestException:
logging.debug(
'Something went wront while connecting to the server.',
exc_info=True)
raise Exception('The request to the server has failed for an '
'unknown reason.')
except Exception as e:
raise e

Expand Down
14 changes: 3 additions & 11 deletions tests/test_cli_files.py
Expand Up @@ -132,22 +132,13 @@ def test_download_file():

def test_upload_file(create_yaml_workflow_schema):
"""Test upload file."""
status_code = 200
reana_token = '000000'
file = 'file.txt'
response = [file]
env = {'REANA_SERVER_URL': 'localhost'}
env = {'REANA_SERVER_URL': 'http://localhost'}
message = 'was successfully uploaded.'
mock_http_response = Mock()
mock_http_response.status_code = status_code
mock_http_response.raw_bytes = str(response).encode()
mock_response = response
runner = CliRunner(env=env)
with runner.isolation():
with patch(
"reana_client.api.client.current_rs_api_client",
make_mock_api_client('reana-server')(mock_response,
mock_http_response)):
with patch("reana_client.api.client.requests.post") as post_request:
with runner.isolated_filesystem():
with open(file, 'w') as f:
f.write('test')
Expand All @@ -157,6 +148,7 @@ def test_upload_file(create_yaml_workflow_schema):
cli, ['upload', '-t', reana_token, '--workflow',
'mytest.1', file]
)
post_request.assert_called_once()
assert result.exit_code == 0
assert message in result.output

Expand Down

0 comments on commit a9c92b9

Please sign in to comment.