Skip to content

Commit 9b7fe32

Browse files
ludeeusfabaff
authored andcommitted
Adds support for https and basic auth (#1)
1 parent d31884a commit 9b7fe32

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

glances_api/__init__.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,37 @@
1212
from . import exceptions
1313

1414
_LOGGER = logging.getLogger(__name__)
15-
_RESOURCE = 'http://{host}:{port}/api/{version}'
15+
_RESOURCE = '{schema}://{host}:{port}/api/{version}'
1616

1717

1818
class Glances(object):
1919
"""A class for handling the data retrieval."""
2020

21-
def __init__(self, loop, session, host='localhost', port=61208, version=2):
21+
def __init__(self, loop, session, host='localhost', port=61208, version=2,
22+
ssl=False, username=None, password=None):
2223
"""Initialize the connection."""
24+
schema = 'https' if ssl else 'http'
2325
self._loop = loop
2426
self._session = session
25-
self.url = _RESOURCE.format(host=host, port=port, version=version)
27+
self.url = _RESOURCE.format(schema=schema, host=host, port=port,
28+
version=version)
2629
self.data = None
2730
self.values = None
2831
self.plugins = None
32+
self.username = username
33+
self.password = password
2934

3035
async def get_data(self):
3136
"""Retrieve the data."""
3237
url = '{}/{}'.format(self.url, 'all')
3338

3439
try:
3540
with async_timeout.timeout(5, loop=self._loop):
36-
response = await self._session.get(url)
41+
if self.password is None:
42+
response = await self._session.get(url)
43+
else:
44+
auth = aiohttp.BasicAuth(self.username, self.password)
45+
response = await self._session.get(url, auth=auth)
3746

3847
_LOGGER.debug("Response from Glances API: %s", response.status)
3948
self.data = await response.json()
@@ -58,7 +67,11 @@ async def get_plugins(self):
5867

5968
try:
6069
with async_timeout.timeout(5, loop=self._loop):
61-
response = await self._session.get(url)
70+
if self.password is None:
71+
response = await self._session.get(url)
72+
else:
73+
auth = aiohttp.BasicAuth(self.username, self.password)
74+
response = await self._session.get(url, auth=auth)
6275

6376
_LOGGER.debug("Response from Glances API: %s", response.status)
6477
self.plugins = await response.json()

0 commit comments

Comments
 (0)