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) 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 ParsingException(msg)
def convert_from_xml(payload, param_name, cid, is_required, is_complex, default_value, path_prefix, use_text): try: elem = path('{}.{}'.format(path_prefix, param_name), is_required).get_from(payload) except ParsingException, e: msg = 'Caught an exception while parsing, payload:[<![CDATA[{}]]>], e:[{}]'.format( etree.tostring(payload), format_exc(e)) raise ParsingException(cid, msg)
def get_params(self, request_params, path_prefix='', default_value=ZATO_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: if isinstance(param, ForceType): param_name = param.name else: param_name = param if self.is_xml: try: elem = path('{}.{}'.format(path_prefix, param_name), is_required).get_from(self.payload) except ParsingException, e: msg = 'Caught an exception while parsing, payload:[<![CDATA[{}]]>], e:[{}]'.format( etree.tostring(self.payload), format_exc(e)) raise ParsingException(self.cid, msg) if isinstance(param, List): value = elem #.getchildren() else: if elem is not None: if use_text: value = elem.text # We are interested in the text the elem contains .. else: return elem # .. or in the elem itself. else: value = default_value else: value = self.payload.get(param_name) # Use a default value if an element is empty and we're allowed to # substitute its (empty) value with the default one. if default_value != ZATO_NO_DEFAULT_VALUE and value is None: value = default_value else: if value is not None: if not isinstance(param, List): value = unicode(value) try: if not isinstance(param, AsIs): params[param_name] = self.convert( param, param_name, value, self.has_simple_io_config, self.is_xml) else: params[param_name] = value except Exception, e: msg = 'Caught an exception, param:[{}], param_name:[{}], value:[{}], self.has_simple_io_config:[{}], e:[{}]'.format( param, param_name, value, self.has_simple_io_config, format_exc(e)) self.logger.error(msg) raise Exception(msg)
def convert_param(cid, payload, param, data_format, is_required, default_value, path_prefix, use_text, channel_params, has_simple_io_config, bool_parameter_prefixes, int_parameters, int_parameter_suffixes): """ Converts request parameters from any data format supported into Python objects. """ param_name = param.name if isinstance(param, ForceType) else param value = convert_impl[data_format](payload, param_name, cid, is_required, isinstance(param, COMPLEX_VALUE), default_value, path_prefix, use_text) if value == NOT_GIVEN: if default_value != NO_DEFAULT_VALUE: value = default_value else: if is_required and not channel_params.get(param_name): msg = 'Required input element:[{}] not found, value:[{}]'.format( param, value) raise ParsingException(cid, msg) else: # Not required and not provided on input value = '' else: if value is not None and not isinstance(param, COMPLEX_VALUE): value = unicode(value) if not isinstance(param, AsIs): return param_name, convert_sio( param, param_name, value, has_simple_io_config, data_format == DATA_FORMAT.XML, bool_parameter_prefixes, int_parameters, int_parameter_suffixes, None, data_format, False) return param_name, value
def convert_param(cid, payload, param, data_format, is_required, default_value, path_prefix, use_text, channel_params, has_simple_io_config, bool_parameter_prefixes, int_parameters, int_parameter_suffixes, params_priority): """ Converts request parameters from any data format supported into Python objects. """ param_name = param.name if isinstance(param, ForceType) else param # First thing is to find out if we have parameters in channel_params. If so and they have priority # over payload, we don't look further. If they don't have priority, whether the value from channel_params # is used depends on whether the payload one exists at all. # We've got a value from the channel, i.e. in GET parameters channel_value = channel_params.get(param_name, ZATO_NONE) # Convert it to a native Python data type if channel_value != ZATO_NONE: channel_value = convert_sio(param, param_name, channel_value, has_simple_io_config, False, bool_parameter_prefixes, int_parameters, int_parameter_suffixes, None, data_format, False) # Return the value immediately if we already know channel_params are of higer priority if params_priority == PARAMS_PRIORITY.CHANNEL_PARAMS_OVER_MSG and channel_value != ZATO_NONE: return param_name, channel_value # Ok, at that point we either don't have anything in channel_params or they don't have priority over payload. if payload is not None: value = convert_impl[data_format](payload, param_name, cid, is_required, isinstance(param, COMPLEX_VALUE), default_value, path_prefix, use_text) else: value = NOT_GIVEN if value == NOT_GIVEN: if default_value != NO_DEFAULT_VALUE: value = default_value else: if is_required: # Ok, we don't have anything in payload but it still may be in channel_params. # We arrive here if params priority is not params over msg. value = channel_value if (channel_value is not None and channel_value != ZATO_NONE) else ZATO_NONE if value == ZATO_NONE: msg = 'Required input element:`{}` not found, value:`{}`, data_format:`{}`, payload:`{}`'\ ', channel_params:`{}`'.format(param, value, data_format, payload, channel_params) raise ParsingException(cid, msg) else: # Not required and not provided on input either in msg or channel params value = '' else: if value is not None and not isinstance(param, COMPLEX_VALUE): value = unicode(value) if not isinstance(param, AsIs): return param_name, convert_sio(param, param_name, value, has_simple_io_config, data_format==DATA_FORMAT.XML, bool_parameter_prefixes, int_parameters, int_parameter_suffixes, None, data_format, False) return param_name, value
def convert_from_xml(payload, param_name, cid, is_required, is_complex, default_value, path_prefix, use_text): try: elem = path('{}.{}'.format(path_prefix, param_name), is_required).get_from(payload) except ParsingException: msg = 'Caught an exception while parsing, payload:`<![CDATA[{}]]>`, e:`{}`'.format( etree.tostring(payload), format_exc()) raise ParsingException(cid, msg) if is_complex: value = elem else: if elem is not None: if use_text: value = elem.text # We are interested in the text the elem contains .. else: return elem # .. or in the elem itself. else: value = default_value return value