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

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

Working soap unit test with UrlLib2SOAPClient.

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 send(self, soapRequest):
35        raise NotImplementedError()
36
37
38class _SoapIOBase(object):
39    """Base class for request and response classes"""
40   
41    def __init__(self):
42        self.__envelope = None
43
44    def _getEnvelope(self):
45        return self.__envelope
46
47    def _setEnvelope(self, value):
48        if not isinstance(value, SOAPEnvelopeBase):
49            raise TypeError('Setting SOAP envelope object: expecting %r; got '
50                            '%r' % (SOAPEnvelopeBase, type(value)))
51                           
52        self.__envelope = value
53
54    envelope = property(fget=_getEnvelope, 
55                        fset=_setEnvelope, 
56                        doc="SOAP Envelope object used in request/response")
57
58       
59class SOAPRequestBase(object):
60    """Interface for SOAP requests"""
61    def __init__(self):
62        self.__url = None
63        self.__envelope = None
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    url = property(fget=_getUrl, fset=_setUrl, doc="URL of SOAP endpoint")
75
76   
77class SOAPResponseBase(_SoapIOBase):
78    """Interface for SOAP responses"""
79
80
81import urllib2
82
83class UrlLib2SOAPRequest(SOAPRequestBase): 
84    """Interface for UrlLib2 based SOAP Requests"""
85   
86   
87class UrlLib2SOAPResponse(SOAPResponseBase):
88    """Interface for UrlLib2 based SOAP Responses"""
89    def __init__(self):
90        self.__fileobject = None
91
92    def _getFileobject(self):
93        return self.__fileobject
94
95    fileobject = property(fget=_getFileobject,
96                          doc="urllib2 file object returned from request")
97
98   
99class UrlLib2SOAPClient(SOAPClientBase):
100    """urllib2 based SOAP Client"""
101   
102    def __init__(self):
103        super(UrlLib2SOAPClient, self).__init__()
104        self.__openerDirector = urllib2.OpenerDirector()
105        self.__timeout = None
106
107    def _getTimeout(self):
108        return self.__timeout
109
110    def _setTimeout(self, value):
111        if not isinstance(value, (int, float)):
112            raise TypeError("Setting request timeout: got %r, expecting int "
113                            "float type" % type(value))
114        self.__timeout = value
115
116    timeout = property(fget=_getTimeout, 
117                       fset=_setTimeout, 
118                       doc="Timeout (seconds) for requests")
119
120    def _getOpenerDirector(self):
121        return self.__openerDirector
122
123    def _setOpenerDirector(self, value):
124        """This shouldn't need to be used much in practice because __init__
125        creates one"""
126        if not isinstance(value, urllib2.OpenerDirector):
127            raise TypeError("Setting opener: expecting %r; got %r" % 
128                            (urllib2.OpenerDirector, value.__class__))
129        self.__openerDirector = value
130
131    openerDirector = property(fget=_getOpenerDirector, 
132                              fset=_setOpenerDirector, 
133                              doc="urllib2.OpenerDirector defines the "
134                                  "opener(s) for handling requests")
135   
136    def send(self, soapRequest):
137        """Make a request to the given URL with a SOAP Request object"""
138       
139        if not isinstance(soapRequest, UrlLib2SOAPRequest):
140            raise TypeError('UrlLib2SOAPClient.makeRequest: expecting %r '
141                            'derived type for SOAP request, got %r' % 
142                            (self.responseEnvelopeClass,soapRequest.__class__))
143           
144        if not isinstance(soapRequest.envelope, self.responseEnvelopeClass):
145            raise TypeError('UrlLib2SOAPClient.makeRequest: expecting %r '
146                            'derived type for SOAP envelope, got %r' % 
147                            (self.responseEnvelopeClass,soapRequest.__class__))
148                           
149        if self.timeout is not None:
150            arg = (self.timeout,)
151        else:
152            arg = ()
153           
154        soapRequestStr = soapRequest.envelope.serialize()
155        soapResponse = UrlLib2SOAPResponse()
156        soapResponse.fileObject = self.openerDirector.open(soapRequest.url, 
157                                                           soapRequestStr, 
158                                                           *arg)
159        soapResponse.envelope = self.responseEnvelopeClass() 
160        soapResponse.envelope.parse(soapResponse.fileObject)
161       
162        return soapResponse
163
164       
165   
166   
Note: See TracBrowser for help on using the repository browser.