Worker code
This page explains how you write the code of a worker action. The structure is the same for every language: you write a single function which receives six arguments and returns a response. The examples on this page give an overview, the PHP API page contains a complete reference with examples.
Structure
The following shows a minimal action for every language:
PHP
<?php
use Fusio\Worker;
use Fusio\Engine;
use Psr\Log\LoggerInterface;
return function(Worker\ExecuteRequest $request, Worker\ExecuteContext $context, Engine\ConnectorInterface $connector, Engine\Response\FactoryInterface $response, Engine\DispatcherInterface $dispatcher, LoggerInterface $logger) {
return $response->build(200, [], [
'hello' => 'world',
]);
};
JavaScript
module.exports = async function(request, context, connector, response, dispatcher, logger) {
return response.build(200, {}, {
hello: 'world'
});
};
Python
def handle(request, context, connector, response, dispatcher, logger):
return response.build(200, None, {
'hello': 'world'
})
Java/Groovy
def handle(ExecuteRequest request, ExecuteContext context, Connector connector, ResponseBuilder response, Dispatcher dispatcher, Logger logger) {
return response.build(200, [:], [
hello: 'world'
]);
}
Arguments
Every worker function receives the same six arguments, independent of the language:
| Argument | Description |
|---|---|
request | The incoming request, contains the path and query parameters (arguments) and the request body (payload) |
context | Information about the invocation, i.e. the authenticated user and app |
connector | Returns the client of a connection which is configured at Fusio |
response | Builds the response of your action, i.e. with a status code and body |
dispatcher | Dispatches events, i.e. to notify webhooks |
logger | Writes log messages, which help you to debug your action |
The method names follow the conventions of each language, i.e. connector.getConnection('App') in JavaScript and
connector.get_connection('App') in Python.
API reference
- PHP API - A complete reference of all arguments and methods for PHP, with examples
- Worker-JavaScript, Worker-Python and Worker-Java - Examples and the supported connection types for each language