pip_services3_commons.commands.Command module

pip_services3_commons.commands.Command

Command 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.Command.Command(name: str, schema: pip_services3_commons.validate.Schema.Schema, function: Union[Callable, pip_services3_commons.run.IExecutable.IExecutable])

Bases: pip_services3_commons.commands.ICommand.ICommand

Concrete implementation of ICommand interface. Command allows to call a method or function using Command pattern.

Example:

def handler(*args):
    param1 = args.getAsFloat("param1")
    param2 = args.getAsFloat("param2")
    return param1 + param2

command_name = Command("add", None, handler)

result = command_name.execute("123",  Parameters.fromTuples("param1", 2, "param2", 2))

print result.__str__()

See ICommand, CommandSet

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

Executes the command_name. Before execution is validates Parameters args using the defined schema. The command_name execution intercepts ApplicationException raised by the called function and throws them.

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

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

Returns

an execution result.

Raises

ApplicationException: when execution fails for whatever reason.

get_name()str

Gets the command_name name.

Returns

the command_name name

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

Performs validation of the command_name arguments.

Parameters

args – the parameters (arguments) to validate using this command_name’s schema.

Returns

an array of ValidationResult or an empty array (if no schema is set).