Skip to content

Commit

Permalink
Added the functionality to include a hive in the interface (issue 2).
Browse files Browse the repository at this point in the history
  • Loading branch information
haz committed Jun 5, 2012
1 parent 9e81ba4 commit b2746f1
Showing 1 changed file with 72 additions and 42 deletions.
114 changes: 72 additions & 42 deletions python/buzzdata.py
Expand Up @@ -78,17 +78,21 @@ class DataRoom(API):
"""
Class that represents a data room.
"""
def __init__(self, user, dataroom, api = None):
def __init__(self, user, dataroom, api = None, hive = ''):
if not isinstance(user, User):
self.user = User(user, api)
else:
self.user = user

self.dataroom = dataroom
self.api = api
self.hive = hive

if hive:
self.hive += '.'

@staticmethod
def create(user, api, name, public, readme, license, topics):
def create(user, api, name, public, readme, license, topics, hive = ''):
"""
Create a new data room on BuzzData and return the
corresponding DataRoom object.
Expand All @@ -101,14 +105,17 @@ def create(user, api, name, public, readme, license, topics):
readme -- string of the readme text for the data room
license -- string of the license for the data room
topics -- array of topics for the data room
hive -- name of the hive for the data room
"""
room_details = {'name':name,
'public':public,
'readme':readme,
'license':license,
'topics':topics}
params = {'api_key': api, 'dataset':room_details}
url = "https://buzzdata.com/api/%s/datasets" % str(user)
if hive:
hive += '.'
url = "https://%sbuzzdata.com/api/%s/datasets" % (hive, str(user))
room = DataRoom(user, name, api)
response = room.post(url, params)
return (response, room)
Expand All @@ -120,7 +127,7 @@ def destroy(self):
if not self.api:
return "Error: Must specify an api."
params = {'api_key': self.api}
url = "https://buzzdata.com/api/%s/%s" % (str(self.user), self.dataroom)
url = "https://%sbuzzdata.com/api/%s/%s" % (self.hive, str(self.user), self.dataroom)
return self.delete(url, params)

def details(self):
Expand All @@ -130,7 +137,7 @@ def details(self):
params = {}
if self.api:
params['api_key'] = self.api
url = "https://buzzdata.com/api/%s/%s" % (str(self.user), self.dataroom)
url = "https://%sbuzzdata.com/api/%s/%s" % (self.hive, str(self.user), self.dataroom)
return self.get(url, params)

def list_datafiles(self):
Expand All @@ -140,7 +147,7 @@ def list_datafiles(self):
params = {}
if self.api:
params['api_key'] = self.api
url = "https://buzzdata.com/api/%s/%s/list_datafiles" % (str(self.user), self.dataroom)
url = "https://%sbuzzdata.com/api/%s/%s/list_datafiles" % (self.hive, str(self.user), self.dataroom)
return self.get(url, params)

def create_datafile(self, name):
Expand All @@ -150,7 +157,7 @@ def create_datafile(self, name):
if not self.api:
return "Error: Must specify an api."
params = {'data_file_name': name, 'api_key': self.api}
url = "https://buzzdata.com/api/%s/%s/create_datafile" % (str(self.user), self.dataroom)
url = "https://%sbuzzdata.com/api/%s/%s/create_datafile" % (self.hive, str(self.user), self.dataroom)

response = self.post(url, params)
if 'datafile_uuid' not in response:
Expand All @@ -172,6 +179,7 @@ class DataFile(API):
def __init__(self, dataroom, uuid):
self.dataroom = dataroom
self.api = dataroom.api
self.hive = dataroom.hive
self.uuid = uuid
self.stage = None

Expand All @@ -182,7 +190,7 @@ def history(self):
params = {}
if self.api:
params['api_key'] = self.api
url = "https://buzzdata.com/api/data_files/%s/history" % self.uuid
url = "https://%sbuzzdata.com/api/data_files/%s/history" % (self.hive, self.uuid)
return self.get(url, params)

def download(self, version=None, filename=None):
Expand All @@ -202,7 +210,7 @@ def download(self, version=None, filename=None):
params['version'] = version
if self.api:
params['api_key'] = self.api
url = "https://buzzdata.com/api/%s/%s/%s/download_request" % (self.dataroom.user, self.dataroom, self.uuid)
url = "https://%sbuzzdata.com/api/%s/%s/%s/download_request" % (self.hive, self.dataroom.user, self.dataroom, self.uuid)
location = self.post(url, params)['download_request']['url']
u = urllib2.urlopen(location)
if not filename:
Expand All @@ -225,7 +233,7 @@ def upload(self, filename, release_notes):
if not self.api:
return "Error: Must specify an api."
params = {'api_key':self.api, 'datafile_uuid':self.uuid}
url = "https://buzzdata.com/api/%s/%s/upload_request" % (self.dataroom.user, self.dataroom)
url = "https://%sbuzzdata.com/api/%s/%s/upload_request" % (self.hive, self.dataroom.user, self.dataroom)
response = self.post(url, params)
upload_code = response['upload_request']['upload_code']
upload_url = response['upload_request']['url']
Expand Down Expand Up @@ -311,9 +319,13 @@ class User(API):
"""
Class that represents a BuzzData user.
"""
def __init__(self, user, api = None):
def __init__(self, user, api = None, hive = ''):
self.user = user
self.api = api
self.hive = hive

if hive:
self.hive += '.'

