Skip to content

Commit

Permalink
Working get endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Dec 17, 2024
1 parent b8f6895 commit b624417
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 200 deletions.
9 changes: 4 additions & 5 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
'Mappings' => ['url' => 'api/mappings'],
'Jobs' => ['url' => 'api/jobs'],
'Synchronizations' => ['url' => 'api/synchronizations'],
'Endpoints' => ['url' => 'api/endpoints'],
'Consumers' => ['url' => 'api/consumers'],
],
'routes' => [
Expand All @@ -26,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', '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' => '.+']],
],
];
13 changes: 7 additions & 6 deletions lib/Controller/EndpointsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function page(): TemplateResponse
*/
public function index(ObjectService $objectService, SearchService $searchService): JSONResponse
{

$filters = $this->request->getParams();
$fieldsToSearch = ['name', 'description', 'endpoint'];

Expand Down Expand Up @@ -178,28 +179,28 @@ public function destroy(int $id): JSONResponse

/**
* Handles generic path requests by matching against registered endpoints
*
*
* This method checks if the current path matches any registered endpoint patterns
* and forwards the request to the appropriate endpoint service if found
*
* @NoAdminRequired
* @NoCSRFRequired
*
*
* @param string $path The request path to match
* @return JSONResponse The response from the endpoint service or 404 if no match
*/
public function handlePath(string $path): JSONResponse
public function handlePath(string $_path): JSONResponse
{
// Find matching endpoints for the given path and method
$matchingEndpoints = $this->endpointMapper->findByPathRegex(
path: $path,
path: $_path,
method: $this->request->getMethod()
);

// If no matching endpoints found, return 404
if (empty($matchingEndpoints)) {
return new JSONResponse(
data: ['error' => 'No matching endpoint found for path and method: ' . $path . ' ' . $this->request->getMethod()],
data: ['error' => 'No matching endpoint found for path and method: ' . $_path . ' ' . $this->request->getMethod()],
statusCode: 404
);
}
Expand All @@ -208,7 +209,7 @@ public function handlePath(string $path): JSONResponse
$endpoint = reset($matchingEndpoints);

// Forward the request to the endpoint service
return $this->endpointService->handleRequest($endpoint, $this->request);
return $this->endpointService->handleRequest($endpoint, $this->request, $_path);
}

}
53 changes: 28 additions & 25 deletions lib/Db/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,37 @@

