Skip to content

Commit

Permalink
query implementations share more post related code
Browse files Browse the repository at this point in the history
  • Loading branch information
Deep Ganguli committed Nov 21, 2013
1 parent ed5a9a6 commit 27e87cd
Showing 1 changed file with 25 additions and 43 deletions.
68 changes: 25 additions & 43 deletions pydruid/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ def __init__(self,url,endpoint):
self.query_type = None

def post(self,query):
querystr = json.dumps(query)
url = self.url + '/' + self.endpoint
headers = {'Content-Type' : 'application/json'}
req = urllib2.Request(url, querystr, headers)
res = urllib2.urlopen(req)
data = res.read()
self.result_json = data;
self.querystr = querystr
res.close()
try:
querystr = json.dumps(query)
url = self.url + '/' + self.endpoint
headers = {'Content-Type' : 'application/json'}
req = urllib2.Request(url, querystr, headers)
res = urllib2.urlopen(req)
data = res.read()
self.result_json = data;
res.close()
except urllib2.HTTPError, e:
raise IOError('Malformed query: \n {0}'.format(json.dumps(self.query_dict, indent = 4)))
else:
self.result = self.parse()
parsed = self.parse()
return parsed

def parse(self):
if self.result_json:
Expand Down Expand Up @@ -126,16 +132,9 @@ def timeseries(self, **args):
else:
query_dict[key] = val

self.query_dict = query_dict

try:
self.post(query_dict)
except urllib2.HTTPError, e:
raise IOError('Malformed query: \n {0}'.format(json.dumps(self.query_dict ,indent = 4)))
else:
self.result = self.parse()
self.query_type = "timeseries"
return self.result
self.query_dict = query_dict
self.query_type = 'timeseries'
return self.post(query_dict)

def groupBy(self, **args):

Expand All @@ -156,15 +155,8 @@ def groupBy(self, **args):
query_dict[key] = val

self.query_dict = query_dict

try:
self.post(query_dict)
except urllib2.HTTPError, e:
raise IOError('Malformed query: \n {0}'.format(json.dumps(self.query_dict ,indent = 4)))
else:
self.result = self.parse()
self.query_type = "groupby"
return self.parse()
self.query_type = 'groupby'
return self.post(query_dict)

def segmentMetadata(self, **args):

Expand All @@ -178,14 +170,8 @@ def segmentMetadata(self, **args):
query_dict[key] = val

self.query_dict = query_dict

try:
self.post(query_dict)
except urllib2.HTTPError, e:
raise IOError('Malformed query: \n {0}'.format(json.dumps(self.query_dict ,indent = 4)))
else:
self.result = self.parse()
return self.parse()
self.query_type = 'segmentMetadata'
return self.post(query_dict)

def timeBoundary(self, **args):

Expand All @@ -198,10 +184,6 @@ def timeBoundary(self, **args):
else:
query_dict[key] = val

try:
self.post(query_dict)
except urllib2.HTTPError, e:
raise IOError('Malformed query: \n {0}'.format(json.dumps(self.query_dict ,indent = 4)))
else:
self.result = self.parse()
return self.parse()
self.query_dict = query_dict
self.query_type = 'timeBoundary'
return self.post(query_dict)

0 comments on commit 27e87cd

Please sign in to comment.