Implementation Usage
For ease of use, Swimlane has two different APIs for interacting with Python scripts: a global and class level.
Global
The global usage was designed for scripters to be able to drop scripts into Swimlane, configure a few variables, and be operational with little effort. In this scenario, users will find anywhere they had arguments passed to the script via CLI/etc and replace them with the Swimlane context object sw_context. Additionally, Swimlane inserts a list of dictionaries (called sw_outputs) into each script that can be used to return data to Swimlane.
Take the following demo script:
This script would be migrated by replacing the apiKey and the ipToLookup variables with variables from the context object. Additionally, the standard output printing can be removed to use the sw_outputs object.
Since we can capture messages from the standard output, moving the output to the outputs object is not necessary.
Class
Developers who are familiar with object-oriented programming concepts can leverage the class architecture pattern.
Class applies to plugin tasks only, and not to custom scripts. Custom scripts do not contain plugin-specific behaviors like being able to use SwMain class.
When a developer defines a class with the name of SwMain, it will be automatically invoked with the sw_context object context argument. After the class is initialized, the execute method is invoked on that instance of the class. The execute method is where the 'work' of the task will be done. When finished, the execute method returns an array of dictionaries as the output.
With this pattern, the context is encapsulated into the SwMain singleton rather than accessing the global sw_context or sw_outputs objects and enforce a single exit point return object. This rigid API helps to create more predictable, more maintainable, and easier readability plugin pattern. Swimlaneβs internal plugins are authored in this format.