Exemple #1
0
    def parse(self, file=None, string=None):
        """
        SAX parse XML text.

        @param file: Parse a python I{file-like} object.
        @type file: I{file-like} object
        @param string: Parse string XML.
        @type string: str
        @return: Parsed XML document.
        @rtype: L{Document}

        """
        if file is None and string is None:
            return
        timer = metrics.Timer()
        timer.start()
        source = file
        if file is None:
            source = InputSource(None)
            source.setByteStream(suds.BytesIO(string))
        sax, handler = self.saxparser()
        sax.parse(source)
        timer.stop()
        if file is None:
            metrics.log.debug("%s\nsax duration: %s", string, timer)
        else:
            metrics.log.debug("sax (%s) duration: %s", file, timer)
        return handler.nodes[0]
Exemple #2
0
    def create(self, name):
        """
        Create a WSDL type by name.

        @param name: The name of a type defined in the WSDL.
        @type name: str
        @return: The requested object.
        @rtype: L{Object}

        """
        timer = metrics.Timer()
        timer.start()
        type = self.resolver.find(name)
        if type is None:
            raise TypeNotFound(name)
        if type.enum():
            result = sudsobject.Factory.object(name)
            for e, a in type.children():
                setattr(result, e.name, e.name)
        else:
            try:
                result = self.builder.build(type)
            except Exception as e:
                log.error("create '%s' failed", name, exc_info=True)
                raise BuildError(name, e)
        timer.stop()
        metrics.log.debug("%s created: %s", name, timer)
        return result
Exemple #3
0
 def invoke(self, args, kwargs):
     """
     Send the required soap message to invoke the specified method
     @param args: A list of args for the method invoked.
     @type args: list
     @param kwargs: Named (keyword) args for the method invoked.
     @type kwargs: dict
     @return: The result of the method invocation.
     @rtype: I{builtin}|I{subclass of} L{Object}
     """
     timer = metrics.Timer()
     timer.start()
     result = None
     binding = self.method.binding.input
     soapenv = binding.get_message(self.method, args, kwargs)
     timer.stop()
     metrics.log.debug("message for '%s' created: %s",
                       self.method.name,
                       timer)
     timer.start()
     result = self.send(soapenv)
     timer.stop()
     metrics.log.debug("method '%s' invoked: %s",
                       self.method.name,
                       timer)
     return result
Exemple #4
0
 def parse(self, file=None, string=None):
     """
     SAX parse XML text.
     @param file: Parse a python I{file-like} object.
     @type file: I{file-like} object.
     @param string: Parse string XML.
     @type string: str
     """
     timer = metrics.Timer()
     timer.start()
     sax, handler = self.saxparser()
     if file is not None:
         sax.parse(file)
         timer.stop()
         metrics.log.debug('sax (%s) duration: %s', file, timer)
         return handler.nodes[0]
     if string is not None:
         if isinstance(string, six.text_type):
             string = string.encode("utf-8")
         source = InputSource(None)
         source.setByteStream(BytesIO(string))
         sax.parse(source)
         timer.stop()
         metrics.log.debug('%s\nsax duration: %s', string, timer)
         return handler.nodes[0]
Exemple #5
0
 def parse(self, file=None, string=None):
     """
     SAX parse XML text.
     @param file: Parse a python I{file-like} object.
     @type file: I{file-like} object.
     @param string: Parse string XML.
     @type string: str
     """
     timer = metrics.Timer()
     timer.start()
     sax, handler = self.saxparser()
     if file is not None:
         sax.parse(file)
         timer.stop()
         metrics.log.debug('sax (%s) duration: %s', file, timer)
         return handler.nodes[0]
     if string is not None:
         source = InputSource(None)
         try:
             source.setByteStream(StringIO(string.encode('utf8')))
         except UnicodeDecodeError:
             source.setByteStream(StringIO(string))
         sax.parse(source)
         timer.stop()
         metrics.log.debug('%s\nsax duration: %s', string, timer)
         return handler.nodes[0]
