Skip to content

Commit

Permalink
Methode for http basic auth with username and password added (#80)
Browse files Browse the repository at this point in the history
* Added methode to allow http basic auth with username and password to BaseDruidClient

* added missing import

* show full error message
  • Loading branch information
DPiontek authored and gianm committed Jan 19, 2018
1 parent d2468f1 commit 30105f0
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pydruid/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
from six.moves import urllib

from pydruid.query import QueryBuilder

from base64 import b64encode

class BaseDruidClient(object):
def __init__(self, url, endpoint):
self.url = url
self.endpoint = endpoint
self.query_builder = QueryBuilder()
self.username = None
self.password = None

def set_basic_auth_credentials(self, username, password):
self.username = username
self.password = password

def _prepare_url_headers_and_body(self, query):
querystr = json.dumps(query.query_dict).encode('utf-8')
Expand All @@ -37,6 +43,11 @@ def _prepare_url_headers_and_body(self, query):
else:
url = self.url + '/' + self.endpoint
headers = {'Content-Type': 'application/json'}
if (self.username is not None) and (self.password is not None):
username_password = \
b64encode(bytes('{}:{}'.format(self.username, self.password)))
headers['Authorization'] = 'Basic {}'.format(username_password)

return headers, querystr, url

def _post(self, query):
Expand Down Expand Up @@ -481,8 +492,6 @@ def _post(self, query):
err = json.loads(e.read().decode("utf-8"))
except (ValueError, AttributeError, KeyError):
pass
else:
err = err.get('error', None)

raise IOError('{0} \n Druid Error: {1} \n Query is: {2}'.format(
e, err, json.dumps(query.query_dict, indent=4)))
Expand Down

0 comments on commit 30105f0

Please sign in to comment.