source: TI12-security/trunk/python/ndg.security.common/ndg/security/common/soap/client.py @ 5526

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/ndg.security.common/ndg/security/common/soap/client.py@5526
Revision 5526, 5.6 KB checked in by pjkersha, 11 years ago (diff)

More work on Urllib2SOAPClient class + unit test

Line 
1"""SOAP client module for NDG Security - initially for use with SAML SOAP
2binding based Attribute Authority interface
3
4NERC DataGrid Project
5"""
6__author__ = "P J Kershaw"
7__date__ = "27/07/09"
8__copyright__ = ""
9__license__ = "BSD - see LICENSE file in top-level directory"
10__contact__ = "Philip.Kershaw@stfc.ac.uk"
11__revision__ = '$Id:$'
12
13from ndg.security.common.soap import SOAPEnvelopeBase
14
15class SOAPClientBase(object):
16    """Handle client request to a SOAP Service"""
17    def __init__(self):
18        self.__responseEnvelopeClass = None
19
20    def _getResponseEnvelopeClass(self):
21        return self.__responseEnvelopeClass
22
23    def _setResponseEnvelopeClass(self, value):
24        if not issubclass(value, SOAPEnvelopeBase):
25            raise TypeError("Setting SOAP class: expecting %r, got %r" %
26                            (SOAPEnvelopeBase, type(value)))
27        self.__responseEnvelopeClass = value
28
29    responseEnvelopeClass = property(fget=_getResponseEnvelopeClass, 
30                                     fset=_setResponseEnvelopeClass, 
31                                     doc="Set the SOAP class for handling "
32                                         "requests and responses")
33     
34    def makeRequest(self, soapRequest):
35        raise NotImplementedError()
36
37
38class _SoapIOBase(object):
39    def __init__(self):
40        self.__envelope = None
41
42    def _getEnvelope(self):
43        return self.__envelope
44
45    def _setEnvelope(self, value):
46        if not isinstance(value, SOAPEnvelopeBase):
47            raise TypeError('Setting SOAP envelope object: expecting %r; got '
48                            '%r' % (SOAPEnvelopeBase, type(value)))
49                           
50        self.__envelope = value
51
52    envelope = property(fget=_getEnvelope, 
53                        fset=_setEnvelope, 
54                        doc="SOAP Envelope object used in request/response")
55
56       
57class SOAPRequestBase(object):
58    """Interface for SOAP requests"""
59    def __init__(self):
60        self.__url = None
61        self.__envelope = None
62
63    url = property(fget=_getUrl, fset=_setUrl, doc="URL of SOAP endpoint")
64
65    def _getUrl(self):
66        return self.__url
67
68    def _setUrl(self, value):
69        if not isinstance(value, basestring):
70            raise TypeError('Setting request URL: expecting %r; got '
71                            '%r' % (basestring, type(value)))
72        self.__url = value
73
74   
75class SOAPResponseBase(_SoapIOBase):
76    """Interface for SOAP responses"""
77
78
79import urllib2
80
81class UrlLib2SOAPRequest(SOAPRequestBase): 
82    """Interface for UrlLib2 based SOAP Requests"""
83   
84   
85class UrlLib2SOAPResponse(SOAPResponseBase):
86    """Interface for UrlLib2 based SOAP Responses"""
87    def __init__(self):
88        self.__fileobject
89
90    fileobject = property(fget=_getFileobject,
91                          doc="urllib2 file object returned from request")
92
93    def getFileobject(self):
94        return self.__fileobject
95
96   
97class UrlLib2SOAPClient(SOAPClientBase):
98    """urllib2 based SOAP Client"""
99   
100    def __init__(self):
101        super(UrlLib2SOAPClient, self).__init__()
102        self.__openerDirector = urllib2.OpenerDirector()
103        self.__timeout = None
104
105    def _getTimeout(self):
106        return self.__timeout
107
108    def _setTimeout(self, value):
109        if not isinstance(value, (int, float)):
110            raise TypeError("Setting request timeout: got %r, expecting int "
111                            "float type" % type(value))
112        self.__timeout = value
113
114    timeout = property(fget=_getTimeout, 
115                       fset=_setTimeout, 
116                       doc="Timeout (seconds) for requests")
117
118    def _getOpenerDirector(self):
119        return self.__openerDirector
120
121    def _setOpenerDirector(self, value):
122        """This shouldn't need to be used much in practice because __init__
123        creates one"""
124        if not isinstance(value, urllib2.OpenerDirector):
125            raise TypeError("Setting opener: expecting %r; got %r" % 
126                            (urllib2.OpenerDirector, value.__class__))
127        self.__openerDirector = value
128
129    openerDirector = property(fget=_getOpenerDirector, 
130                              fset=_setOpenerDirector, 
131                              doc="urllib2.OpenerDirector defines the "
132                                  "opener(s) for handling requests")
133   
134    def makeRequest(self, soapRequest):
135        """Make a request to the given URL with a SOAP Request object"""
136       
137        if not isinstance(soapRequest, UrlLib2SOAPRequest):
138            raise TypeError('UrlLib2SOAPClient.makeRequest: expecting %r '
139                            'derived type for SOAP request, got %r' % 
140                            (self.responseEnvelopeClass,soapRequest.__class__))
141           
142        if not isinstance(soapRequest.envelope, self.responseEnvelopeClass):
143            raise TypeError('UrlLib2SOAPClient.makeRequest: expecting %r '
144                            'derived type for SOAP envelope, got %r' % 
145                            (self.responseEnvelopeClass,soapRequest.__class__))
146                           
147        if self.timeout is not None:
148            arg = (self.timeout,)
149        else:
150            arg = ()
151           
152        soapRequestStr = soapRequest.envelope.serialize()
153        soapResponse = UrlLib2SOAPResponse()
154        soapResponse.fileObject = self.openerDirector.open(soapRequest.url, 
155                                                           soapRequestStr, 
156                                                           *arg)
157        soapResponse.envelope = self.responseEnvelopeClass() 
158        soapResponse.envelope.parse(soapResponse.fileObject)
159       
160        return soapResponse
161
162       
163   
164   
Note: See TracBrowser for help on using the repository browser.