def __GetReportXml(self, report):
        """Transforms the report object into xml.

    Args:
      report: dict ReportDefinition object to turn to xml.

    Returns:
      str ReportDefinition XML.
    """
        SanityCheck.SoappySanityCheck(self._soappyservice, report,
                                      self._namespace, u'reportDefinition')

        packed = self._message_handler.PackForSoappy(report, self._namespace,
                                                     'reportDefinition',
                                                     self._soappyservice,
                                                     False, lambda x: '')

        # Use a SOAPBuilder
        builder = SOAPpy.SOAPBuilder(kw={'reportDefinition': packed},
                                     envelope=0,
                                     noroot=1)

        # Fixes list serialization.
        builder.config.typed = False

        # Hack, need to remove top element and body wrapper.
        builder._xml_top = ''
        builder.body = 0

        # Build the XML.
        report_xml = builder.build()

        # Removes xsi:types.
        report_xml = self.__RemoveAttributes(report_xml)
        return report_xml
示例#2
0
        def CallMethod(*args):
            """Perform a SOAP call."""
            try:
                self._lock.acquire()
                self._ReadyOAuth()
                self._ReadyCompression()
                self._SetHeaders()

                args = self._TakeActionOnSoapCall(method_name, args)
                method_info = self._GetMethodInfo(method_name)
                method_attrs_holder = None

                if len(method_info[MethodInfoKeys.INPUTS]) > 1:
                    self._ConfigureArgOrder(method_name,
                                            method_info[MethodInfoKeys.INPUTS])

                if not method_info[MethodInfoKeys.INPUTS]:
                    # Don't put any namespaces other than this service's namespace on
                    # calls with no input params.
                    method_attrs_holder = self._soappyservice.soapproxy.methodattrs
                    self._soappyservice.soapproxy.methodattrs = {
                        'xmlns': self._namespace
                    }

                if len(args) != len(method_info[MethodInfoKeys.INPUTS]):
                    raise TypeError(''.join([
                        method_name + '() takes exactly ',
                        str(
                            len(self._soappyservice.methods[method_name].
                                inparams)), ' argument(s). (',
                        str(len(args)), ' given)'
                    ]))

                ksoap_args = {}
                for i in range(len(method_info[MethodInfoKeys.INPUTS])):
                    if Utils.BoolTypeConvert(self._config['strict']):
                        SanityCheck.SoappySanityCheck(
                            self._soappyservice, args[i], method_info[
                                MethodInfoKeys.INPUTS][i][MethodInfoKeys.NS],
                            method_info[MethodInfoKeys.INPUTS][i][
                                MethodInfoKeys.TYPE],
                            method_info[MethodInfoKeys.INPUTS][i][
                                MethodInfoKeys.MAX_OCCURS])

                    element_name = str(method_info[MethodInfoKeys.INPUTS][i][
                        MethodInfoKeys.ELEMENT_NAME])

                    ksoap_args[element_name] = MessageHandler.PackForSoappy(
                        args[i], method_info[MethodInfoKeys.INPUTS][i][
                            MethodInfoKeys.NS], method_info[
                                MethodInfoKeys.INPUTS][i][MethodInfoKeys.TYPE],
                        self._soappyservice, self._wrap_lists,
                        self._namespace_extractor)

                ksoap_args = self._TakeActionOnPackedArgs(
                    method_name, ksoap_args)

                buf = self._buffer_class(xml_parser=self._config['xml_parser'],
                                         pretty_xml=Utils.BoolTypeConvert(
                                             self._config['pretty_xml']))
                sys_stdout_monkey_lock.acquire()
                try:
                    old_stdout = sys.stdout
                    sys.stdout = buf

                    error = {}
                    response = None
                    start_time = time.strftime('%Y-%m-%d %H:%M:%S')
                    try:
                        response = MessageHandler.UnpackResponseAsDict(
                            soap_service_method(**ksoap_args))
                    except Exception, e:
                        error['data'] = e
                    stop_time = time.strftime('%Y-%m-%d %H:%M:%S')
                    # Restore stdout
                    sys.stdout = old_stdout
                finally:
                    sys_stdout_monkey_lock.release()

                if isinstance(response, Error):
                    error = response

                if not Utils.BoolTypeConvert(self._config['raw_debug']):
                    self._HandleLogsAndErrors(buf, start_time, stop_time,
                                              error)

                # When debugging mode is ON, fetch last traceback.
                if Utils.BoolTypeConvert(self._config['debug']):
                    if Utils.LastStackTrace(
                    ) and Utils.LastStackTrace() != 'None':
                        error['trace'] = Utils.LastStackTrace()

                # Catch local errors prior to going down to the SOAP layer, which may
                # not exist for this error instance.
                if 'data' in error and not buf.IsHandshakeComplete():
                    # Check if buffer contains non-XML data, most likely an HTML page.
                    # This happens in the case of 502 errors (and similar). Otherwise,
                    # this is a local error and API request was never made.
                    html_error = Utils.GetErrorFromHtml(buf.GetBufferAsStr())
                    if html_error:
                        msg = html_error
                    else:
                        msg = str(error['data'])
                        if Utils.BoolTypeConvert(self._config['debug']):
                            msg += '\n%s' % error['trace']

                    # When debugging mode is ON, store the raw content of the buffer.
                    if Utils.BoolTypeConvert(self._config['debug']):
                        error['raw_data'] = buf.GetBufferAsStr()

                    # Catch errors from AuthToken and ValidationError levels, raised
                    # during try/except above.
                    if isinstance(error['data'], AuthTokenError):
                        raise AuthTokenError(msg)
                    elif isinstance(error['data'], ValidationError):
                        raise ValidationError(error['data'])
                    if 'raw_data' in error:
                        msg = '%s [RAW DATA: %s]' % (msg, error['raw_data'])
                    return Error(msg)

                if Utils.BoolTypeConvert(self._config['raw_response']):
                    response = buf.GetRawSoapIn()
                elif error:
                    response = error
                else:
                    output_types = [
                        (out_param[MethodInfoKeys.NS],
                         out_param[MethodInfoKeys.TYPE],
                         out_param[MethodInfoKeys.MAX_OCCURS])
                        for out_param in method_info[MethodInfoKeys.OUTPUTS]
                    ]
                    response = MessageHandler.RestoreListTypeWithSoappy(
                        response, self._soappyservice, output_types)

                if Utils.BoolTypeConvert(self._config['wrap_in_tuple']):
                    response = MessageHandler.WrapInTuple(response)

                # Restore method_attrs if they were over-ridden
                if method_attrs_holder:
                    self._soappyservice.soapproxy.methodattrs = method_attrs_holder

                return response