class Endpoint extends Entity implements JsonSerializable
{
protected ?string $uuid = null;
protected ?string $name = null; // The name of the endpoint
protected ?string $description = null; // The description of the endpoint
protected ?string $reference = null; // The reference of the endpoint
protected ?string $version = '0.0.0'; // The version of the endpoint
protected ?string $endpoint = null; // The actual endpoint e.g /api/buildings/{{id}}. An endpoint may contain parameters e.g {{id}}
protected ?array $endpointArray = null; // An array representation of the endpoint. Automatically generated
protected ?string $endpointRegex = null; // A regex representation of the endpoint. Automatically generated
protected ?string $method = null; // One of GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD. method and endpoint combination should be unique
protected ?string $targetType = null; // The target to attach this endpoint to, should be one of source (to create a proxy endpoint) or register/schema (to create an object endpoint) or job (to fire an event) or synchronization (to create a synchronization endpoint)
protected ?string $targetId = null; // The target id to attach this endpoint to
protected ?string $uuid = null;
protected ?string $name = null; // The name of the endpoint
protected ?string $description = null; // The description of the endpoint
protected ?string $reference = null; // The reference of the endpoint
protected ?string $version = '0.0.0'; // The version of the endpoint
protected ?string $endpoint = null; // The actual endpoint e.g /api/buildings/{{id}}. An endpoint may contain parameters e.g {{id}}
protected ?array $endpointArray = null; // An array representation of the endpoint. Automatically generated
protected ?string $endpointRegex = null; // A regex representation of the endpoint. Automatically generated
protected ?string $method = null; // One of GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD. method and endpoint combination should be unique
protected ?string $targetType = null; // The target to attach this endpoint to, should be one of source (to create a proxy endpoint) or register/schema (to create an object endpoint) or job (to fire an event) or synchronization (to create a synchronization endpoint)
protected ?string $targetId = null; // The target id to attach this endpoint to
protected ?DateTime $created = null;
protected ?DateTime $updated = null;

public function __construct() {
$this->addType('uuid', 'string');
$this->addType('name', 'string');
$this->addType('description', 'string');
$this->addType('reference', 'string');
$this->addType('version', 'string');
$this->addType('endpoint', 'string');
$this->addType('endpointArray', 'json');
$this->addType('endpointRegex', 'string');
$this->addType('method', 'string');
$this->addType('targetType', 'string');
$this->addType('targetId', 'string');
$this->addType('created', 'datetime');
$this->addType('updated', 'datetime');
$this->addType(fieldName:'uuid', type: 'string');
$this->addType(fieldName:'name', type: 'string');
$this->addType(fieldName:'description', type: 'string');
$this->addType(fieldName:'reference', type: 'string');
$this->addType(fieldName:'version', type: 'string');
$this->addType(fieldName:'endpoint', type: 'string');
$this->addType(fieldName:'endpointArray', type: 'json');
$this->addType(fieldName:'endpointRegex', type: 'string');
$this->addType(fieldName:'method', type: 'string');
$this->addType(fieldName:'targetType', type: 'string');
$this->addType(fieldName:'targetId', type: 'string');
$this->addType(fieldName:'schema', type: 'int');
$this->addType(fieldName:'register', type: 'int');
$this->addType(fieldName:'source', type: 'int');
$this->addType(fieldName:'created', type: 'datetime');
$this->addType(fieldName:'updated', type: 'datetime');
}

public function getJsonFields(): array
Expand Down Expand Up @@ -85,7 +88,7 @@ public function jsonSerialize(): array
'targetId' => $this->targetId,
'created' => isset($this->created) ? $this->created->format('c') : null,
'updated' => isset($this->updated) ? $this->updated->format('c') : null,

];
}
}
21 changes: 16 additions & 5 deletions lib/Db/EndpointMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
return $this->findEntities(query: $qb);
}

private function createEndpointRegex(string $endpoint) {
return '#'.preg_replace(pattern: ['#\/{{([^}}]+)}}\/#', '#\/{{([^}}]+)}}$#'], replacement: ['/([^/]+)/', '(/([^/]+))?'], subject: $endpoint).'#';
}

public function createFromArray(array $object): Endpoint
{
$obj = new Endpoint();
Expand All @@ -69,6 +73,10 @@ public function createFromArray(array $object): Endpoint
if ($obj->getUuid() === null){
$obj->setUuid(Uuid::v4());
}

$obj->setEndpointRegex($this->createEndpointRegex($obj->getEndpoint()));
$obj->setEndpointArray(explode('/', $obj->getEndpoint()));

return $this->insert(entity: $obj);
}

Expand All @@ -82,6 +90,9 @@ public function updateFromArray(int $id, array $object): Endpoint
$version[2] = (int)$version[2] + 1;
$obj->setVersion(implode('.', $version));

$obj->setEndpointRegex($this->createEndpointRegex($obj->getEndpoint()));
$obj->setEndpointArray(explode('/', $obj->getEndpoint()));

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

Expand Down Expand Up @@ -112,23 +123,23 @@ public function getTotalCallCount(): int
* @param string $method The HTTP method to filter by (GET, POST, etc)
* @return array Array of matching Endpoint entities
*/
public function findByPathRegex(string $path, string $method): array
public function findByPathRegex(string $path, string $method): array
{
// Get all endpoints first since we need to do regex comparison
$endpoints = $this->findAll();

// Filter endpoints where both path matches regex pattern and method matches
return array_filter($endpoints, function(Endpoint $endpoint) use ($path, $method) {
// Get the regex pattern from the endpoint
$pattern = $endpoint->getEndpointRegex();

// Skip if no regex pattern is set
if (empty($pattern)) {
return false;
}

// Check if both path matches the regex pattern and method matches
return preg_match($pattern, $path) === 1 &&
return preg_match($pattern, $path) === 1 &&
$endpoint->getMethod() === $method;
});
}
Expand Down
134 changes: 0 additions & 134 deletions lib/Service/ConsumertService.php

This file was deleted.

Loading

0 comments on commit b624417

Please sign in to comment.