Skip to content

Commit

Permalink
update worker examples
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Aug 4, 2024
1 parent 2027dcf commit f7091d7
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 36 deletions.
53 changes: 36 additions & 17 deletions docs/backend/api/action/worker-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,43 @@ More information about the worker at: https://github.com/apioo/fusio-worker-java
## Example

```groovy
def connection = connector.getConnection("app");
def entries = [];
def query = "SELECT name, description FROM app_product_0";
try (def stmt = connection.createStatement()) {
def rs = stmt.executeQuery(query);
while (rs.next()) {
entries.add([
name: rs.getString("name"),
description: rs.getString("description")
]);
import org.fusioproject.worker.runtime.generated.ExecuteRequest;
import org.fusioproject.worker.runtime.generated.ExecuteContext;
import org.fusioproject.worker.runtime.Connector;
import org.fusioproject.worker.runtime.ResponseBuilder;
import org.fusioproject.worker.runtime.Dispatcher;
import org.fusioproject.worker.runtime.Logger;
def handle(ExecuteRequest request, ExecuteContext context, Connector connector, ResponseBuilder response, Dispatcher dispatcher, Logger logger) {
def connection = connector.getConnection("App");
def filter = request.getArguments().get("filter");
def query = "SELECT id, title, content, insert_date FROM my_blog";
if (filter) {
query += " WHERE title LIKE ?"
}
}
response.build(200, [:], [
foo: "bar",
entries: entries
]);
def entries = [];
try (def ps = connection.prepareStatement(query)) {
if (filter) {
ps.setString(1, "%" + filter + "%");
}
def rs = ps.executeQuery();
while (rs.next()) {
entries.add([
id: rs.getInt("id"),
name: rs.getString("title"),
description: rs.getString("content"),
insertDate: rs.getString("insert_date")
]);
}
}
return response.build(200, [:], [
entries: entries
]);
}
```

Expand All @@ -37,7 +56,7 @@ and which implementation is used:
| ---- | --------------
| `Fusio.Adapter.Sql.Connection.Sql` | `java.sql.Connection`
| `Fusio.Adapter.Sql.Connection.SqlAdvanced` | `java.sql.Connection`
| `Fusio.Adapter.Http.Connection.Http` | `org.apache.http.client.HttpClient`
| `Fusio.Adapter.Http.Connection.Http` | `org.apache.hc.client5.http.impl.classic.HttpClient`
| `Fusio.Adapter.Mongodb.Connection.MongoDB` | `com.mongodb.client.MongoDatabase`
| `Fusio.Adapter.Elasticsearch.Connection.Elasticsearch` | `org.elasticsearch.client.RestHighLevelClient`

Expand Down
25 changes: 21 additions & 4 deletions docs/backend/api/action/worker-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,28 @@ More information about the worker at: https://github.com/apioo/fusio-worker-java

```javascript
module.exports = async function(request, context, connector, response, dispatcher, logger) {
const connection = await connector.getConnection('app');
const [entries, fields] = await connection.query('SELECT name, description FROM app_product_0');
const connection = await connector.getConnection('App');
const filter = request.arguments['filter'];

const values = [];
let query = 'SELECT id, title, content, insert_date FROM my_blog';
if (filter) {
query += ' WHERE title LIKE ?'
values.push('%' + filter + '%');
}

const entries = [];
const [result] = await connection.execute(query, values);
result.forEach((row) => {
entries.push({
id: row.id,
name: row.title,
description: row.content,
insertDate: row.insert_date,
});
});

response.build(200, {}, {
foo: 'bar',
return response.build(200, {}, {
entries: entries
});
};
Expand Down
24 changes: 20 additions & 4 deletions docs/backend/api/action/worker-php-local.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ action through the config. More information about the worker at: https://github.
```php
<?php

use Doctrine\DBAL\Connection;
use Fusio\Worker\ExecuteContext;
use Fusio\Worker\ExecuteRequest;
use Fusio\Engine\ConnectorInterface;
Expand All @@ -20,13 +21,28 @@ use Fusio\Engine\DispatcherInterface;
use Psr\Log\LoggerInterface;

return function(ExecuteRequest $request, ExecuteContext $context, ConnectorInterface $connector, FactoryInterface $response, DispatcherInterface $dispatcher, LoggerInterface $logger) {
$connection = $connector->getConnection('app');
$connection = $connector->getConnection('App');
$filter = $request->getArguments()->get('filter');

$query = 'SELECT name, description FROM app_product_0';
$entries = $connection->fetchAllAssociative($query);
$params = [];
$query = 'SELECT id, title, content, insert_date FROM my_blog';
if (!empty($filter)) {
$query .= ' WHERE title LIKE ?';
$params[] = $filter;
}

$entries = [];
$result = $connection->fetchAllAssociative($query, $params);
foreach ($result as $row) {
$entries[] = [
'id' => (int) $row['id'],
'name' => $row['title'],
'description' => $row['content'],
'insertDate' => $row['insert_date'],
];
}

return $response->build(200, [], [
'foo' => 'bar',
'entries' => $entries,
]);
};
Expand Down
24 changes: 20 additions & 4 deletions docs/backend/api/action/worker-php.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ More information about the worker at: https://github.com/apioo/fusio-worker-php
```php
<?php

use Doctrine\DBAL\Connection;
use Fusio\Worker\ExecuteContext;
use Fusio\Worker\ExecuteRequest;
use Fusio\Engine\ConnectorInterface;
Expand All @@ -17,13 +18,28 @@ use Fusio\Engine\DispatcherInterface;
use Psr\Log\LoggerInterface;

return function(ExecuteRequest $request, ExecuteContext $context, ConnectorInterface $connector, FactoryInterface $response, DispatcherInterface $dispatcher, LoggerInterface $logger) {
$connection = $connector->getConnection('app');
$connection = $connector->getConnection('App');
$filter = $request->getArguments()->get('filter');

$query = 'SELECT name, description FROM app_product_0';
$entries = $connection->fetchAllAssociative($query);
$params = [];
$query = 'SELECT id, title, content, insert_date FROM my_blog';
if (!empty($filter)) {
$query .= ' WHERE title LIKE ?';
$params[] = $filter;
}

$entries = [];
$result = $connection->fetchAllAssociative($query, $params);
foreach ($result as $row) {
$entries[] = [
'id' => (int) $row['id'],
'name' => $row['title'],
'description' => $row['content'],
'insertDate' => $row['insert_date'],
];
}

return $response->build(200, [], [
'foo' => 'bar',
'entries' => $entries,
]);
};
Expand Down
22 changes: 15 additions & 7 deletions docs/backend/api/action/worker-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@ More information about the worker at: https://github.com/apioo/fusio-worker-pyth

```python
def handle(request, context, connector, response, dispatcher, logger):
connection = connector.get_connection('app')
connection = connector.get_connection('App')
filter = request.arguments.get('filter')

cursor = connection.cursor()
cursor.execute("""SELECT name, description FROM app_product_0""")
values = []
query = "SELECT id, title, content, insert_date FROM my_blog"
if filter:
query += " WHERE title LIKE ?"
values.append(filter)

cursor = connection.cursor(prepared=True)
cursor.execute(query, values)
result = cursor.fetchall()
cursor.close()

entries = []
for row in result:
entries.append({
'name': row[0],
'description': row[1],
'id': row[0],
'title': row[1],
'content': row[2],
'insert_date': row[3],
})

return response.build(200, None, {
'foo': 'bar',
'entries': entries
})

Expand All @@ -38,7 +46,7 @@ and which implementation is used:
| ---- | -------------- |
| `Fusio.Adapter.Sql.Connection.Sql` | `PyMySQL / pymongo`
| `Fusio.Adapter.Sql.Connection.SqlAdvanced` | `PyMySQL / pymongo`
| `Fusio.Adapter.Http.Connection.Http` | `http.client`
| `Fusio.Adapter.Http.Connection.Http` | `httpx.Client`
| `Fusio.Adapter.Mongodb.Connection.MongoDB` | `pymongo`
| `Fusio.Adapter.Elasticsearch.Connection.Elasticsearch` | `elasticsearch`

Expand Down

0 comments on commit f7091d7

Please sign in to comment.