Skip to content

Commit

Permalink
Merge pull request #165 from ConductionNL/feature/CONNECTOR-136/up-do…
Browse files Browse the repository at this point in the history
…wnload-endpoints

Import & export for OpenConnector
  • Loading branch information
WilcoLouwerse authored Jan 10, 2025
2 parents a6488f9 + 121188e commit 34e3df6
Show file tree
Hide file tree
Showing 17 changed files with 923 additions and 39 deletions.
4 changes: 4 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@
['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' => '.+']],
// Import & Export
['name' => 'import#import', 'url' => '/api/import', 'verb' => 'POST'],
// ['name' => 'import#importUpdate', 'url' => '/api/import/{id}', 'verb' => 'PUT'],
['name' => 'export#export', 'url' => '/api/export/{type}/{id}', 'verb' => 'GET'],
],
];
52 changes: 52 additions & 0 deletions lib/Controller/ExportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace OCA\OpenConnector\Controller;

use OCA\OpenConnector\Service\ExportService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;

class ExportController extends Controller
{
/**
* Constructor for the ExportController
*
* @param string $appName The name of the app
* @param IRequest $request The request object
* @param IAppConfig $config The app configuration object
* @param ExportService $exportService The Export Service.
*/
public function __construct(
$appName,
IRequest $request,
private IAppConfig $config,
private readonly ExportService $exportService
)
{
parent::__construct($appName, $request);
}

/**
* Creates and return a json file for a specific object.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $type The object type we want to export an object for.
* @param string $id The id used to find an existing object to export.
*
* @return JSONResponse
*/
public function export(string $type, string $id): JSONResponse
{
$accept = $this->request->getHeader(name: 'Accept');

if (empty($accept) === true) {
return new JSONResponse(data: ['error' => 'Request is missing header Accept'], statusCode: 400);
}

return $this->exportService->export(objectType: $type, id: $id, accept: $accept);
}
}
70 changes: 70 additions & 0 deletions lib/Controller/ImportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace OCA\OpenConnector\Controller;

use GuzzleHttp\Exception\GuzzleException;
use OCA\OpenConnector\Service\ImportService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;

class ImportController extends Controller
{
/**
* Constructor for the ImportController
*
* @param string $appName The name of the app
* @param IRequest $request The request object
* @param IAppConfig $config The app configuration object
* @param ImportService $importService The Import Service.
*/
public function __construct(
$appName,
IRequest $request,
private IAppConfig $config,
private readonly ImportService $importService
)
{
parent::__construct($appName, $request);
}

/**
* Creates a new object or updates an existing one using a json text/string as input.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
* @throws GuzzleException
*/
public function import(): JSONResponse
{
$data = $this->request->getParams();
$uploadedFiles = [];

// Check if multiple files have been uploaded.
$files = $_FILES['files'] ?? null;

if (empty($files) === false) {
// Loop through each file using the count of 'name'
for ($i = 0; $i < count($files['name']); $i++) {
$uploadedFiles[] = [
'name' => $files['name'][$i],
'type' => $files['type'][$i],
'tmp_name' => $files['tmp_name'][$i],
'error' => $files['error'][$i],
'size' => $files['size'][$i]
];
}
}

// Get the uploaded file from the request if a single file hase been uploaded.
$uploadedFile = $this->request->getUploadedFile(key: 'file');
if (empty($uploadedFile) === false) {
$uploadedFiles[] = $uploadedFile;
}

return $this->importService->import(data: $data, uploadedFiles: $uploadedFiles);
}
}
19 changes: 11 additions & 8 deletions lib/Db/ConsumerMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(IDBConnection $db)
}

/**
* Find a Consumer by its ID
* Find a Consumer by its ID.
*
* @param int $id The ID of the Consumer
* @return Consumer The found Consumer entity
Expand All @@ -49,7 +49,7 @@ public function find(int $id): Consumer
}

/**
* Find all Consumers with optional filtering and pagination
* Find all Consumers with optional filtering and pagination.
*
* @param int|null $limit Maximum number of results to return
* @param int|null $offset Number of results to skip
Expand Down Expand Up @@ -88,7 +88,7 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
}

/**
* Create a new Consumer from an array of data
* Create a new Consumer from an array of data.
*
* @param array $object An array of Consumer data
* @return Consumer The newly created Consumer entity
Expand All @@ -105,7 +105,7 @@ public function createFromArray(array $object): Consumer
}

/**
* Update an existing Consumer from an array of data
* Update an existing Consumer from an array of data.
*
* @param int $id The ID of the Consumer to update
* @param array $object An array of updated Consumer data
Expand All @@ -116,10 +116,13 @@ public function updateFromArray(int $id, array $object): Consumer
$obj = $this->find($id);
$obj->hydrate($object);

// Set or update the version
// $version = explode('.', $obj->getVersion());
// $version[2] = (int)$version[2] + 1;
// $obj->setVersion(implode('.', $version));
// @todo: does Consumer need a version? $version field does currently not exist.
// if (isset($object['version']) === false) {
// // Set or update the version
// $version = explode('.', $obj->getVersion());
// $version[2] = (int)$version[2] + 1;
// $obj->setVersion(implode('.', $version));
// }

return $this->update($obj);
}
Expand Down
23 changes: 19 additions & 4 deletions lib/Db/EndpointMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public function find(int $id): Endpoint
return $this->findEntity(query: $qb);
}

public function findByRef(string $reference): array
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_endpoints')
->where(
$qb->expr()->eq('reference', $qb->createNamedParameter($reference))
);

return $this->findEntities(query: $qb);
}

public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
{
$qb = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -85,10 +98,12 @@ public function updateFromArray(int $id, array $object): Endpoint
$obj = $this->find($id);
$obj->hydrate($object);

// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
if (isset($object['version']) === false) {
// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
}

$obj->setEndpointRegex($this->createEndpointRegex($obj->getEndpoint()));
$obj->setEndpointArray(explode('/', $obj->getEndpoint()));
Expand Down
5 changes: 4 additions & 1 deletion lib/Db/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Job extends Entity implements JsonSerializable
protected ?string $uuid = null;
protected ?string $name = null;
protected ?string $description = null;
protected ?string $version = '0.0.0'; // The version of the endpoint
protected ?string $reference = null; // The reference of the Job
protected ?string $version = '0.0.0'; // The version of the Job
protected ?string $jobClass = 'OCA\OpenConnector\Action\PingAction';
protected ?array $arguments = null;
protected ?int $interval = 3600; // seconds in an hour
Expand All @@ -34,6 +35,7 @@ public function __construct() {
$this->addType('uuid', 'string');
$this->addType('name', 'string');
$this->addType('description', 'string');
$this->addType(fieldName:'reference', type: 'string');
$this->addType('version', 'string');
$this->addType('jobClass', 'string');
$this->addType('arguments', 'json');
Expand Down Expand Up @@ -91,6 +93,7 @@ public function jsonSerialize(): array
'uuid' => $this->uuid,
'name' => $this->name,
'description' => $this->description,
'reference' => $this->reference,
'version' => $this->version,
'jobClass' => $this->jobClass,
'arguments' => $this->arguments,
Expand Down
23 changes: 19 additions & 4 deletions lib/Db/JobMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public function find(int $id): Job
return $this->findEntity(query: $qb);
}

public function findByRef(string $reference): array
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_jobs')
->where(
$qb->expr()->eq('reference', $qb->createNamedParameter($reference))
);

return $this->findEntities(query: $qb);
}

public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
{
$qb = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -74,10 +87,12 @@ public function updateFromArray(int $id, array $object): Job
$obj = $this->find($id);
$obj->hydrate($object);

// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
if (isset($object['version']) === false) {
// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
}

return $this->update($obj);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Mapping extends Entity implements JsonSerializable
{
protected ?string $uuid = null;
protected ?string $reference = null;
protected ?string $version = null;
protected ?string $version = '0.0.0';
protected ?string $name = null;
protected ?string $description = null;
protected ?array $mapping = null;
Expand Down
20 changes: 16 additions & 4 deletions lib/Db/MappingMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public function find(int $id): Mapping
return $this->findEntity(query: $qb);
}

public function findByRef(string $reference): array
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_mappings')
->where(
$qb->expr()->eq('reference', $qb->createNamedParameter($reference))
);

return $this->findEntities(query: $qb);
}

public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
{
$qb = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -73,17 +86,16 @@ public function updateFromArray(int $id, array $object): Mapping
{
$obj = $this->find($id);
$obj->hydrate($object);
// Set or update the version
if ($obj->getVersion() !== null) {

if ($obj->getVersion() !== null && isset($object['version']) === false) {
// Set or update the version
$version = explode('.', $obj->getVersion());
if (isset($version[2]) === true) {
$version[2] = (int) $version[2] + 1;
$obj->setVersion(implode('.', $version));
}
}


return $this->update($obj);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Db/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Source extends Entity implements JsonSerializable
protected ?string $name = null;
protected ?string $description = null;
protected ?string $reference = null;
protected ?string $version = null;
protected ?string $version = '0.0.0';
protected ?string $location = null;
protected ?bool $isEnabled = null;
protected ?string $type = null;
Expand Down
23 changes: 19 additions & 4 deletions lib/Db/SourceMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public function find(int $id): Source
return $this->findEntity(query: $qb);
}

public function findByRef(string $reference): array
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('openconnector_sources')
->where(
$qb->expr()->eq('reference', $qb->createNamedParameter($reference))
);

return $this->findEntities(query: $qb);
}

public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
{
$qb = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -74,10 +87,12 @@ public function updateFromArray(int $id, array $object): Source
$obj = $this->find($id);
$obj->hydrate($object);

// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
if (isset($object['version']) === false) {
// Set or update the version
$version = explode('.', $obj->getVersion());
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));
}

return $this->update($obj);
}
Expand Down
Loading

0 comments on commit 34e3df6

Please sign in to comment.