Skip to content

Commit

Permalink
Add Web Service client to backend class
Browse files Browse the repository at this point in the history
Ref #279
  • Loading branch information
algorys committed Apr 10, 2018
1 parent ef77920 commit 82be6c3
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
5 changes: 5 additions & 0 deletions alignak_app/backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from alignak_backend_client.client import Backend, BackendException

from alignak_app.backend.datamanager import data_manager
from alignak_app.backend.alignak_ws_client import WSClient

from alignak_app.items.daemon import Daemon
from alignak_app.items.event import Event
Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(self):
self.backend = None
self.connected = False
self.user = {}
self.ws_client = WSClient()

def login(self, username=None, password=None, proxies=None, check=False):
"""
Expand Down Expand Up @@ -126,6 +128,9 @@ def login(self, username=None, password=None, proxies=None, check=False):
'Check [Alignak] section in configuration file or use login window of application.'
)

if self.connected and check:
self.ws_client.login(self.user['token'])

return self.connected

def get(self, endpoint, params=None, projection=None, all_items=False):
Expand Down
173 changes: 173 additions & 0 deletions alignak_app/backend/ws_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2015-2018:
# Matthieu Estrada, ttamalfor@gmail.com
#
# This file is part of (AlignakApp).
#
# (AlignakApp) is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# (AlignakApp) is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with (AlignakApp). If not, see <http://www.gnu.org/licenses/>.

"""
WS Client
+++++++++
WS Client manage connection and access to Alignak backend by the Web Service module. It will
also fill :class:`DataManager <alignak_app.backend.datamanager.DataManager>` in future versions.
"""

import json
import requests

from requests import HTTPError
from requests import ConnectionError as RequestsConnectionError

from logging import getLogger

from alignak_app.utils.config import settings

logger = getLogger(__name__)


class WSClient(object):
"""
Class who manage Web Service requests
"""

def __init__(self):
self.ws_backend = ''
self.token = ''
self.auth = None

def login(self, token):
"""
Login to Web Service. Used only to ensure that the user token is identical
:param token: user's token of App
:type token: str
"""

self.ws_backend = settings.get_config('Alignak', 'webservice')

try:
# WS login
logger.info("Requesting Web Service authentication with token: %s", token)
headers = {'Content-Type': 'application/json'}
params = {'username': token, 'password': ''}
response = requests.post(
'/'.join([self.ws_backend, 'login']), json=params, headers=headers
)
resp = response.json()

if '_result' in resp:
assert token == resp['_result'][0]
self.token = token
self.auth = requests.auth.HTTPBasicAuth(self.token, '')
except (RequestsConnectionError, AssertionError) as exp:
logger.error(exp)

def get(self, endpoint, params):
"""
GET on alignak Backend Web Service
:param endpoint: WS endpoint
:type endpoint: str
:param params: dict of parameters for the WS
:type params: dict|None
:return: request result
:rtype: dict
"""

request = None

try:
request = requests.get(
'/'.join([self.ws_backend, endpoint]), params=params, auth=self.auth
)
logger.info('GET on [%s] web service > %s', endpoint, str(request))

request.raise_for_status()
logger.debug('\tparams: [%s]', str(params))

except RequestsConnectionError as exp:
logger.error("Backend connection error, error: %s", exp)

return request.json()

def post(self, endpoint, params=None):
"""
Post on alignak Backend Web Service
:param endpoint: WS endpoint
:type endpoint: str
:param params: dict of parameters for the WS
:type params: dict
:return: list of properties when query item | list of items when get many items
:rtype: list
"""

request = None

try:
headers = {'Content-Type': 'application/json'}
params = json.dumps(params)

request = requests.post(
'/'.join([self.ws_backend, endpoint]), data=params, headers=headers, auth=self.auth
)

request.raise_for_status()
logger.info('POST on [%s] backend > %s', endpoint, str(request))
logger.debug('\tdata: [%s]', str(data))
logger.debug('\theaders: [%s]', str(headers))
except (RequestsConnectionError, HTTPError) as exp:
logger.warning("WS connection error, error: %s", str(exp))

except Exception as exp:
logger.exception("WS exception, error: %s", exp)

return request.json()


if __name__ == '__main__':
import os
from alignak_app.backend.backend import app_backend
os.environ['ALIGNAKAPP_APP_DIR'] = '/usr/local/alignak_app'
os.environ['ALIGNAKAPP_USR_DIR'] = '/home/algorys/.local/alignak_app'
settings.init_config()
app_backend.login(
settings.get_config('Alignak', 'username'),
settings.get_config('Alignak', 'password')
)

ws_client = WSClient()
ws_client.login(app_backend.user['token'])

# ACKNOWLEDGE_HOST_PROBLEM;<host_name>;<sticky>;<notify>;<persistent>;<author>;<comment>
# data = {
# 'command': 'ACKNOWLEDGE_HOST_PROBLEM',
# "element": "pc_matt",
# "parameters": "pc_matt;True;True;%s;Test ACK" % user_id
# }

# CHANGE_HOST_CHECK_COMMAND;<host_name>;<check_command>
# ACKNOWLEDGE_SVC_PROBLEM;<service_name>;<sticky>;<notify>;<persistent>;<author>;<comment>
data = {
'command': 'ACKNOWLEDGE_SVC_PROBLEM',
'element': 'pc_matt',
'parameters': 'Cpu;2;1;0;%s;Test Ack Service' % 'admin'
}

test = ws_client.post('command', data)

print(test)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests
pyqt5<5.10
configparser
future
Expand Down

0 comments on commit 82be6c3

Please sign in to comment.