Skip to content

Commit

Permalink
First sketch logic on endpoint data
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Dec 18, 2024
1 parent f35e9f3 commit 75760f5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/Service/EndpointService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Adbar\Dot;
use Exception;
use JWadhams\JsonLogic;
use OCA\OpenConnector\Db\SourceMapper;
use OCA\OpenConnector\Service\AuthenticationService;
use OCA\OpenConnector\Service\MappingService;
Expand Down Expand Up @@ -62,6 +63,12 @@ public function __construct(
*/
public function handleRequest(Endpoint $endpoint, IRequest $request, string $path): JSONResponse
{
$errors = $this->checkConditions($endpoint, $request);

if($errors !== []) {
return new JSONResponse(['error' => 'The following parameters are not correctly set', 'fields' => $errors], 400);
}

try {
// Check if endpoint connects to a schema
if ($endpoint->getTargetType() === 'register/schema') {
Expand Down Expand Up @@ -299,6 +306,21 @@ private function getHeaders(array $server, bool $proxyHeaders = false): array
);
}

private function checkConditions(Endpoint $endpoint, IRequest $request): array
{
$conditions = $endpoint->getConditions();
$data['parameters'] = $request->getParams();
$data['headers'] = $this->getHeaders($request->server, true);

$result = JsonLogic::apply(logic: $conditions, data: $data);

if($result === true || $result === []) {
return [];
}

return $result;
}

/**
* Handles requests for source-based endpoints
*
Expand Down

0 comments on commit 75760f5

Please sign in to comment.