Commit 885320af authored by André Anjos's avatar André Anjos 💬

[utils] Remove outdated scheduler communication bridge

parent f3a4582c
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform 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. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from django.conf import settings
import httplib
import logging
import traceback
import urlparse
import urllib
import simplejson as json
logger = logging.getLogger(__name__)
#----------------------------------------------------------
def _connect(url, port):
parsed_url = urlparse.urlparse(url)
host_and_port = parsed_url.netloc.split(':')
if port is None:
if len(host_and_port) == 2:
host, port = host_and_port
else:
host = host_and_port
if parsed_url.scheme == 'https':
port = 443
else:
port = 80
else:
host = host_and_port[0]
if parsed_url.scheme == 'https':
connection = httplib.HTTPSConnection(host, port)
else:
connection = httplib.HTTPConnection(host, port)
try:
connection.connect()
except:
logger.error('Failed to establish a connection with the scheduler API, reason: %s' % traceback.format_exc())
return None
return connection
#----------------------------------------------------------
def _combine_url_parameters(url, params):
"""Combines the given URL with the parameter dictionary"""
parsed_url = urlparse.urlparse(url)
qs = []
if parsed_url.query:
qs.extend([parsed_url.query, '&'])
qs.append(urllib.urlencode(params, doseq=True))
return urlparse.urlunparse((
parsed_url[0],
parsed_url[1],
parsed_url[2],
parsed_url[3],
''.join(qs),
parsed_url[5]
))
#----------------------------------------------------------
def _sendMessage(url, params=None, data=None, msg_type='PUT'):
# Establish the connection
connection = _connect(settings.SCHEDULER_ADDRESS, settings.SCHEDULER_PORT)
if connection is None:
return None
# Send the request
headers = {}
if data is not None:
data = json.dumps(data)
headers['Content-Type'] = 'application/json'
else:
headers['Content-Length'] = 0
try:
if params:
full_url = _combine_url_parameters(url, params)
else:
full_url = url
connection.request(msg_type, full_url, data, headers)
response = connection.getresponse()
data = response.read()
except:
logger.error('Failed to use the Web API, reason: %s' % traceback.format_exc())
return None
finally:
connection.close()
return (response.status, data)
#----------------------------------------------------------
def putMessage(url, params=None, data=None):
return _sendMessage(url, params=params, data=data, msg_type='PUT')
#----------------------------------------------------------
def postMessage(url, params=None, data=None):
return _sendMessage(url, params=params, data=data, msg_type='POST')
#----------------------------------------------------------
def getMessage(url, params=None, data=None):
return _sendMessage(url, params=params, data=data, msg_type='GET')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment