Setup¶
To process a command posted from a client, a command handler must be created. This is very similar to how endpoints and tasks are added to a project before a client application can use them.
Note
Setting up command handlers in a project is only required when adding custom commands.
Setting up command handlers requires the following steps:
- Create the
command_handlersfolder and copy the__init__.pyto it. - Import command handlers in the project's init.
- Create a handler.
- Write some tests!
1. Create the command_handlers folder and copy the __init__.py to it
Create a folder named command_handlers in the solution or package, next to the
endpoints folder.
Example:
cd solution-test/solution_test
mkdir command_handlers
Add the following __init__.py file to the created folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Import command handlers in the project's init
Next step is to import register_command_handlers when the solution or package
is initialized.
Add the following lines to the end of the project's __init__.py file.
Eg. solution-test/solution_test/__init__.py, given that the Lime Project is
named solution-test.
try:
from .command_handlers import register_command_handlers # noqa
except ImportError:
logger.info(
"solution_test doesn't implement any custom command handlers"
)
(remember to replace solution_test with the project name)
Create a handler
Create a file named as the command name and place it in the command_handlers
folder and add the command handler function to it.
1 2 3 4 5 6 7 8 9 10 11 | |
Attention
Command names are global and need to be unique. Two packages can't both
register the command name send-sms so pick a name that uses the solution
or package name.
Write some tests!
The example handler show how a pytest fixture can be used to "send" a command batch from a test case.