def decorator(f): # Validate if feature_name is None or feature_name.strip() == '': logger.error('Missing feature name.') raise ValidationError('Feature name is required.') # Add feature if not exists if feature_name in self._features: logger.error('Feature "%s" is already registered.', feature_name) raise FeatureAlreadyRegisteredError(feature_name) assign_help_attrs(f, feature_name, ATTR_FEATURE_TYPE) wrapped = wrap_with_server(f, self.server) # Assign help attributes for thread decorator assign_help_attrs(wrapped, feature_name, ATTR_FEATURE_TYPE) self._features[feature_name] = wrapped if options: options_type = get_method_registration_options_type( feature_name) if options_type and not is_instance(options, options_type): raise TypeError( (f'Options of method "{feature_name}"' f' should be instance of type {options_type}')) self._feature_options[feature_name] = options logger.info('Registered "%s" with options "%s"', feature_name, options) return f
def _check_ret_type_and_send_response(self, method_name, method_type, msg_id, result): """Check if registered feature returns appropriate result type.""" if method_type == ATTR_FEATURE_TYPE: return_type = get_method_return_type(method_name) if not is_instance(result, return_type): error = JsonRpcInternalError().to_dict() self._send_response(msg_id, error=error) self._send_response(msg_id, result=result)