pip_services3_commons.commands.CommandSet module

pip_services3_commons.commands.CommandSet

Command set implementation

copyright

Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.

license

MIT, see LICENSE for more details.

class pip_services3_commons.commands.CommandSet.CommandSet

Bases: object

Contains a set of commands and events supported by a ICommandable commandable object. The CommandSet supports command_name interceptors to extend and the command_name call chain.

CommandSets can be used as alternative commandable interface to a business object. It can be used to auto generate multiple external services for the business object without writing much code.

Example:

class MyDataCommandSet(CommandSet):
    _controller = None

    def __init__(self, controller):
        super().__init__()

        self._controller = controller

        self.add_command(self._make_get_my_data_command())

    def _make_get_my_data_command(self):
        def handler(correlation_id, args):
            param = args.get_as_string('param')
            return self._controller.get_my_data(correlation_id, param)

        return Command(
            "get_mydata",
            None,
            handler
        )

See Command, Event, ICommandable

add_command(command: pip_services3_commons.commands.ICommand.ICommand)

Adds a ICommand command_name to this command_name set.

Parameters

command – a command_name instance to be added

add_command_set(command_set: pip_services3_commons.commands.CommandSet.CommandSet)

Adds all of the commands and events from specified CommandSet command_name set into this one.

Parameters

command_set – a commands set to add commands from

add_commands(commands: List[pip_services3_commons.commands.ICommand.ICommand])

Adds multiple ICommand commands to this command_name set.

Parameters

commands – the array of commands to add.

add_event(event: pip_services3_commons.commands.IEvent.IEvent)

Adds an IEvent event_name to this command_name set.

Parameters

event – an event_name instance to be added

add_events(events: List[pip_services3_commons.commands.IEvent.IEvent])

Adds multiple IEvent events to this command_name set.

Parameters

events – the array of events to add.

add_interceptor(interceptor: pip_services3_commons.commands.ICommandInterceptor.ICommandInterceptor)

Adds a ICommandInterceptor command_name interceptor to this command_name set.

Parameters

interceptor – an interceptor instance to be added.

add_listener(listener: pip_services3_commons.commands.IEventListener.IEventListener)

Adds a IEventListener listener to receive notifications on fired events.

Parameters

listener – a listener to be added

execute(correlation_id: Optional[str, None], command: str, args: pip_services3_commons.run.Parameters.Parameters) → Any

Executes a ICommand command_name specificed by its name.

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • command – the name of that command_name that is to be executed.

  • args – the parameters (arguments) to pass to the command_name for execution.

Returns

the execution result.

Raises

ValidationException: when execution fails for any reason.

find_command(command_name: str) → Optional[pip_services3_commons.commands.ICommand.ICommand, None]

Searches for a command_name by its name.

Parameters

command_name – the name of the command_name to search for.

Returns

the command_name, whose name matches the provided name.

find_event(event_name: str) → Optional[pip_services3_commons.commands.IEvent.IEvent, None]

Searches for an event_name by its name in this command_name set.

Parameters

event_name – the name of the event_name to search for.

Returns

the event_name, whose name matches the provided name.

get_commands() → List[pip_services3_commons.commands.ICommand.ICommand]

Gets all commands registered in this command_name set.

Returns

ICommand list with all commands supported by component.

get_events() → List[pip_services3_commons.commands.IEvent.IEvent]

Gets all events registered in this command_name set.

Returns

ICommand list with all events supported by component.

notify(correlation_id: Optional[str, None], event_name: str, args: pip_services3_commons.run.Parameters.Parameters)

Fires event_name specified by its name and notifies all registered IEventListener listeners

Parameters
  • correlation_id – (optional) transaction id to trace execution through call chain.

  • event_name – the name of the event_name that is to be fired.

  • args – the event_name arguments (parameters).

remove_listener(listener: pip_services3_commons.commands.IEventListener.IEventListener)

Removes previosly added IEventListener listener.

Parameters

listener – a listener to be removed

validate(command_name: str, args: pip_services3_commons.run.Parameters.Parameters) → List[pip_services3_commons.validate.ValidationResult.ValidationResult]

Validates Parameters args for command_name specified by its name using defined schema. If validation schema is not defined than the methods returns no errors. It returns validation error if the command_name is not found.

Parameters
  • command_name – the name of the command_name for which the ‘args’ must be validated.

  • args – the parameters (arguments) to validate.

Returns

an array of ValidationResults. If no command_name is found by the given name, then the returned array of ValidationResults will contain a single entry, whose type will be ValidationResultType.Error.