예제 #1
0
     def __call__(self,*args,**kwargs):
         # copied & simplified from soaplib.client.SimpleSoapClient
        if len(args) != len(self.descriptor.inMessage.params):
            argstring = '\r\n'.join([ '    ' + str(arg) for arg in args ])
            paramstring = '\r\n'.join([ '    ' + str(p[0]) for p in self.descriptor.inMessage.params ])
            err_msg = _err_format % (argstring, paramstring)
            raise Exception(err_msg)

        msg = self.descriptor.inMessage.to_xml(*args)

        # grab the soap headers passed into this call
        headers = kwargs.get('headers', [])
        msgid = kwargs.get('msgid')
        if msgid:
            # special case for the msgid field as a convenience
            # when dealing with async callback methods
            headers.append(create_relates_to_header(msgid))

        envelope = make_soap_envelope(msg, header_elements=headers)

        body = ElementTree.tostring(envelope)
        # removed http header logic - not needed for django test client

        # use django.test.client to send request
        client = Client()
        response = client.post(self.path, body, content_type="text/xml")
        data = response.content        
        
        if str(response.status_code) not in ('200', '202'):
            # consider everything NOT 200 or 202 as an error response
            
            if str(response.status_code) == '500': 
                fault = None
                try:
                    payload, headers = from_soap(data)
                    fault = Fault.from_xml(payload)
                except:
                    trace = StringIO()
                    import traceback
                    traceback.print_exc(file=trace)
                    
                    fault = Exception('Unable to read response \n  %s %s \n %s \n %s' % \
                            (response.status, response.reason, trace.getvalue(), data))
                raise fault
            else:
                raise Exception('%s %s' % (response.status,response.reason,))

        if not self.descriptor.outMessage.params:            
            return

        payload, headers = from_soap(data)        
        
        results = self.descriptor.outMessage.from_xml(payload)        
        return results[0]
예제 #2
0
파일: client.py 프로젝트: edevil/soaplib
    def __call__(self, *args, **kwargs):
        '''
        This method executes the http request to the remote web service.  With
        the exception of 'headers', 'msgid', and 'mtom'; all keyword arguments
        to this method are put in the http header.  The 'headers' keyword is to
        denote a list of elements to be included in the soap header, 'msgid'
        is a convenience keyword used in async web services which creates a
        WS-Addressing messageid header to be included in the soap headers, and
        'mtom' enables the Message Transmission Optimization Mechanism.

        @param the arguments to the remote method
        @param the keyword arguments
        '''
        if len(args) != len(self.descriptor.inMessage.params):
            argstring = '\r\n'.join(['    ' + str(arg) for arg in args])
            paramstring = '\r\n'.join(['    ' + str(p[0])
                for p in self.descriptor.inMessage.params])
            err_msg = _err_format % (argstring, paramstring)
            raise Exception(err_msg)

        msg = self.descriptor.inMessage.to_xml(*args)

        # grab the soap headers passed into this call
        headers = kwargs.get('headers', [])
        mtom = kwargs.get('mtom', False)
        msgid = kwargs.get('msgid')
        if msgid:
            # special case for the msgid field as a convenience
            # when dealing with async callback methods
            headers.append(create_relates_to_header(msgid))

        tns = self.descriptor.inMessage.ns
        envelope = make_soap_envelope(msg, tns, header_elements=headers)

        body = ElementTree.tostring(envelope)
        methodName = '\"%s\"' % self.descriptor.soapAction
        httpHeaders = {'Content-Length': len(body),
                      'Content-type': 'text/xml; charset="UTF-8"',
                      'Accept': ('application/soap+xml, application/dime, '
                                'multipart/related, text/*'),
                      'User-Agent': 'Soaplib/1.0',
                      'SOAPAction': methodName,
                      }

        for k, v in kwargs.items():
            # add all the other keywords to the http headers
            if k not in ('headers', 'msgid', 'mtom'):
                httpHeaders[k] = v

        if mtom:
            httpHeaders, body = apply_mtom(httpHeaders, body,
                self.descriptor.inMessage.params, args)

        dump(self.host, self.path, httpHeaders, body)

        if self.scheme == "http":
            conn = httplib.HTTPConnection(self.host)
        elif self.scheme == "https":
            conn = httplib.HTTPSConnection(self.host)
        else:
            raise RuntimeError("Unsupported URI connection scheme: %s" %
                self.scheme)

        conn.request("POST", self.path, body=body, headers=httpHeaders)
        response = conn.getresponse()
        data = response.read()

        dump(self.host, self.path, dict(response.getheaders()), data)

        contenttype = response.getheader('Content-Type')
        data = collapse_swa(contenttype, data)

        conn.close()
        if str(response.status) not in['200', '202']:
            # consider everything NOT 200 or 202 as an error response

            if str(response.status) == '500':
                fault = None
                try:
                    payload, headers = from_soap(data)
                    fault = Fault.from_xml(payload)
                except:
                    trace = StringIO()
                    import traceback
                    traceback.print_exc(file=trace)

                    fault = Exception("Unable to read response \n"
                        "%s %s \n %s \n %s" %
                        (response.status, response.reason, trace.getvalue(),
                         data))
                raise fault
            else:
                raise Exception("%s %s" % (response.status, response.reason))

        if not self.descriptor.outMessage.params:
            return

        payload, headers = from_soap(data)
        results = self.descriptor.outMessage.from_xml(payload)
        return results[0]
