Commits

Peter Rudenko committed 6955d97

API refactor

  • Participants
  • Parent commits fc44a60

Comments (0)

Files changed (1)

templeton/src/templeton/api.py

 
 class Templeton(object):
 
-    def get(self, url, data):
+    def __init__(self, user="hdfs"):
+        self.user = user
+
+    def get(self, url, data=None):
         """
         Make GET query to templeton url.
         """
+        if data is not None:
+            data['user.name'] = self.user
+        else:
+            data = {"user.name": self.user}
         data = urllib.urlencode(data)
-        print TEMPLETON_URL + url + "?" + data
         response = urllib2.urlopen(TEMPLETON_URL + url + "?" + data)
         return json.loads(response.read())
 
-    def post(self, url, data):
+    def post(self, url, data=None):
         """
         Make POST query to templeton url.
         """
+        if data is not None:
+            data['user.name'] = self.user
+        else:
+            data = {"user.name": self.user}
         data = urllib.urlencode(data)
         req = urllib2.Request(TEMPLETON_URL + url, data)
         response = urllib2.urlopen(req)
-        return json.loads(response.read()) 
+        return json.loads(response.read())
 
 
-    def pig_query(self, user="hdfs", execute=None, pig_file=None, statusdir=None, callback=None):
+    def pig_query(self, execute=None, pig_file=None, statusdir=None, callback=None):
         """
         Create and queue a Pig job.
 
         """
         if not any([execute, pig_file]):
             raise Exception("""One of either "execcute" or "file" is required""")
-        data = {"user.name": user}
+        data = {}
         if execute:
             data['execute'] = execute
         if pig_file:
         """
         Check the status of a job and get related job information given its job ID.
         """
-        data = {'user.name': user}
-        return self.get("queue/%s" % job_id, data)
+        return self.get("queue/%s" % job_id)
 
     def show_hcat_tables(self, user="hdfs", db="default"):
         """
         List the tables in an HCatalog database.
         """
-        data = {'user.name': user}
-        return self.get("ddl/database/%s/table" % db, data)['tables']
+        return self.get("ddl/database/%s/table" % db)['tables']