Exemple #6
0
 def send(self, soapenv):
     """
     Send soap message.
     @param soapenv: A soap envelope to send.
     @type soapenv: L{Document}
     @return: The reply to the sent message.
     @rtype: I{builtin} or I{subclass of} L{Object}
     """
     result = None
     location = self.location()
     binding = self.method.binding.input
     transport = self.options.transport
     retxml = self.options.retxml
     nosend = self.options.nosend
     prettyxml = self.options.prettyxml
     timer = metrics.Timer()
     log.debug('sending to (%s)\nmessage:\n%s', location, soapenv)
     try:
         self.last_sent(soapenv)
         plugins = PluginContainer(self.options.plugins)
         plugins.message.marshalled(envelope=soapenv.root())
         if prettyxml:
             soapenv = soapenv.str()
         else:
             soapenv = soapenv.plain()
         soapenv = soapenv.encode('utf-8')
         ctx = plugins.message.sending(envelope=soapenv)
         soapenv = ctx.envelope
         if nosend:
             return RequestContext(self, binding, soapenv)
         request = Request(location, soapenv)
         request.headers = self.headers()
         timer.start()
         reply = transport.send(request)
         timer.stop()
         metrics.log.debug('waited %s on server reply', timer)
         ctx = plugins.message.received(reply=reply.message)
         reply.message = ctx.reply
         if retxml:
             result = reply.message
         else:
             result = self.succeeded(binding, reply.message)
     except TransportError as e:
         if e.httpcode in (202, 204):
             result = None
         else:
             log.error(self.last_sent())
             result = self.failed(binding, e)
     return result
Exemple #7
0
    def send(self, soapenv):
        """
        Send SOAP message.

        Depending on how the ``nosend`` & ``retxml`` options are set, may do
        one of the following:
          * Return a constructed web service operation request without sending
            it to the web service.
          * Invoke the web service operation and return its SOAP reply XML.
          * Invoke the web service operation, process its results and return
            the Python object representing the returned value.

        @param soapenv: A SOAP envelope to send.
        @type soapenv: L{Document}
        @return: SOAP request, SOAP reply or a web service return value.
        @rtype: L{RequestContext}|I{builtin}|I{subclass of} L{Object}|I{bytes}|
            I{None}

        """
        location = self.__location()
        log.debug("sending to (%s)\nmessage:\n%s", location, soapenv)
        plugins = PluginContainer(self.options.plugins)
        plugins.message.marshalled(envelope=soapenv.root())
        if self.options.prettyxml:
            soapenv = soapenv.str()
        else:
            soapenv = soapenv.plain()
        soapenv = soapenv.encode("utf-8")
        ctx = plugins.message.sending(envelope=soapenv)
        soapenv = ctx.envelope
        if self.options.nosend:
            return RequestContext(self.process_reply, soapenv)
        request = suds.transport.Request(location, soapenv)
        request.headers = self.__headers()
        try:
            timer = metrics.Timer()
            timer.start()
            reply = self.options.transport.send(request)
            timer.stop()
            metrics.log.debug("waited %s on server reply", timer)
        except suds.transport.TransportError as e:
            content = e.fp and e.fp.read() or ""
            return self.process_reply(content, e.httpcode, tostr(e))
        return self.process_reply(reply.message, None, None)
 def addports(self):
     """
     Look through the list of service ports and construct a list of tuples
     where each tuple is used to describe a port and its list of methods as:
     (port, [method]).  Each method is a tuple: (name, [pdef,..]) where each
     pdef is a tuple: (param-name, type).
     """
     timer = metrics.Timer()
     timer.start()
     for port in self.service.ports:
         p = self.findport(port)
         for op in port.binding.operations.values():
             m = p[0].method(op.name)
             binding = m.binding.input
             method = (m.name, binding.param_defs(m))
             p[1].append(method)
             metrics.log.debug("method '%s' created: %s", m.name, timer)
         p[1].sort()
     timer.stop()
