Skip to content

Commit

Permalink
feat: add invoke_script
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar committed Mar 28, 2022
1 parent 48294b0 commit 4ab5bd9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
29 changes: 29 additions & 0 deletions examples/InvocableScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,35 @@
$createdScript = $scriptsApi->updateScript($createdScript->getId(), $updateRequest);
print $createdScript;

//
// Invoke a script
//
print "\n------- Invoke to FluxTables -------\n";
$tables = $scriptsApi->invokeScript($createdScript->getId(), ["bucket_name" => $bucket]);
foreach ($tables as $table) {
foreach ($table->records as $record) {
print "\n${record['time']} ${record['location']}: ${record['_field']} ${record['_value']}";
}
}

//
// Stream of FluxRecords
//
print "\n";
print "\n------- Invoke to Stream of FluxRecords -------\n";
$records = $scriptsApi->invokeScriptStream($createdScript->getId(), ["bucket_name" => $bucket]);
foreach ($records->each() as $record) {
print "\n${record['time']} ${record['location']}: ${record['_field']} ${record['_value']}";
}

//
// RAW
//
print "\n";
print "\n------- Invoke to Raw-------\n";
$raw = $scriptsApi->invokeScriptRaw($createdScript->getId(), ["bucket_name" => $bucket]);
print "RAW output:\n\n ${raw}";

//
// List scripts
//
Expand Down
2 changes: 1 addition & 1 deletion src/InfluxDB2/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function createInvocableScriptsApi(): InvocableScriptsApi
{
/** @var InvocableScriptsService $service */
$service = $this->createService(InvocableScriptsService::class);
return new InvocableScriptsApi($service);
return new InvocableScriptsApi($this->options, $service);
}

/**
Expand Down
66 changes: 64 additions & 2 deletions src/InfluxDB2/InvocableScriptsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use InfluxDB2\Model\Script;
use InfluxDB2\Model\ScriptCreateRequest;
use InfluxDB2\Model\ScriptInvocationParams;
use InfluxDB2\Model\ScriptUpdateRequest;
use InfluxDB2\Service\InvocableScriptsService;
use Psr\Http\Message\StreamInterface;

/**
* Use API invokable scripts to create custom InfluxDB API endpoints that query, process, and shape data.
Expand All @@ -15,16 +17,19 @@
*
* @package InfluxDB2
*/
class InvocableScriptsApi
class InvocableScriptsApi extends DefaultApi
{
private $service;

/**
* InvocableScriptsApi constructor.
*
* @param array $options default array options
* @param InvocableScriptsService $service HTTP API for Invocable Scripts
*/
public function __construct(InvocableScriptsService $service)
public function __construct(array $options, InvocableScriptsService $service)
{
parent::__construct($options);
$this->service = $service;
}

Expand Down Expand Up @@ -74,4 +79,61 @@ public function findScripts(int $limit = null, int $offset = null): array
{
return $this->service->getScripts($limit, $offset)->getScripts();
}

/**
* Invoke synchronously a script and return result as a FluxTable[].
*
* @param string $scriptId The ID of the script to invoke. (required)
* @param array<string,object>|null $params Represent key/value pairs parameters to be injected into script
* @return FluxTable[]
*/
public function invokeScript(string $scriptId, array $params = null): ?array
{
$response = $this->invokeScriptRaw($scriptId, $params);

$parser = new FluxCsvParser($response, false, 'only_names');
$parser->parse();

return $parser->tables;
}

/**
* Invoke synchronously a script and return result as a stream of FluxRecord.
*
* @param string $scriptId The ID of the script to invoke. (required)
* @param array<string,object>|null $params Represent key/value pairs parameters to be injected into script
* @return FluxCsvParser generator of FluxRecords
*/
public function invokeScriptStream(string $scriptId, array $params = null): FluxCsvParser
{

$invocation_params = new ScriptInvocationParams();
$invocation_params->setParams($params);

$response = $this->_invokeScript($invocation_params, $scriptId);

return new FluxCsvParser($response, true, 'only_names');
}

/**
* Invoke synchronously a script and return result as a String.
*
* @param string $scriptId The ID of the script to invoke. (required)
* @param array|null $params Represent key/value pairs parameters to be injected into script
* @return string
*/
public function invokeScriptRaw(string $scriptId, array $params = null): ?string
{
$invocation_params = new ScriptInvocationParams();
$invocation_params->setParams($params);

$response = $this->_invokeScript($invocation_params, $scriptId);

return $response->getContents();
}

private function _invokeScript(ScriptInvocationParams $invocation_params, string $scriptId): StreamInterface
{
return $this->post($invocation_params->__toString(), "/api/v2/scripts/${scriptId}/invoke", [])->getBody();
}
}

0 comments on commit 4ab5bd9

Please sign in to comment.