def details(self):
"""
Expand All @@ -322,7 +334,7 @@ def details(self):
params = {}
if self.api:
params['api_key'] = self.api
url = "https://buzzdata.com/api/%s" % self.user
url = "https://%sbuzzdata.com/api/%s" % (self.hive, self.user)
return self.get(url, params)

def list_datarooms(self):
Expand All @@ -332,7 +344,7 @@ def list_datarooms(self):
params = {}
if self.api:
params['api_key'] = self.api
url = "https://buzzdata.com/api/%s/datasets/list" % self.user
url = "https://%sbuzzdata.com/api/%s/datasets/list" % (self.hive, self.user)
return self.get(url, params)

def __str__(self):
Expand All @@ -349,14 +361,15 @@ class Stage(API):
def __init__(self, datafile):
self.datafile = datafile
self.api = datafile.api
self.hive = datafile.hive
self.load_stage()

def load_stage(self):
"""Start (load) a new stage for updating."""
if not self.api:
return "Error: Must specify an api."
params = {'api_key': self.api}
url = "https://buzzdata.com/api/%s/%s/%s/stage" % (self.datafile.dataroom.user, self.datafile.dataroom, self.datafile)
url = "https://%sbuzzdata.com/api/%s/%s/%s/stage" % (self.hive, self.datafile.dataroom.user, self.datafile.dataroom, self.datafile)
response = self.post(url, params)
self.stage_id = response['id']

Expand All @@ -368,10 +381,11 @@ def insert_rows(self, rows):
'stage_id':self.stage_id,
'api_key':self.api,
'rows':json.dumps(rows)}
url = "https://buzzdata.com/api/%s/%s/%s/stage/%s/rows" % (self.datafile.dataroom.user,
self.datafile.dataroom,
self.datafile,
self.stage_id)
url = "https://%sbuzzdata.com/api/%s/%s/%s/stage/%s/rows" % (self.hive,
self.datafile.dataroom.user,
self.datafile.dataroom,
self.datafile,
self.stage_id)
return self.www_post(url, params)

def update_row(self, row, row_num):
Expand All @@ -380,23 +394,25 @@ def update_row(self, row, row_num):
return "Error: Must specify an api."
params = {'api_key':self.api,
'row':json.dumps(row)}
url = "https://buzzdata.com/api/%s/%s/%s/stage/%s/rows/%d" % (self.datafile.dataroom.user,
self.datafile.dataroom,
self.datafile,
self.stage_id,
row_num)
url = "https://%sbuzzdata.com/api/%s/%s/%s/stage/%s/rows/%d" % (self.hive,
self.datafile.dataroom.user,
self.datafile.dataroom,
self.datafile,
self.stage_id,
row_num)
return self.put(url, params, True)

def delete_row(self, row_num):
"""Delete a specific row from the data file."""
if not self.api:
return "Error: Must specify an api."
params = {'api_key':self.api}
url = "https://buzzdata.com/api/%s/%s/%s/stage/%s/rows/%d" % (self.datafile.dataroom.user,
self.datafile.dataroom,
self.datafile,
self.stage_id,
row_num)
url = "https://%sbuzzdata.com/api/%s/%s/%s/stage/%s/rows/%d" % (self.hive,
self.datafile.dataroom.user,
self.datafile.dataroom,
self.datafile,
self.stage_id,
row_num)
return self.delete(url, params)

def commit(self):
Expand All @@ -406,10 +422,11 @@ def commit(self):
params = {'datafile_uuid':str(self.datafile),
'stage_id':self.stage_id,
'api_key':self.api}
url = "https://buzzdata.com/api/%s/%s/%s/stage/%s/commit" % (self.datafile.dataroom.user,
self.datafile.dataroom,
self.datafile,
self.stage_id)
url = "https://%sbuzzdata.com/api/%s/%s/%s/stage/%s/commit" % (self.hive,
self.datafile.dataroom.user,
self.datafile.dataroom,
self.datafile,
self.stage_id)
# Record the response and sleep to avoid server issues
resp = self.post(url, params)
time.sleep(1)
Expand All @@ -421,10 +438,11 @@ def rollback(self):
return "Error: Must specify an api."
params = {'datafile_uuid':str(self.datafile),
'api_key':self.api}
url = "https://buzzdata.com/api/%s/%s/%s/stage/%s/rollback" % (self.datafile.dataroom.user,
self.datafile.dataroom,
self.datafile,
self.stage_id)
url = "https://%sbuzzdata.com/api/%s/%s/%s/stage/%s/rollback" % (self.hive,
self.datafile.dataroom.user,
self.datafile.dataroom,
self.datafile,
self.stage_id)
return self.post(url, params)

def __str__(self):
Expand All @@ -433,20 +451,32 @@ def __str__(self):
def __repr__(self):
return self.__str__()

def buzz_search(query, api = None):
def buzz_search(query, api = None, hive = ''):
"""Search BuzzData for a particular query."""
params = {'query':query}
if api:
params['api_key'] = api
return API().get('https://buzzdata.com/api/search', params)
if hive:
hive += '.'
return API().get("https://%sbuzzdata.com/api/search" % hive, params)

def buzz_licenses():
def buzz_licenses(api = None, hive = ''):
"""Fetch the list of BuzzData licenses."""
return API().get('https://buzzdata.com/api/licenses', {})
params = {}
if api:
params['api_key'] = api
if hive:
hive += '.'
return API().get("https://%sbuzzdata.com/api/licenses" % hive, params)

def buzz_topics():
def buzz_topics(api = None, hive = ''):
"""Fetch the list of BuzzData topics."""
return API().get('https://buzzdata.com/api/topics', {})
params = {}
if api:
params['api_key'] = api
if hive:
hive += '.'
return API().get("https://%sbuzzdata.com/api/topics" % hive, params)



Expand Down

0 comments on commit b2746f1

Please sign in to comment.