python-awips/awips/ThriftClient.py

87 lines
3 KiB
Python
Raw Permalink Normal View History

2015-06-12 11:57:06 -06:00
##
##
try:
import http.client as httpcl
except ImportError:
import httplib as httpcl
2015-06-12 11:57:06 -06:00
from dynamicserialize import DynamicSerializationManager
from dynamicserialize.dstypes.com.raytheon.uf.common.serialization.comm.response import ServerErrorResponse
from dynamicserialize.dstypes.com.raytheon.uf.common.serialization import SerializableExceptionWrapper
#
# Provides a Python-based interface for executing Thrift requests.
#
2016-03-16 16:32:17 -05:00
#
#
2015-06-12 11:57:06 -06:00
# SOFTWARE HISTORY
2016-03-16 16:32:17 -05:00
#
2015-06-12 11:57:06 -06:00
# Date Ticket# Engineer Description
# ------------ ---------- ----------- --------------------------
# 09/20/10 dgilling Initial Creation.
#
2016-03-16 16:32:17 -05:00
#
#
2015-06-12 11:57:06 -06:00
class ThriftClient:
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
# How to call this constructor:
2016-03-16 16:32:17 -05:00
# 1. Pass in all arguments separately (e.g.,
2015-06-12 11:57:06 -06:00
# ThriftClient.ThriftClient("localhost", 9581, "/services"))
# will return a Thrift client pointed at http://localhost:9581/services.
2016-03-16 16:32:17 -05:00
# 2. Pass in all arguments through the host string (e.g.,
2015-06-12 11:57:06 -06:00
# ThriftClient.ThriftClient("localhost:9581/services"))
# will return a Thrift client pointed at http://localhost:9581/services.
2016-03-16 16:32:17 -05:00
# 3. Pass in host/port arguments through the host string (e.g.,
2015-06-12 11:57:06 -06:00
# ThriftClient.ThriftClient("localhost:9581", "/services"))
# will return a Thrift client pointed at http://localhost:9581/services.
def __init__(self, host, port=9581, uri="/services"):
hostParts = host.split("/", 1)
if (len(hostParts) > 1):
hostString = hostParts[0]
self.__uri = "/" + hostParts[1]
self.__httpConn = httpcl.HTTPConnection(hostString)
2015-06-12 11:57:06 -06:00
else:
if (port is None):
self.__httpConn = httpcl.HTTPConnection(host)
2015-06-12 11:57:06 -06:00
else:
self.__httpConn = httpcl.HTTPConnection(host, port)
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
self.__uri = uri
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
self.__dsm = DynamicSerializationManager.DynamicSerializationManager()
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
def sendRequest(self, request, uri="/thrift"):
message = self.__dsm.serializeObject(request)
2016-04-18 22:43:57 -06:00
#message = message.decode('cp437')
2015-06-12 11:57:06 -06:00
self.__httpConn.connect()
self.__httpConn.request("POST", self.__uri + uri, message)
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
response = self.__httpConn.getresponse()
if (response.status != 200):
raise ThriftRequestException("Unable to post request to server")
rval = self.__dsm.deserializeBytes(response.read())
self.__httpConn.close()
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
# let's verify we have an instance of ServerErrorResponse
# IF we do, through an exception up to the caller along
# with the original Java stack trace
# ELSE: we have a valid response and pass it back
try:
2016-03-16 16:32:17 -05:00
forceError = rval.getException()
2015-06-12 11:57:06 -06:00
raise ThriftRequestException(forceError)
except AttributeError:
pass
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
return rval
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
class ThriftRequestException(Exception):
def __init__(self, value):
self.parameter = value
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
def __str__(self):
return repr(self.parameter)
2016-03-16 16:32:17 -05:00