Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mappings on endpoint input and output #154

Merged
merged 4 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
['name' => 'mappings#saveObject', 'url' => '/api/mappings/objects', 'verb' => 'POST'],
['name' => 'mappings#getObjects', 'url' => '/api/mappings/objects', 'verb' => 'GET'],
// Running endpoints - allow any path after /api/endpoints/
['name' => 'endpoints#handlePath', 'url' => '/api/endpoint/{_path}', 'verb' => 'GET', 'requirements' => ['_path' => '.+']],
// ['name' => 'endpoints#handlePath', 'url' => '/api/endpoint/{path}', 'verb' => 'PUT', 'requirements' => ['path' => '.+']],
// ['name' => 'endpoints#handlePath', 'url' => '/api/endpoint/{path}', 'verb' => 'POST', 'requirements' => ['path' => '.+']],
// ['name' => 'endpoints#handlePath', 'url' => '/api/endpoint/{path}', 'verb' => 'DELETE', 'requirements' => ['path' => '.+']],
['name' => 'endpoints#handlePath', 'postfix' => 'read', 'url' => '/api/endpoint/{_path}', 'verb' => 'GET', 'requirements' => ['_path' => '.+']],
['name' => 'endpoints#handlePath', 'postfix' => 'update', 'url' => '/api/endpoint/{_path}', 'verb' => 'PUT', 'requirements' => ['_path' => '.+']],
['name' => 'endpoints#handlePath', 'postfix' => 'create', 'url' => '/api/endpoint/{_path}', 'verb' => 'POST', 'requirements' => ['_path' => '.+']],
['name' => 'endpoints#handlePath', 'postfix' => 'destroy', 'url' => '/api/endpoint/{_path}', 'verb' => 'DELETE', 'requirements' => ['_path' => '.+']],
],
];
7 changes: 7 additions & 0 deletions lib/Db/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Endpoint extends Entity implements JsonSerializable
protected array $conditions = [];
protected ?DateTime $created = null;
protected ?DateTime $updated = null;
protected ?string $inputMapping = null;
protected ?string $outputMapping = null;

public function __construct() {
$this->addType(fieldName:'uuid', type: 'string');
Expand All @@ -39,6 +41,9 @@ public function __construct() {
$this->addType(fieldName:'conditions', type: 'json');
$this->addType(fieldName:'created', type: 'datetime');
$this->addType(fieldName:'updated', type: 'datetime');
$this->addType(fieldName:'inputMapping', type: 'string');
$this->addType(fieldName:'outputMapping', type: 'string');

}

public function getJsonFields(): array
Expand Down Expand Up @@ -89,6 +94,8 @@ public function jsonSerialize(): array
'conditions' => $this->conditions,
'created' => isset($this->created) ? $this->created->format('c') : null,
'updated' => isset($this->updated) ? $this->updated->format('c') : null,
'inputMapping' => $this->inputMapping,
'outputMapping' => $this->outputMapping,

];
}
Expand Down
60 changes: 60 additions & 0 deletions lib/Migration/Version1Date20241230141628.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\OpenConnector\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* FIXME Auto-generated migration step: Please modify to your needs!
*/
class Version1Date20241230141628 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/**
* @var ISchemaWrapper $schema
*/
$schema = $schemaClosure();

if($schema->hasTable(tableName: 'openconnector_endpoints') === true) {
$table = $schema->getTable(tableName: 'openconnector_endpoints');
$table->addColumn('input_mapping', Types::STRING)->setNotnull(false)->setDefault(null);
$table->addColumn('output_mapping', Types::STRING)->setNotnull(false)->setDefault(null);
$table->getColumn('conditions')->setDefault('[]');
}

return $schema;
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}
}
8 changes: 8 additions & 0 deletions lib/Service/EndpointService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct(
private readonly CallService $callService,
private readonly LoggerInterface $logger,
private readonly IURLGenerator $urlGenerator,
private readonly MappingService $mappingService,
)
{
}
Expand Down Expand Up @@ -228,10 +229,17 @@ private function handleSchemaRequest(Endpoint $endpoint, IRequest $request, stri
$register = $target[0];
$schema = $target[1];


$mapper = $this->objectService->getMapper(schema: $schema, register: $register);

$parameters = $request->getParams();


if($endpoint->getInputMapping() !== null) {
$inputMapping = $this->mappingService->getMapping($endpoint->getInputMapping());
$parameters = $this->mappingService->executeMapping(mapping: $inputMapping, input: $parameters);
}

$pathParams = $this->getPathParameters($endpoint->getEndpointArray(), $path);

unset($parameters['_route'], $parameters['_path']);
Expand Down
1 change: 0 additions & 1 deletion src/Controller/JobController.php

This file was deleted.

1 change: 0 additions & 1 deletion src/Controller/LogController.php

This file was deleted.

1 change: 0 additions & 1 deletion src/Controller/SourceController.php

This file was deleted.

Loading