8000 Move basic auth handler into exposition. Update docstring. · insequent/client_python@7e45925 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e45925

Browse files
committed
Move basic auth handler into exposition. Update docstring.
1 parent 0bebb0a commit 7e45925

File tree

6 files changed

+24
-23
lines changed

6 files changed

+24
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ you can use a special handler to set the Authorization header.
328328

329329
```python
330330
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
331-
from prometheus_client.handlers.basic_auth import handler as basic_auth_handler
331+
from prometheus_client.exposition import basic_auth_handler
332332

333333
def my_auth_handler(url, method, timeout, headers, data):
334334
username = 'foobar'

prometheus_client/exposition.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import threading
99
from contextlib import closing
1010
from wsgiref.simple_server import make_server
11+
import base64
1112

1213
from . import core
1314
try:
@@ -119,8 +120,10 @@ def write_to_textfile(path, registry):
119120

120121

121122
def default_handler(url, method, timeout, headers, data):
123+
'''Default handler that implements HTTP/HTTPS connections.
124+
125+
Used by the push_to_gateway functions. Can be re-used by other handlers.'''
122126
def handle():
123-
'''Default handler that implements HTTP/HTTPS connections.'''
124127
request = Request(url, data=data)
125128
request.get_method = lambda: method
126129
for k, v in headers:
@@ -133,6 +136,23 @@ def handle():
133136
return handle
134137

135138

139+
def basic_auth_handler(url, method, timeout, headers, data, username=None, password=None):
140+
'''Handler that implements HTTP/HTTPS connections with Basic Auth.
141+
142+
Sets auth headers using supplied 'username' and 'password', if set.
143+
Used by the push_to_gateway functions. Can be re-used by other handlers.'''
144+
def handle():
145+
'''Handler that implements HTTP Basic Auth.
146+
'''
147+
if username is not None and password is not None:
148+
auth_value = "{0}:{1}".format(username, password)
149+
auth_header = "Basic {0}".format(base64.b64encode(bytes(auth_value)))
150+
headers.append(['Authorization', auth_header])
151+
default_handler(url, method, timeout, headers, data)()
152+
153+
return handle
154+
155+
136156
def push_to_gateway(gateway, job, registry, grouping_key=None, timeout=None, handler=default_handler):
137157
'''Push metrics to the given pushgateway.
138158

prometheus_client/handlers/__init__.py

Whitespace-only changes.

prometheus_client/handlers/basic_auth.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
license = "Apache Software License 2.0",
1212
keywords = "prometheus monitoring instrumentation client",
1313
url = "https://github.com/prometheus/client_python",
14-
packages=['prometheus_client', 'prometheus_client.bridge', 'prometheus_client.twisted', 'prometheus_client.handlers'],
14+
packages=['prometheus_client', 'prometheus_client.bridge', 'prometheus_client.twisted'],
1515
extras_require={
1616
'twisted': ['twisted'],
1717
},

tests/test_exposition.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
from prometheus_client import CollectorRegistry, generate_latest
1414
from prometheus_client import push_to_gateway, pushadd_to_gateway, delete_from_gateway
1515
from prometheus_client import CONTENT_TYPE_LATEST, instance_ip_grouping_key
16-
from prometheus_client.exposition import default_handler
17-
from prometheus_client.handlers.basic_auth import handler as basic_auth_handler
16+
from prometheus_client.exposition import default_handler, basic_auth_handler
1817

1918
try:
2019
from BaseHTTPServer import BaseHTTPRequestHandler

0 commit comments

Comments
 (0)
0