Skip to content

Commit f2ab735

Browse files
committed
Add token authorization and cafile / https support. Fixes #3 #5
1 parent e537a85 commit f2ab735

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

hawkular/metrics.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,30 @@ def __init__(self,
7878
tenant_id,
7979
host='localhost',
8080
port=8080,
81-
path='hawkular/metrics'):
81+
path='hawkular/metrics',
82+
scheme='http',
83+
cafile=None,
84+
token=None):
8285
"""
8386
A new instance of HawkularMetricsClient is created with the following defaults:
8487
8588
host = localhost
8689
port = 8081
8790
path = hawkular-metrics
91+
scheme = http
92+
cafile = None
8893
8994
The url that is called by the client is:
9095
91-
http://{host}:{port}/{2}/
96+
{scheme}://{host}:{port}/{2}/
9297
"""
9398
self.tenant_id = tenant_id
9499
self.host = host
95100
self.port = port
96101
self.path = path
102+
self.cafile = cafile
103+
self.scheme = scheme
104+
self.token = token
97105

98106
opener = build_opener(HawkularHTTPErrorProcessor())
99107
install_opener(opener)
@@ -106,7 +114,7 @@ def _clean_metric_id(metric_id):
106114
return quote(metric_id, '')
107115

108116
def _get_base_url(self):
109-
return "http://{0}:{1}/{2}/".format(self.host, str(self.port), self.path)
117+
return "{0}://{1}:{2}/{3}/".format(self.scheme, self.host, str(self.port), self.path)
110118

111119
def _get_url(self, metric_type):
112120
return self._get_base_url() + '{0}'.format(metric_type)
@@ -138,6 +146,9 @@ def _http(self, url, method, data=None):
138146
req.add_header('Content-Type', 'application/json')
139147
req.add_header('Hawkular-Tenant', self.tenant_id)
140148

149+
if self.token is not None:
150+
req.add_header('Authorization', 'Bearer {0}'.format(self.token))
151+
141152
if not isinstance(data, str):
142153
data = json.dumps(data, indent=2)
143154

0 commit comments

Comments
 (0)