Commit 56e45f59 authored by Lucas Carvalho's avatar Lucas Carvalho

implemented the first version of upload and download methods. Upload is using...

implemented the first version of upload and download methods. Upload is using both server already, shacache and shadir.
parent ef7a7da5
......@@ -13,56 +13,126 @@
##############################################################################
import base64
import hashlib
import httplib
import json
import os
from urlparse import urlparse
class NetworkcacheClient(object):
'''
NetworkcacheClient is a wrapper for httplib.
It must implement all the required methods to use the Networkcache HTTP
Server.
- put(file)
- get(key)
It must implement all the required methods to use:
- SHADIR
- SHACACHE
'''
def __init__(self, networkcache_url):
# XXX (lucas): Is it required to check if networkcache_url is a valid URL?
self.networkcache_url = networkcache_url
def _start(self):
self.connection = httplib.HTTPConnection(self.networkcache_url)
def __init__(self, shacache, shadir=None):
''' Set the initial values. '''
# ShaCache Properties
shacache_urlparse = urlparse(shacache)
self.shacache_host = shacache_urlparse.hostname
if shacache_urlparse.port is not None:
self.shacache_host += ':%s' % shacache_urlparse.port
def _close(self):
self.connection.close()
self.shacache_header_dict = {'Content-Type': 'application/json'}
shacache_user = shacache_urlparse.username
shacache_passwd = shacache_urlparse.password
if shacache_user is not None:
authentication_string = '%s:%s' % (shacache_user, shacache_passwd)
base64string = base64.encodestring(authentication_string).strip()
self.shacache_header_dict['Authorization'] = 'Basic %s' % base64string
def put(self, path, file_content):
'''
Upload the file to the server.
It uses http PUT resquest method.
self.shacache_path = '/'
if shacache_urlparse.path not in ('', '/'):
self.shacache_path = shacache_urlparse.path
shacache_urlparse = urlparse(shacache)
self.shacache_host = shacache_urlparse.hostname
if shacache_urlparse.port is not None:
self.shacache_host += ':%s' % shacache_urlparse.port
# ShaDir Properties
if shadir is not None:
shadir_urlparse = urlparse(shadir)
self.shadir_header_dict = {'Content-Type': 'application/json'}
shadir_user = shadir_urlparse.username
shadir_passwd = shadir_urlparse.password
if shadir_user is not None:
authentication_string = '%s:%s' % (shadir_user, shadir_passwd)
base64string = base64.encodestring(authentication_string).strip()
self.shadir_header_dict['Authorization'] = 'Basic %s' % base64string
self.shadir_path = '/'
if shadir_urlparse.path not in ('', '/'):
self.shadir_path = shadir_urlparse.path
shadir_urlparse = urlparse(shadir)
self.shadir_host = shadir_urlparse.hostname
if shadir_urlparse.port is not None:
self.shadir_host += ':%s' % shadir_urlparse.port
def upload(self, file_descriptor, directory_key=None, **kw):
''' Upload the file to the server.
If directory_key is None it must only upload to SHACACHE.
Otherwise, it must create a new entry on SHADIR.
'''
if file_content is None:
raise ValueError('File content should not be None.')
if directory_key is not None and 'urlmd5' not in kw:
msg = 'The parameter "urlmd5" is required once you set directory_key.'
raise ValueError(msg)
self._start()
try:
self.connection.request('PUT', path, file_content)
result = self.connection.getresponse()
finally:
self._close()
file_content = file_descriptor.read()
sha512sum = hashlib.sha512(file_content).hexdigest()
path = os.path.join(self.shacache_path, sha512sum)
self.shacache_connection = httplib.HTTPConnection(self.shacache_host)
self.shacache_connection.request('POST', path, file_content,
self.shacache_header_dict)
result = self.shacache_connection.getresponse()
if result.status != 200:
return result
def get(self, key):
'''
Download the file.
if directory_key is not None:
path = os.path.join(self.shadir_path, directory_key)
file_name = kw.get('file', None)
if file_name is None:
kw['file'] = file_descriptor.name
sha512 = kw.get('sha512', None)
if sha512 is None:
kw['sha512'] = sha512sum
signature = kw.get('signature', None)
if signature is None:
signature = "SIGNATURE"
data = [kw, signature]
self.shadir_connection = httplib.HTTPConnection(self.shadir_host)
self.shadir_connection.request('POST', path, json.dumps(data),
self.shadir_header_dict)
result = self.shadir_connection.getresponse()
return result
def download(self, sha512sum):
''' Download the file.
It uses http GET request method.
'''
path_info = '/%s' % key
self._start()
try:
self.connection.request('GET', path_info)
result = self.connection.getresponse()
finally:
self._close()
path_info = os.path.join(self.shacache_path, sha512sum)
self.shacache_connection = httplib.HTTPConnection(self.shacache_host)
self.shacache_connection.request('GET', path_info, headers=self.shacache_header_dict)
result = self.shacache_connection.getresponse()
if result.status != 200:
raise FileNotFound(result.read())
return result
def close(self):
''' It must be called to close the HTTP Connection. '''
self.shacache_connection.close()
self.shadir_connection.close()
class FileNotFound(Exception):
pass
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