def upload(self, filepath, shard_size=0, start_pos=0, read_size=1024, callback=None): """ Uploads a shard via POST to the specified node to the web-core API. See API docs: https://github.com/Storj/web-core#api-documentation :param filepath: Path to file as a string :return: upstream.shard.Shard :raise LookupError: IF """ # Open the file and upload it via POST url = self.server + "/api/upload" # web-core API r = self._upload_form_encoded( url, filepath, shard_size=shard_size, start_pos=start_pos, read_size=read_size, callback=callback ) # Make sure that the API call is actually there if r.status_code == 404: raise ResponseError("API call not found.") elif r.status_code == 402: raise ResponseError("Payment required.") elif r.status_code == 500: raise ResponseError("Server error.") elif r.status_code == 201: shard = Shard() shard.from_json(r.text) return shard else: err = ResponseError("Received status code %s %s" % (r.status_code, r.reason)) err.response = r raise err
def upload(self, filepath, shard_size=0, start_pos=0, read_size=1024, callback=None): """ Uploads a shard via POST to the specified node to the web-core API. See API docs: https://github.com/Storj/web-core#api-documentation :param filepath: Path to file as a string :return: upstream.shard.Shard :raise LookupError: IF """ # Open the file and upload it via POST url = self.server + "/api/upload" # web-core API r = self._upload_form_encoded(url, filepath, shard_size=shard_size, start_pos=start_pos, read_size=read_size, callback=callback) # Make sure that the API call is actually there if r.status_code == 404: raise ResponseError("API call not found.") elif r.status_code == 402: raise ResponseError("Payment required.") elif r.status_code == 500: raise ResponseError("Server error.") elif r.status_code == 201: shard = Shard() shard.from_json(r.text) return shard else: err = ResponseError("Received status code %s %s" % (r.status_code, r.reason)) err.response = r raise err
class TestShard(unittest.TestCase): def setUp(self): self.cryptkey = ("2b77e64156f9f7eb16d74b98f70417e4" "d665d977d0ef00e793d41767acf13e8c") self.filehash = ("5547a152337de9ff6a97f6f099bb024e" "08af419cee613b18da76a33e581d49ac") self.raw_uri = ( "5547a152337de9ff6a97f6f099bb024e08af419cee613b18da76a33e581d49ac" "?key=2b77e64156f9f7eb16d74b98f70417e4d665d977d0ef00e793d41767acf" "13e8c") # Create empty object self.empty_shard = Shard() # Create half object self.half_shard = Shard(self.filehash) # Create full object self.full_shard = Shard(self.filehash, self.cryptkey) # Create new objects self.shard1 = Shard() self.shard2 = Shard() # Load content self.json_dict = { "key": "2b77e64156f9f7eb16d74b98f70417e4" "d665d977d0ef00e793d41767acf13e8c", "filehash": "5547a152337de9ff6a97f6f099bb024e" "08af419cee613b18da76a33e581d49ac" } self.shard1.from_json(json.dumps(self.json_dict)) self.shard2.from_uri(self.raw_uri) def tearDown(self): del self.empty_shard del self.half_shard del self.full_shard del self.shard1 del self.shard2 del self.json_dict def test_getters_empty_shard(self): def _callable(meth): meth = getattr(self.empty_shard, meth) meth() for method in ['uri', 'get_hashes', 'get_json']: self.assertRaises(ShardError, _callable, method) def test_getters_half_shard(self): def _callable(meth): meth = getattr(self.half_shard, meth) meth() for method in ['uri', 'get_hashes', 'get_json']: self.assertRaises(ShardError, _callable, method) def test_getters_full_shard(self): uri = self.full_shard.uri self.assertEqual(uri, self.raw_uri) hash, key = self.full_shard.get_hashes() self.assertEqual(hash, self.filehash) self.assertEqual(key, self.cryptkey) json_ = self.full_shard.get_json() self.assertEqual(json_, json.dumps(self.json_dict)) def test_shards(self): # Fit everything into one test case self.assertEqual(self.shard1.get_hashes(), self.shard2.get_hashes())
class TestShard(unittest.TestCase): def setUp(self): self.cryptkey = ("2b77e64156f9f7eb16d74b98f70417e4" "d665d977d0ef00e793d41767acf13e8c") self.filehash = ("5547a152337de9ff6a97f6f099bb024e" "08af419cee613b18da76a33e581d49ac") self.raw_uri = ( "5547a152337de9ff6a97f6f099bb024e08af419cee613b18da76a33e581d49ac" "?key=2b77e64156f9f7eb16d74b98f70417e4d665d977d0ef00e793d41767acf" "13e8c") # Create empty object self.empty_shard = Shard() # Create half object self.half_shard = Shard(self.filehash) # Create full object self.full_shard = Shard(self.filehash, self.cryptkey) # Create new objects self.shard1 = Shard() self.shard2 = Shard() # Load content self.json_dict = { "key": "2b77e64156f9f7eb16d74b98f70417e4" "d665d977d0ef00e793d41767acf13e8c", "filehash": "5547a152337de9ff6a97f6f099bb024e" "08af419cee613b18da76a33e581d49ac" } self.shard1.from_json(json.dumps(self.json_dict)) self.shard2.from_uri(self.raw_uri) def tearDown(self): del self.empty_shard del self.half_shard del self.full_shard del self.shard1 del self.shard2 del self.json_dict def test_shard_with_malformed_uri(self): shard = Shard() self.assertRaises(ShardError, shard.from_uri, 'funkymonkey') def test_getters_empty_shard(self): def _callable(meth): meth = getattr(self.empty_shard, meth) meth() for method in ['uri', 'get_hashes', 'get_json']: self.assertRaises(ShardError, _callable, method) def test_getters_half_shard(self): def _callable(meth): meth = getattr(self.half_shard, meth) meth() for method in ['uri', 'get_hashes', 'get_json']: self.assertRaises(ShardError, _callable, method) def test_getters_full_shard(self): uri = self.full_shard.uri self.assertEqual(uri, self.raw_uri) hash, key = self.full_shard.get_hashes() self.assertEqual(hash, self.filehash) self.assertEqual(key, self.cryptkey) json_ = self.full_shard.get_json() self.assertEqual(json_, json.dumps(self.json_dict)) def test_shards(self): # Fit everything into one test case self.assertEqual(self.shard1.get_hashes(), self.shard2.get_hashes())