python-awips/awips/ThriftClient.py

102 lines
3.6 KiB
Python
Raw Permalink Normal View History

2015-06-12 11:57:06 -06:00
##
# This software was developed and / or modified by Raytheon Company,
# pursuant to Contract DG133W-05-CQ-1067 with the US Government.
2016-03-16 16:32:17 -05:00
#
2015-06-12 11:57:06 -06:00
# U.S. EXPORT CONTROLLED TECHNICAL DATA
# This software product contains export-restricted data whose
# export/transfer/disclosure is restricted by U.S. law. Dissemination
# to non-U.S. persons whether in the United States or abroad requires
# an export license or other authorization.
2016-03-16 16:32:17 -05:00
#
2015-06-12 11:57:06 -06:00
# Contractor Name: Raytheon Company
# Contractor Address: 6825 Pine Street, Suite 340
# Mail Stop B8
# Omaha, NE 68106
# 402.291.0100
2016-03-16 16:32:17 -05:00
#
2015-06-12 11:57:06 -06:00
# See the AWIPS II Master Rights File ("Master Rights File.pdf") for
# further licensing information.
##
import httplib
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 = httplib.HTTPConnection(hostString)
else:
if (port is None):
self.__httpConn = httplib.HTTPConnection(host)
else:
self.__httpConn = httplib.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-03-16 16:32:17 -05:00
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")
2016-03-16 16:32:17 -05:00
2015-06-12 11:57:06 -06:00
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