Command

Command allows to call a method or a function.

Implements: ICommand

Description

The Command class allows you to call a method or a function.

Constructors

Creates a new command object and assigns it’s parameters.

Command(name: str, schema: Schema, function: Union[Callable, IExecutable])

  • name: str - the command name.
  • schema: Schema - the schema to validate command arguments.
  • action: IExecutable - the function to be executed by this command.

Instance methods

execute

Executes the command. Before execution it validates args using the defined schema.

Raise ApplicationException when execution fails for whatever reason.
See Parameters

execute(correlation_id: Optional[str], args: Parameters): Any

  • correlation_id: Optional[str] - (optional) transaction id to trace execution through call chain.
  • args: Parameters - the parameters (arguments) to pass to this command for execution.
  • returns: Any - the execution result

get_name

Gets the command name.

get_name(): str

  • returns: str - the name of this command.

validate

Validates the command args before execution using the defined schema.

validate(args: Parameters): List[ValidationResult]

  • args: Parameters - the parameters (arguments) to validate using this command’s schema.
  • returns: List[ValidationResult] - an array of ValidationResults or an empty array (if no schema is set).

Examples

def handler(args):
    param1 = args.getAsFloat("param1")
    param2 = args.getAsFloat("param2")
    return param1 + param2
command = Command("add", None, handler)
result = command.execute("123",  Parameters.fromTuples("param1", 2, "param2", 2))

print result.__str__()

See also