예제 #3
0
    def __call__(self, *args, **kwargs):
        '''
        This method executes the http request to the remote web service.  With
        the exception of 'headers', 'msgid', and 'mtom'; all keyword arguments
        to this method are put in the http header.  The 'headers' keyword is to
        denote a list of elements to be included in the soap header, 'msgid'
        is a convenience keyword used in async web services which creates a
        WS-Addressing messageid header to be included in the soap headers, and
        'mtom' enables the Message Transmission Optimization Mechanism.

        @param the arguments to the remote method
        @param the keyword arguments
        '''
        if len(args) != len(self.descriptor.inMessage.params):
            argstring = '\r\n'.join(['    ' + str(arg) for arg in args])
            paramstring = '\r\n'.join(
                ['    ' + str(p[0]) for p in self.descriptor.inMessage.params])
            err_msg = _err_format % (argstring, paramstring)
            raise Exception(err_msg)

        msg = self.descriptor.inMessage.to_xml(*args)

        # grab the soap headers passed into this call
        headers = kwargs.get('headers', [])
        mtom = kwargs.get('mtom', False)
        msgid = kwargs.get('msgid')
        if msgid:
            # special case for the msgid field as a convenience
            # when dealing with async callback methods
            headers.append(create_relates_to_header(msgid))

        tns = self.descriptor.inMessage.ns
        envelope = make_soap_envelope(msg, tns, header_elements=headers)

        body = ElementTree.tostring(envelope)
        methodName = '\"%s\"' % self.descriptor.soapAction
        httpHeaders = {
            'Content-Length':
            len(body),
            'Content-type':
            'text/xml; charset="UTF-8"',
            'Accept': ('application/soap+xml, application/dime, '
                       'multipart/related, text/*'),
            'User-Agent':
            'Soaplib/1.0',
            'SOAPAction':
            methodName,
        }

        for k, v in kwargs.items():
            # add all the other keywords to the http headers
            if k not in ('headers', 'msgid', 'mtom'):
                httpHeaders[k] = v

        if mtom:
            httpHeaders, body = apply_mtom(httpHeaders, body,
                                           self.descriptor.inMessage.params,
                                           args)

        dump(self.host, self.path, httpHeaders, body)

        if self.scheme == "http":
            conn = httplib.HTTPConnection(self.host)
        elif self.scheme == "https":
            conn = httplib.HTTPSConnection(self.host)
        else:
            raise RuntimeError("Unsupported URI connection scheme: %s" %
                               self.scheme)

        conn.request("POST", self.path, body=body, headers=httpHeaders)
        response = conn.getresponse()
        data = response.read()

        dump(self.host, self.path, dict(response.getheaders()), data)

        contenttype = response.getheader('Content-Type')
        data = collapse_swa(contenttype, data)

        conn.close()
        if str(response.status) not in ['200', '202']:
            # consider everything NOT 200 or 202 as an error response

            if str(response.status) == '500':
                fault = None
                try:
                    payload, headers = from_soap(data)
                    fault = Fault.from_xml(payload)
                except:
                    trace = StringIO()
                    import traceback
                    traceback.print_exc(file=trace)

                    fault = Exception("Unable to read response \n"
                                      "%s %s \n %s \n %s" %
                                      (response.status, response.reason,
                                       trace.getvalue(), data))
                raise fault
            else:
                raise Exception("%s %s" % (response.status, response.reason))

        if not self.descriptor.outMessage.params:
            return

        payload, headers = from_soap(data)
        results = self.descriptor.outMessage.from_xml(payload)
        #TODO: consider supporting multiple return types in a better manner
        if len(results) > 1:
            return results
        return results[0]