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

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

Added urllib2 based SOAP client handler class.

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.__soapClass = None
19
20    def _getSoapClass(self):
21        return self.__soapClass
22
23    def _setSoapClass(self, value):
24        if not isinstance(value, SOAPEnvelopeBase):
25            raise TypeError("Setting SOAP class: expecting %r, got %r" %
26                            (SOAPEnvelopeBase, value.__class__))
27        self.__soapClass = value
28
29    soapClass = property(fget=_getSoapClass, 
30                         fset=_setSoapClass, 
31                         doc="Set the SOAP class for handling requests and "
32                             " responses")
33     
34    def makeRequest(self, url, soapRequest):
35        raise NotImplementedError()
36
37
38import urllib2
39from cStringIO import StringIO
40
41class UrlLib2SOAPClient(SOAPClientBase):
42    """urllib2 based SOAP Client"""
43   
44    def __init__(self, soapClass=None):
45        self.__openerDirector = urllib2.OpenerDirector()
46        self.__timeout = None
47       
48        if soapClass is not None:
49            self.soapClass = soapClass
50
51    def _getTimeout(self):
52        return self.__timeout
53
54    def _setTimeout(self, value):
55        if not isinstance(value, (int, float)):
56            raise TypeError("Setting request timeout: got %r, expecting int "
57                            "float type" % type(value))
58        self.__timeout = value
59
60    timeout = property(fget=_getTimeout, 
61                       fset=_setTimeout, 
62                       doc="Timeout (seconds) for requests")
63
64    def _getOpenerDirector(self):
65        return self.__openerDirector
66
67    def _setOpenerDirector(self, value):
68        """This shouldn't need to be used much in practice because __init__
69        creates one"""
70        if not isinstance(value, urllib2.OpenerDirector):
71            raise TypeError("Setting opener: expecting %r; got %r" % 
72                            (urllib2.OpenerDirector, value.__class__))
73        self.__openerDirector = value
74
75    openerDirector = property(fget=_getOpenerDirector, 
76                              fset=_setOpenerDirector, 
77                              doc="urllib2.OpenerDirector defines the "
78                                  "opener(s) for handling requests")
79   
80    def makeRequest(self, url, soapRequest):
81        """Make a request to the given URL with a SOAP Request object"""
82       
83        if not isinstance(soapRequest, self.soapClass):
84            raise TypeError('Request to "%s", expecting %r derived type for '
85                            'SOAP request, got %r' % (url, self.soapClass,
86                                                      soapRequest.__class__))
87                           
88        if self.timeout is not None:
89            arg = (self.timeout,)
90        else:
91            arg = ()
92           
93        result = self.__openerDirector.open(url, soapRequest, *arg)
94       
95        resultStream = StringIO()
96        resultStream.write(result)
97        resultStream.seek(0)
98       
99        soapResponse = self.soapClass()       
100        soapResponse.parse(resultStream)
101       
102        return soapResponse
103
104       
105   
106   
Note: See TracBrowser for help on using the repository browser.