Exemple #9
0
    def invoke(self, args, kwargs):
        """
        Invoke a specified web service method.

        Depending on how the ``nosend`` & ``retxml`` options are set, may do
        one of the following:
          * Return a constructed web service operation SOAP request without
            sending it to the web service.
          * Invoke the web service operation and return its SOAP reply XML.
          * Invoke the web service operation, process its results and return
            the Python object representing the returned value.

        When returning a SOAP request, the request is wrapped inside a
        RequestContext object allowing the user to acquire a corresponding SOAP
        reply himself and then pass it back to suds for further processing.

        Constructed request data is automatically processed using registered
        plugins and serialized into a byte-string. Exact request XML formatting
        may be affected by the ``prettyxml`` suds option.

        @param args: A list of args for the method invoked.
        @type args: list|tuple
        @param kwargs: Named (keyword) args for the method invoked.
        @type kwargs: dict
        @return: SOAP request, SOAP reply or a web service return value.
        @rtype: L{RequestContext}|I{builtin}|I{subclass of} L{Object}|I{bytes}|
            I{None}

        """
        timer = metrics.Timer()
        timer.start()
        binding = self.method.binding.input
        soapenv = binding.get_message(self.method, args, kwargs)
        timer.stop()
        method_name = self.method.name
        metrics.log.debug("message for '%s' created: %s", method_name, timer)
        timer.start()
        result = self.send(soapenv)
        timer.stop()
        metrics.log.debug("method '%s' invoked: %s", method_name, timer)
        return result
Exemple #10
0
 def parse(self, file=None, url=None, string=None):
     """ parse a document """
     handler = Handler()
     timer = metrics.Timer()
     timer.start()
     if file is not None:
         parse(file, handler)
         timer.stop()
         metrics.log.debug('sax (%s) duration: %s', file, timer)
         return handler.nodes[0]
     if url is not None:
         fp = self.transport.open(Request(url))
         parse(fp, handler)
         timer.stop()
         metrics.log.debug('sax (%s) duration: %s', url, timer)
         return handler.nodes[0]
     if string is not None:
         parseString(string, handler)
         timer.stop()
         metrics.log.debug('%s\nsax duration: %s', string, timer)
         return handler.nodes[0]
Exemple #11
0
 def send(self, soapenv):
     """
     Send soap message.
     @param soapenv: A soap envelope to send.
     @type soapenv: L{Document}
     @return: The reply to the sent message.
     @rtype: I{builtin} or I{subclass of} L{Object}
     """
     location = self.location()
     log.debug('sending to (%s)\nmessage:\n%s', location, soapenv)
     original_soapenv = soapenv
     plugins = PluginContainer(self.options.plugins)
     plugins.message.marshalled(envelope=soapenv.root())
     if self.options.prettyxml:
         soapenv = soapenv.str()
     else:
         soapenv = soapenv.plain()
     soapenv = soapenv.encode('utf-8')
     ctx = plugins.message.sending(envelope=soapenv)
     soapenv = ctx.envelope
     if self.options.nosend:
         return RequestContext(self, soapenv, original_soapenv)
     request = Request(location, soapenv)
     request.headers = self.headers()
     try:
         timer = metrics.Timer()
         timer.start()
         reply = self.options.transport.send(request)
         timer.stop()
         metrics.log.debug('waited %s on server reply', timer)
     except TransportError as e:
         content = e.fp and e.fp.read() or ''
         return self.process_reply(reply=content,
                                   status=e.httpcode,
                                   description=tostr(e),
                                   original_soapenv=original_soapenv)
     return self.process_reply(reply=reply.message,
                               original_soapenv=original_soapenv)
Exemple #12
0
 def parse(self, file=None, string=None):
     """
     SAX parse XML text.
     @param file: Parse a python I{file-like} object.
     @type file: I{file-like} object.
     @param string: Parse string XML.
     @type string: str
     """
     timer = metrics.Timer()
     timer.start()
     sax, handler = self.saxparser()
     if file is not None:
         sax.parse(file)
         timer.stop()
         metrics.log.debug('sax (%s) duration: %s', file, timer)
         return handler.nodes[0]
     if string is not None:
         if isinstance(string, str):
             string = string.encode()
         parseString(string, handler)
         timer.stop()
         metrics.log.debug('%s\nsax duration: %s', string, timer)
         return handler.nodes[0]
Exemple #13
0
try:
    url = "http://arcweb.esri.com/services/v2/RouteFinder.wsdl"
    start(url)
    client = Client(url)
    print client
except WebFault, f:
    errors += 1
    print f
    print f.fault
except Exception, e:
    errors += 1
    print e
    tb.print_exc()

timer = metrics.Timer()

try:
    url = "https://www.e-conomic.com/secure/api1/EconomicWebService.asmx?WSDL"
    start(url)
    timer.start()
    client = Client(url)
    #client.setport(0)
    timer.stop()
    print 'create client: %s' % timer
    timer.start()
    s = str(client)
    timer.stop()
    print 'str(client): %s' % timer
    print 'client:\n%s' % s
except WebFault, f: