def __xmlstr__(self, tag=None, node=None, nsmap=None): ''' Convert a DynamicObject a string containing the XML representation. See __xml__ for an explaination of the arguments. ''' return ET.tostring(self.__xml__(tag, node, nsmap), pretty_print=True)
def invoke(self, operation, *args, **kwargs): ''' Invoke a SOAP operation. ''' self._reqno += 1 retxml = kwargs.pop('__retxml__', self.retxml) timeout = kwargs.pop('__timeout__', self.timeout) transport_options = kwargs.pop('__transport__', {}) # Create an instance of the request message and initialize # the object from the arguments param = self.factory(operation.imsg) tmpl = param.__template__ for k,v in zip((t[0] for t in tmpl), args): param[k] = v for k,v in kwargs.items(): param[k] = v # Build the soap envelope and set the http headers payload = self.envelope(self.headers, param, operation.imsg) httphdr = { 'Content-Type': 'text/xml', 'SOAPAction': operation.action } httphdr.update(self.httphdr) # Construct and issue the request, read the response payload = ET.tostring(payload, pretty_print=True) log.debug('=== SOAP REQUEST ===\n%s', re.sub(r'password>.*?<', r'password>*****<', payload )) req = urllib2.Request(self.url, payload, httphdr) try: if self._inject: xml = ET.fromstring(self._inject.next()) else: if hasattr(self.transport, 'open'): rsp = self.transport.open(req, timeout=timeout, **transport_options) else: rsp = self.transport.urlopen(req, timeout=timeout, **transport_options) xml = ET.parse(rsp) except urllib2.HTTPError as ex: xml = ET.parse(ex) log.debug('=== SOAP RESPONSE ===\n%s', xmlstr(xml)) # Get the soap body retval = xml.find(ns.expand('soapenv:Body', self.nsmap)) if not retxml: # Does the body contain any nodes? if len(retval): # Get the first child and examine it retval = retval[0] namespace, tag = ns.split(retval.tag) # If it's a fault, convert it to an exception if tag == 'Fault': raise SoapFault(retval, self) # Otherwise, deserialize obj = self.factory(operation.omsg, retval) # If the deserialized # object has only one item, return that item, otherwise the # whole object # # This is so if the return value is a single primitive type # (like a string), you don't have to dig into an object just # to get at the single primitive return value if len(obj) == 1: obj = obj[0] retval = obj else: retval = None return retval
def invoke(self, operation, *args, **kwargs): ''' Invoke a SOAP operation. ''' self._reqno += 1 retxml = kwargs.pop('__retxml__', self.retxml) timeout = kwargs.pop('__timeout__', self.timeout) transport_options = kwargs.pop('__transport__', {}) # Create an instance of the request message and initialize # the object from the arguments param = self.factory(operation.imsg) tmpl = param.__template__ for k,v in zip((t[0] for t in tmpl), args): param[k] = v for k,v in kwargs.items(): param[k] = v # Build the soap envelope and set the http headers payload = self.envelope(self.headers, param, operation.imsg) httphdr = { 'Content-Type': 'text/xml', 'SOAPAction': operation.action } httphdr.update(self.httphdr) # Construct and issue the request, read the response payload = ET.tostring(payload, pretty_print=True) log.debug('=== SOAP REQUEST ===\n%s', payload) req = urllib2.Request(self.url, payload, httphdr) try: if self._inject: xml = ET.fromstring(self._inject.next()) else: if hasattr(self.transport, 'open'): rsp = self.transport.open(req, timeout=timeout, **transport_options) else: rsp = self.transport.urlopen(req, timeout=timeout, **transport_options) xml = ET.parse(rsp) except urllib2.HTTPError as ex: xml = ET.parse(ex) log.debug('=== SOAP RESPONSE ===\n%s', xmlstr(xml)) # Get the soap body retval = xml.find(ns.expand('soapenv:Body', self.nsmap)) if not retxml: # Does the body contain any nodes? if len(retval): # Get the first child and examine it retval = retval[0] namespace, tag = ns.split(retval.tag) # If it's a fault, convert it to an exception if tag == 'Fault': raise SoapFault(retval, self) # Otherwise, deserialize obj = self.factory(operation.omsg, retval) # If the deserialized # object has only one item, return that item, otherwise the # whole object # # This is so if the return value is a single primitive type # (like a string), you don't have to dig into an object just # to get at the single primitive return value if len(obj) == 1: obj = obj[0] retval = obj else: retval = None return retval