Issues (227)

awips/ThriftClient.py (1 issue)

1
#
2
# Provides a Python-based interface for executing Thrift requests.
3
#
4
#
5
#
6
#     SOFTWARE HISTORY
7
#
8
#    Date            Ticket#       Engineer       Description
9
#    ------------    ----------    -----------    --------------------------
10
#    09/20/10                      dgilling       Initial Creation.
11
#
12
#
13
14
try:
15
    import http.client as httpcl
16
except ImportError:
17
    import httplib as httpcl
18
from dynamicserialize import DynamicSerializationManager
19
20
21 View Code Duplication
class ThriftClient:
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
22
23
    # How to call this constructor:
24
    #   1. Pass in all arguments separately (e.g.,
25
    #      ThriftClient.ThriftClient("localhost", 9581, "/services"))
26
    #      will return a Thrift client pointed at http://localhost:9581/services.
27
    #   2. Pass in all arguments through the host string (e.g.,
28
    #      ThriftClient.ThriftClient("localhost:9581/services"))
29
    #      will return a Thrift client pointed at http://localhost:9581/services.
30
    #   3. Pass in host/port arguments through the host string (e.g.,
31
    #      ThriftClient.ThriftClient("localhost:9581", "/services"))
32
    #      will return a Thrift client pointed at http://localhost:9581/services.
33
    def __init__(self, host, port=9581, uri="/services"):
34
        hostParts = host.split("/", 1)
35
        if len(hostParts) > 1:
36
            hostString = hostParts[0]
37
            self.__uri = "/" + hostParts[1]
38
            self.__httpConn = httpcl.HTTPConnection(hostString)
39
        else:
40
            if port is None:
41
                self.__httpConn = httpcl.HTTPConnection(host)
42
            else:
43
                self.__httpConn = httpcl.HTTPConnection(host, port)
44
45
            self.__uri = uri
46
47
        self.__dsm = DynamicSerializationManager.DynamicSerializationManager()
48
49
    def sendRequest(self, request, uri="/thrift"):
50
        message = self.__dsm.serializeObject(request)
51
52
        self.__httpConn.connect()
53
        self.__httpConn.request("POST", self.__uri + uri, message)
54
55
        response = self.__httpConn.getresponse()
56
        if response.status != 200:
57
            raise ThriftRequestException("Unable to post request to server")
58
59
        rval = self.__dsm.deserializeBytes(response.read())
60
        self.__httpConn.close()
61
62
        # let's verify we have an instance of ServerErrorResponse
63
        # IF we do, through an exception up to the caller along
64
        # with the original Java stack trace
65
        # ELSE: we have a valid response and pass it back
66
        try:
67
            forceError = rval.getException()
68
            raise ThriftRequestException(forceError)
69
        except AttributeError:
70
            pass
71
72
        return rval
73
74
75
class ThriftRequestException(Exception):
76
    def __init__(self, value):
77
        self.parameter = value
78
79
    def __str__(self):
80
        return repr(self.parameter)
81