Skip to content

Commit

Permalink
Merge pull request #72 from algorys/develop
Browse files Browse the repository at this point in the history
Add proxy management to client, by login function
  • Loading branch information
mohierf authored Mar 22, 2018
2 parents 614c867 + 776b0fe commit 287bb58
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions alignak_backend_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
BACKEND_PAGINATION_LIMIT = 50
BACKEND_PAGINATION_DEFAULT = 25

# Proxy protocols
PROXY_PROTOCOLS = ['http', 'https']

# Connection error code
BACKEND_ERROR = 1000

Expand Down Expand Up @@ -136,6 +139,7 @@ def __init__(self, endpoint, processes=1):

self.authenticated = False
self._token = None
self.proxies = None

self.timeout = None # TODO: Add this option in config file

Expand Down Expand Up @@ -174,7 +178,8 @@ def get_response(self, method, endpoint, headers=None, json=None, params=None, d
# First stage. Errors are connection errors (timeout, no session, ...)
try:
response = self.session.request(method=method, url=url, headers=headers, json=json,
params=params, data=data, timeout=self.timeout)
params=params, data=data, proxies=self.proxies,
timeout=self.timeout)
logger.debug("response headers: %s", response.headers)
logger.debug("response content: %s", response.content)
except RequestException as e:
Expand Down Expand Up @@ -235,7 +240,7 @@ def get_token(self):

token = property(get_token, set_token)

def login(self, username, password, generate='enabled'):
def login(self, username, password, generate='enabled', proxies=None):
"""
Log into the backend and get the token
Expand All @@ -256,6 +261,8 @@ def login(self, username, password, generate='enabled'):
:type password: str
:param generate: Can have these values: enabled | force | disabled
:type generate: str
:param proxies: dict of proxy (http and / or https)
:type proxies: dict
:return: return True if authentication is successfull, otherwise False
:rtype: bool
"""
Expand All @@ -264,6 +271,14 @@ def login(self, username, password, generate='enabled'):
if not username or not password:
raise BackendException(BACKEND_ERROR, "Missing mandatory parameters")

if proxies:
for key in proxies.keys():
try:
assert key in PROXY_PROTOCOLS
except AssertionError:
raise BackendException(BACKEND_ERROR, "Wrong proxy protocol ", key)
self.proxies = proxies

endpoint = 'login'
json = {u'username': username, u'password': password}
if generate == 'force':
Expand Down

0 comments on commit 287bb58

Please sign in to comment.