Commit db10cadc authored by Jeroen F.J. Laros's avatar Jeroen F.J. Laros

Uniform way of uploading files.

parent 6745fc22
......@@ -3,8 +3,6 @@ Tests for the transfer_client CLI.
"""
from __future__ import unicode_literals
import json
from io import StringIO
from fake_open import FakeOpen, make_fake_file, md5_check
......@@ -19,10 +17,10 @@ class TestCLI(object):
cli.open = opener.open
self._fake_reply = None
self._file_handle = StringIO()
self._file_handle = make_fake_file('upload.bin', '')
self._file_handles = [
make_fake_file('a', 'a\n'), make_fake_file('b', 'b\n')]
self._metadata_handle = StringIO('null\n')
self._metadata_handle = make_fake_file('metadata.json', 'null\n')
self._output_handle = StringIO()
self._log_handle = StringIO()
......
"""
Tests for the transfer_client library.
"""
from io import StringIO
from __future__ import unicode_literals
from fake_open import make_fake_file
from transfer_client import TransferClient
......@@ -11,7 +13,7 @@ class TestLibrary(object):
TransferClient._request = lambda *args, **kwargs: None
self._transfer_client = TransferClient('127.0.0.1', False)
self._input_handle = StringIO()
self._input_handle = make_fake_file('upload.bin', '')
def test_users(self):
assert self._transfer_client.users('123') == None
......@@ -20,7 +22,8 @@ class TestLibrary(object):
assert self._transfer_client.schema('123') == None
def test_transfers(self):
assert self._transfer_client.transfers('123', {}) == None
assert self._transfer_client.transfers(
'123', make_fake_file('metadata.json', 'null\n')) == None
def test_status(self):
assert self._transfer_client.status('123', '456') == None
......
......@@ -4,6 +4,7 @@ import argparse
import datetime
import getpass
import hashlib
import io
import json
import socket
import sys
......@@ -58,7 +59,7 @@ def transfers(output_handle, metadata_handle, server_name, user_id, ssl_check):
:arg bool ssl_check: Check server SSL certificate.
"""
_write(output_handle, TransferClient(server_name, ssl_check).transfers(
user_id, json.loads(metadata_handle.read())))
user_id, metadata_handle))
def status(output_handle, server_name, user_id, transfer_id, ssl_check):
......@@ -176,9 +177,11 @@ def transfer(log_handle, file_handles, server_name, user_id, title, ssl_check):
:arg bool ssl_check: Check server SSL certificate.
"""
transfer_client = TransferClient(server_name, ssl_check)
fake_handle = io.StringIO(
json.dumps(_make_metadata(log_handle, file_handles, title)).decode())
fake_handle.name = 'metadata.json'
transfer_id = transfer_client.transfers(
user_id, _make_metadata(log_handle, file_handles, title))['id']
transfer_id = transfer_client.transfers(user_id, fake_handle)['id']
log_handle.write('Transfer ID: {}.\n'.format(transfer_id))
for file_handle in file_handles:
log_handle.write('Uploading file: {}.\n'.format(file_handle.name))
......
......@@ -67,7 +67,7 @@ class TransferClient(object):
"""
return self._request('get', 'users/schema', {'User-Id': user_id})
def transfers(self, user_id, metadata):
def transfers(self, user_id, metadata_handle):
"""
Initiates a new transfer.
......@@ -76,7 +76,7 @@ class TransferClient(object):
:returns dict: Transfer JSON object.
"""
multipart = requests_toolbelt.MultipartEncoder(
fields={'metadata': ('metadata.json', json.dumps(metadata))})
fields={'metadata': (metadata_handle.name, metadata_handle)})
return self._request(
'post', 'transfers',
{'User-Id': user_id, 'Content-Type': multipart.content_type},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment