Exemple #1
0
class Request(SIOConverter):
    """ Wraps a service request and adds some useful meta-data.
    """
    __slots__ = ('logger', 'payload', 'raw_request', 'input', 'cid',
                 'has_simple_io_config', 'simple_io_config',
                 'bool_parameter_prefixes', 'int_parameters',
                 'int_parameter_suffixes', 'is_xml', 'data_format',
                 'transport', '_wsgi_environ', 'channel_params',
                 'merge_channel_params')

    def __init__(self,
                 logger,
                 simple_io_config={},
                 data_format=None,
                 transport=None):
        self.logger = logger
        self.payload = ''
        self.raw_request = ''
        self.input = ServiceInput()
        self.cid = None
        self.simple_io_config = simple_io_config
        self.has_simple_io_config = False
        self.bool_parameter_prefixes = simple_io_config.get(
            'bool_parameter_prefixes', [])
        self.int_parameters = simple_io_config.get('int_parameters', [])
        self.int_parameter_suffixes = simple_io_config.get(
            'int_parameter_suffixes', [])
        self.is_xml = None
        self.data_format = data_format
        self.transport = transport
        self.http = HTTPRequestData()
        self._wsgi_environ = None
        self.channel_params = {}
        self.merge_channel_params = True
        self.params_priority = PARAMS_PRIORITY.DEFAULT

    def init(self, is_sio, cid, sio, data_format, transport, wsgi_environ):
        """ Initializes the object with an invocation-specific data.
        """

        if is_sio:
            self.is_xml = data_format == SIMPLE_IO.FORMAT.XML
            self.data_format = data_format
            self.transport = transport
            self._wsgi_environ = wsgi_environ

            path_prefix = getattr(sio, 'request_elem', 'request')
            required_list = getattr(sio, 'input_required', [])
            optional_list = getattr(sio, 'input_optional', [])
            default_value = getattr(sio, 'default_value', NO_DEFAULT_VALUE)
            use_text = getattr(sio, 'use_text', True)
            use_channel_params_only = getattr(sio, 'use_channel_params_only',
                                              False)

            if self.simple_io_config:
                self.has_simple_io_config = True
                self.bool_parameter_prefixes = self.simple_io_config.get(
                    'bool_parameter_prefixes', [])
                self.int_parameters = self.simple_io_config.get(
                    'int_parameters', [])
                self.int_parameter_suffixes = self.simple_io_config.get(
                    'int_parameter_suffixes', [])
            else:
                self.payload = self.raw_request

            required_params = {}

            if required_list:

                # Needs to check for this exact default value to prevent a FutureWarning in 'if not self.payload'
                if self.payload == '' and not self.channel_params:
                    raise ZatoException(cid, 'Missing input')

                required_params.update(
                    self.get_params(required_list, use_channel_params_only,
                                    path_prefix, default_value, use_text))

            if optional_list:
                optional_params = self.get_params(optional_list,
                                                  use_channel_params_only,
                                                  path_prefix, default_value,
                                                  use_text, False)
            else:
                optional_params = {}

            self.input.update(required_params)
            self.input.update(optional_params)

            for param, value in self.channel_params.iteritems():
                if param not in self.input:
                    self.input[param] = value

        # We merge channel params in if requested even if it's not SIO
        else:
            if self.merge_channel_params:
                self.input.update(self.channel_params)

    def get_params(self,
                   params_to_visit,
                   use_channel_params_only,
                   path_prefix='',
                   default_value=NO_DEFAULT_VALUE,
                   use_text=True,
                   is_required=True):
        """ Gets all requested parameters from a message. Will raise ParsingException if any is missing.
        """
        params = {}

        for param in params_to_visit:
            try:
                param_name, value = convert_param(
                    self.cid, '' if use_channel_params_only else self.payload,
                    param, self.data_format, is_required, default_value,
                    path_prefix, use_text, self.channel_params,
                    self.has_simple_io_config, self.bool_parameter_prefixes,
                    self.int_parameters, self.int_parameter_suffixes,
                    self.params_priority)
                params[param_name] = value

            except Exception, e:
                msg = 'Caught an exception, param:`{}`, params_to_visit:`{}`, has_simple_io_config:`{}`, e:`{}`'.format(
                    param, params_to_visit, self.has_simple_io_config,
                    format_exc(e))
                self.logger.error(msg)
                raise Exception(msg)

        return params
class Request(SIOConverter):
    """ Wraps a service request and adds some useful meta-data.
    """
    __slots__ = ('logger', 'payload', 'raw_request', 'input', 'cid', 'has_simple_io_config',
                 'simple_io_config', 'bool_parameter_prefixes', 'int_parameters',
                 'int_parameter_suffixes', 'is_xml', 'data_format', 'transport',
                 '_wsgi_environ', 'channel_params', 'merge_channel_params')

    def __init__(self, logger, simple_io_config={}, data_format=None, transport=None):
        self.logger = logger
        self.payload = ''
        self.raw_request = ''
        self.input = ServiceInput()
        self.cid = None
        self.simple_io_config = simple_io_config
        self.has_simple_io_config = False
        self.bool_parameter_prefixes = simple_io_config.get('bool_parameter_prefixes', [])
        self.int_parameters = simple_io_config.get('int_parameters', [])
        self.int_parameter_suffixes = simple_io_config.get('int_parameter_suffixes', [])
        self.is_xml = None
        self.data_format = data_format
        self.transport = transport
        self.http = HTTPRequestData()
        self._wsgi_environ = None
        self.channel_params = {}
        self.merge_channel_params = True
        self.params_priority = PARAMS_PRIORITY.DEFAULT

    def init(self, is_sio, cid, sio, data_format, transport, wsgi_environ):
        """ Initializes the object with an invocation-specific data.
        """

        if is_sio:
            self.is_xml = data_format == SIMPLE_IO.FORMAT.XML
            self.data_format = data_format
            self.transport = transport
            self._wsgi_environ = wsgi_environ

            path_prefix = getattr(sio, 'request_elem', 'request')
            required_list = getattr(sio, 'input_required', [])
            optional_list = getattr(sio, 'input_optional', [])
            default_value = getattr(sio, 'default_value', NO_DEFAULT_VALUE)
            use_text = getattr(sio, 'use_text', True)
            use_channel_params_only = getattr(sio, 'use_channel_params_only', False)

            if self.simple_io_config:
                self.has_simple_io_config = True
                self.bool_parameter_prefixes = self.simple_io_config.get('bool_parameter_prefixes', [])
                self.int_parameters = self.simple_io_config.get('int_parameters', [])
                self.int_parameter_suffixes = self.simple_io_config.get('int_parameter_suffixes', [])
            else:
                self.payload = self.raw_request

            required_params = {}

            if required_list:

                # Needs to check for this exact default value to prevent a FutureWarning in 'if not self.payload'
                if self.payload == '' and not self.channel_params:
                    raise ZatoException(cid, 'Missing input')

                required_params.update(self.get_params(
                    required_list, use_channel_params_only, path_prefix, default_value, use_text))

            if optional_list:
                optional_params = self.get_params(
                    optional_list, use_channel_params_only, path_prefix, default_value, use_text, False)
            else:
                optional_params = {}

            self.input.update(required_params)
            self.input.update(optional_params)

            for param, value in self.channel_params.iteritems():
                if param not in self.input:
                    self.input[param] = value

        # We merge channel params in if requested even if it's not SIO
        else:
            if self.merge_channel_params:
                self.input.update(self.channel_params)

    def get_params(self, params_to_visit, use_channel_params_only, path_prefix='', default_value=NO_DEFAULT_VALUE,
            use_text=True, is_required=True):
        """ Gets all requested parameters from a message. Will raise ParsingException if any is missing.
        """
        params = {}

        for param in params_to_visit:
            try:
                param_name, value = convert_param(
                    self.cid, '' if use_channel_params_only else self.payload, param, self.data_format, is_required,
                    default_value, path_prefix, use_text, self.channel_params, self.has_simple_io_config,
                    self.bool_parameter_prefixes, self.int_parameters, self.int_parameter_suffixes, self.params_priority)
                params[param_name] = value

            except Exception, e:
                msg = 'Caught an exception, param:`{}`, params_to_visit:`{}`, has_simple_io_config:`{}`, e:`{}`'.format(
                    param, params_to_visit, self.has_simple_io_config, format_exc(e))
                self.logger.error(msg)
                raise Exception(msg)

        return params
Exemple #3
0
class Request(SIOConverter):
    """ Wraps a service request and adds some useful meta-data.
    """
    __slots__ = ('logger', 'payload', 'raw_request', 'input', 'cid', 'has_simple_io_config',
                 'simple_io_config', 'bool_parameter_prefixes', 'int_parameters',
                 'int_parameter_suffixes', 'is_xml', 'data_format', 'transport',
                 '_wsgi_environ', 'channel_params', 'merge_channel_params')

    def __init__(self, logger, simple_io_config={}, data_format=None, transport=None):
        self.logger = logger
        self.payload = ''
        self.raw_request = ''
        self.input = ServiceInput()
        self.cid = None
        self.simple_io_config = simple_io_config
        self.has_simple_io_config = False
        self.bool_parameter_prefixes = simple_io_config.get('bool_parameter_prefixes', [])
        self.int_parameters = simple_io_config.get('int_parameters', [])
        self.int_parameter_suffixes = simple_io_config.get('int_parameter_suffixes', [])
        self.is_xml = None
        self.data_format = data_format
        self.transport = transport
        self.http = HTTPRequestData()
        self._wsgi_environ = None
        self.channel_params = {}
        self.merge_channel_params = True
        self.params_priority = PARAMS_PRIORITY.DEFAULT

    def init(self, is_sio, cid, io, data_format, transport, wsgi_environ):
        """ Initializes the object with an invocation-specific data.
        """

        if is_sio:
            self.is_xml = data_format == SIMPLE_IO.FORMAT.XML
            self.data_format = data_format
            self.transport = transport
            self._wsgi_environ = wsgi_environ

            path_prefix = getattr(io, 'request_elem', 'request')
            required_list = getattr(io, 'input_required', [])
            optional_list = getattr(io, 'input_optional', [])
            default_value = getattr(io, 'default_value', NO_DEFAULT_VALUE)
            use_text = getattr(io, 'use_text', True)

            if self.simple_io_config:
                self.has_simple_io_config = True
                self.bool_parameter_prefixes = self.simple_io_config.get('bool_parameter_prefixes', [])
                self.int_parameters = self.simple_io_config.get('int_parameters', [])
                self.int_parameter_suffixes = self.simple_io_config.get('int_parameter_suffixes', [])
            else:
                self.payload = self.raw_request

            if required_list:
                required_params = self.get_params(required_list, path_prefix, default_value, use_text)
            else:
                required_params = {}

            if optional_list:
                optional_params = self.get_params(optional_list, path_prefix, default_value, use_text, False)
            else:
                optional_params = {}

            if self.params_priority == PARAMS_PRIORITY.CHANNEL_PARAMS_OVER_MSG:
                self.input.update(required_params)
                self.input.update(optional_params)
                self.input.update(self.channel_params)
            else:
                self.input.update(self.channel_params)
                self.input.update(required_params)
                self.input.update(optional_params)

        # We merge channel params in if requested even if it's not SIO
        else:
            if self.merge_channel_params:
                self.input.update(self.channel_params)

    def get_params(self, request_params, path_prefix='', default_value=NO_DEFAULT_VALUE, use_text=True, is_required=True):
        """ Gets all requested parameters from a message. Will raise ParsingException if any is missing.
        """
        params = {}
        if not isinstance(self.payload, basestring):
            for param in request_params:
                try:
                    param_name, value = convert_param(self.cid, self.payload, param, self.data_format, is_required, default_value,
                            path_prefix, use_text, self.channel_params, self.has_simple_io_config,
                            self.bool_parameter_prefixes, self.int_parameters, self.int_parameter_suffixes)
                    params[param_name] = value
    
                except Exception, e:
                    msg = 'Caught an exception, param:[{}], self.has_simple_io_config:[{}], e:[{}]'.format(
                        param, self.has_simple_io_config, format_exc(e))
                    self.logger.error(msg)
                    raise Exception(msg)
        else:
Exemple #4
0
class Request(SIOConverter):
    """ Wraps a service request and adds some useful meta-data.
    """
    __slots__ = ('logger', 'payload', 'raw_request', 'input', 'cid',
                 'has_simple_io_config', 'simple_io_config',
                 'bool_parameter_prefixes', 'int_parameters',
                 'int_parameter_suffixes', 'is_xml', 'data_format',
                 'transport', '_wsgi_environ', 'channel_params',
                 'merge_channel_params', 'http', 'amqp', 'wmq', 'ibm_mq',
                 'enforce_string_encoding')

    def __init__(self,
                 logger,
                 simple_io_config=None,
                 data_format=None,
                 transport=None):
        self.logger = logger
        self.payload = ''
        self.raw_request = ''
        self.input = {}  # Will be overwritten in self.init if necessary
        self.cid = None
        self.simple_io_config = simple_io_config or {}
        self.has_simple_io_config = False
        self.bool_parameter_prefixes = self.simple_io_config.get(
            'bool_parameter_prefixes', [])
        self.int_parameters = self.simple_io_config.get('int_parameters', [])
        self.int_parameter_suffixes = self.simple_io_config.get(
            'int_parameter_suffixes', [])
        self.is_xml = None
        self.data_format = data_format
        self.transport = transport
        self.http = HTTPRequestData()
        self._wsgi_environ = None
        self.channel_params = {}
        self.merge_channel_params = True
        self.params_priority = PARAMS_PRIORITY.DEFAULT
        self.amqp = None
        self.wmq = self.ibm_mq = None
        self.encrypt_func = None
        self.encrypt_secrets = True
        self.bytes_to_str_encoding = None

# ################################################################################################################################

    def init(self, is_sio, cid, sio, data_format, transport, wsgi_environ,
             encrypt_func):
        """ Initializes the object with an invocation-specific data.
        """
        self.input = ServiceInput()
        self.encrypt_func = encrypt_func

        if is_sio:
            required_list = getattr(sio, 'input_required', [])
            required_list = [required_list] if isinstance(
                required_list, basestring) else required_list
            self.init_flat_sio(cid, sio, data_format, transport, wsgi_environ,
                               required_list)

        # We merge channel params in if requested even if it's not SIO
        else:
            if self.merge_channel_params:
                self.input.update(self.channel_params)

# ################################################################################################################################

    def init_flat_sio(self,
                      cid,
                      sio,
                      data_format,
                      transport,
                      wsgi_environ,
                      required_list,
                      _sio_container=(tuple, list)):
        """ Initializes flat SIO requests, i.e. not list ones.
        """
        self.is_xml = data_format == SIMPLE_IO.FORMAT.XML
        self.data_format = data_format
        self.transport = transport
        self._wsgi_environ = wsgi_environ

        optional_list = getattr(sio, 'input_optional', [])
        optional_list = optional_list if isinstance(
            optional_list, _sio_container) else [optional_list]

        path_prefix = getattr(sio, 'request_elem', 'request')
        default_value = getattr(sio, 'default_value', NO_DEFAULT_VALUE)
        use_text = getattr(sio, 'use_text', True)
        use_channel_params_only = getattr(sio, 'use_channel_params_only',
                                          False)
        self.encrypt_secrets = getattr(sio, 'encrypt_secrets', True)

        if self.simple_io_config:
            self.has_simple_io_config = True
            self.bool_parameter_prefixes = self.simple_io_config.get(
                'bool_parameter_prefixes', [])
            self.int_parameters = self.simple_io_config.get(
                'int_parameters', [])
            self.int_parameter_suffixes = self.simple_io_config.get(
                'int_parameter_suffixes', [])
            self.bytes_to_str_encoding = self.simple_io_config['bytes_to_str'][
                'encoding']
        else:
            self.payload = self.raw_request

        required_params = {}

        if required_list:

            required_list = required_list if isinstance(
                required_list, _sio_container) else [required_list]

            # Needs to check for this exact default value to prevent a FutureWarning in 'if not self.payload'
            if self.payload == '' and not self.channel_params:
                raise ZatoException(cid, 'Missing input')

            required_params.update(
                self.get_params(required_list, use_channel_params_only,
                                path_prefix, default_value, use_text))

        if optional_list:
            optional_params = self.get_params(optional_list,
                                              use_channel_params_only,
                                              path_prefix, default_value,
                                              use_text, False)
        else:
            optional_params = {}

        self.input.update(required_params)
        self.input.update(optional_params)

        for param, value in iteritems(self.channel_params):
            if param not in self.input:
                self.input[param] = value

# ################################################################################################################################

    def get_params(self,
                   params_to_visit,
                   use_channel_params_only,
                   path_prefix='',
                   default_value=NO_DEFAULT_VALUE,
                   use_text=True,
                   is_required=True):
        """ Gets all requested parameters from a message. Will raise ParsingException if any is missing.
        """
        params = {}

        for param in params_to_visit:
            try:

                param_name, value = convert_param(
                    self.cid, '' if use_channel_params_only else self.payload,
                    param, self.data_format, is_required, default_value,
                    path_prefix, use_text, self.channel_params,
                    self.has_simple_io_config, self.bool_parameter_prefixes,
                    self.int_parameters, self.int_parameter_suffixes, True,
                    self.encrypt_func, self.encrypt_secrets,
                    self.params_priority)

                if self.bytes_to_str_encoding and isinstance(value, bytes):
                    value = value.decode(self.bytes_to_str_encoding)

                params[param_name] = value

            except Exception:
                msg = 'Caught an exception, param:`{}`, params_to_visit:`{}`, has_simple_io_config:`{}`, e:`{}`'.format(
                    param, params_to_visit, self.has_simple_io_config,
                    format_exc())
                self.logger.error(msg)
                raise ParsingException(msg)

        return params

# ################################################################################################################################

    def deepcopy(self):
        """ Returns a deep copy of self.
        """
        request = Request(None)
        request.logger = logging.getLogger(self.logger.name)

        for name in Request.__slots__:
            if name == 'logger':
                continue
            setattr(request, name, deepcopy(getattr(self, name)))

        return request

# ################################################################################################################################

    def bunchified(self):
        """ Returns a bunchified (converted into bunch.Bunch) version of self.raw_request,
        deep copied if it's a dict (or a subclass). Note that it makes sense to use this method
        only with dicts or JSON input.
        """
        # We have a dict
        if isinstance(self.raw_request, dict):
            return bunchify(deepcopy(self.raw_request))

        # Must be a JSON input, raises exception when attempting to load it if it's not
        return bunchify(loads(self.raw_request))
Exemple #5
0
class Request(SIOConverter):
    """ Wraps a service request and adds some useful meta-data.
    """
    __slots__ = ('logger', 'payload', 'raw_request', 'input', 'cid',
                 'has_simple_io_config', 'simple_io_config',
                 'bool_parameter_prefixes', 'int_parameters',
                 'int_parameter_suffixes', 'is_xml', 'data_format',
                 'transport', '_wsgi_environ', 'channel_params',
                 'merge_channel_params')

    def __init__(self,
                 logger,
                 simple_io_config={},
                 data_format=None,
                 transport=None):
        self.logger = logger
        self.payload = ''
        self.raw_request = ''
        self.input = ServiceInput()
        self.cid = None
        self.simple_io_config = simple_io_config
        self.has_simple_io_config = False
        self.bool_parameter_prefixes = simple_io_config.get(
            'bool_parameter_prefixes', [])
        self.int_parameters = simple_io_config.get('int_parameters', [])
        self.int_parameter_suffixes = simple_io_config.get(
            'int_parameter_suffixes', [])
        self.is_xml = None
        self.data_format = data_format
        self.transport = transport
        self.http = HTTPRequestData()
        self._wsgi_environ = None
        self.channel_params = {}
        self.merge_channel_params = True
        self.params_priority = PARAMS_PRIORITY.DEFAULT

    def init(self, is_sio, cid, io, data_format, transport, wsgi_environ):
        """ Initializes the object with an invocation-specific data.
        """

        if is_sio:
            self.is_xml = data_format == SIMPLE_IO.FORMAT.XML
            self.data_format = data_format
            self.transport = transport
            self._wsgi_environ = wsgi_environ

            path_prefix = getattr(io, 'request_elem', 'request')
            required_list = getattr(io, 'input_required', [])
            optional_list = getattr(io, 'input_optional', [])
            default_value = getattr(io, 'default_value', NO_DEFAULT_VALUE)
            use_text = getattr(io, 'use_text', True)

            if self.simple_io_config:
                self.has_simple_io_config = True
                self.bool_parameter_prefixes = self.simple_io_config.get(
                    'bool_parameter_prefixes', [])
                self.int_parameters = self.simple_io_config.get(
                    'int_parameters', [])
                self.int_parameter_suffixes = self.simple_io_config.get(
                    'int_parameter_suffixes', [])
            else:
                self.payload = self.raw_request

            if required_list:
                required_params = self.get_params(required_list, path_prefix,
                                                  default_value, use_text)
            else:
                required_params = {}

            if optional_list:
                optional_params = self.get_params(optional_list, path_prefix,
                                                  default_value, use_text,
                                                  False)
            else:
                optional_params = {}

            if self.params_priority == PARAMS_PRIORITY.CHANNEL_PARAMS_OVER_MSG:
                self.input.update(required_params)
                self.input.update(optional_params)
                self.input.update(self.channel_params)
            else:
                self.input.update(self.channel_params)
                self.input.update(required_params)
                self.input.update(optional_params)

        # We merge channel params in if requested even if it's not SIO
        else:
            if self.merge_channel_params:
                self.input.update(self.channel_params)

    def get_params(self,
                   request_params,
                   path_prefix='',
                   default_value=NO_DEFAULT_VALUE,
                   use_text=True,
                   is_required=True):
        """ Gets all requested parameters from a message. Will raise ParsingException if any is missing.
        """
        params = {}
        if not isinstance(self.payload, basestring):
            for param in request_params:
                try:
                    param_name, value = convert_param(
                        self.cid, self.payload, param, self.data_format,
                        is_required, default_value, path_prefix, use_text,
                        self.channel_params, self.has_simple_io_config,
                        self.bool_parameter_prefixes, self.int_parameters,
                        self.int_parameter_suffixes)
                    params[param_name] = value

                except Exception, e:
                    msg = 'Caught an exception, param:[{}], self.has_simple_io_config:[{}], e:[{}]'.format(
                        param, self.has_simple_io_config, format_exc(e))
                    self.logger.error(msg)
                    raise Exception(msg)
        else: