Skip to content

Commit

Permalink
References and endpoints for dataset properties and dynamic API routi…
Browse files Browse the repository at this point in the history
…ng (#129)
  • Loading branch information
thierrydallacroce authored and fmizzell committed Jun 7, 2019
1 parent 20045ce commit be785f0
Show file tree
Hide file tree
Showing 26 changed files with 1,596 additions and 911 deletions.
1 change: 1 addition & 0 deletions cypress/integration/home.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ context('Home', () => {
'Public Safety',
'Transportation'
]

for (var key in topics) {
var value = topics[key]
var index = parseInt(key) + 1
Expand Down
60 changes: 3 additions & 57 deletions modules/custom/dkan_api/dkan_api.routing.yml
Original file line number Diff line number Diff line change
@@ -1,57 +1,3 @@
dkan_api.dataset_get_all:
path: '/api/v1/dataset'
methods: [GET]
defaults:
{ _controller: '\Drupal\dkan_api\Controller\Dataset::getAll'}
requirements:
_access: 'TRUE'
dkan_api.dataset_get:
path: '/api/v1/dataset/{uuid}'
methods: [GET]
defaults:
{ _controller: '\Drupal\dkan_api\Controller\Dataset::get'}
requirements:
_access: 'TRUE'
dkan_api.dataset_post:
path: '/api/v1/dataset'
methods: [POST]
defaults:
{ _controller: '\Drupal\dkan_api\Controller\Dataset::post'}
options:
_auth: [ 'basic_auth' ]
requirements:
_permission: 'post put delete datasets through the api'
dkan_api.dataset_put:
path: '/api/v1/dataset/{uuid}'
methods: [PUT]
defaults:
{ _controller: '\Drupal\dkan_api\Controller\Dataset::put'}
options:
_auth: [ 'basic_auth' ]
requirements:
_permission: 'post put delete datasets through the api'
dkan_api.dataset_patch:
path: '/api/v1/dataset/{uuid}'
methods: [PATCH]
defaults:
{ _controller: '\Drupal\dkan_api\Controller\Dataset::patch'}
options:
_auth: [ 'basic_auth' ]
requirements:
_permission: 'post put delete datasets through the api'
dkan_api.dataset_delete:
path: '/api/v1/dataset/{uuid}'
methods: [DELETE]
defaults:
{ _controller: '\Drupal\dkan_api\Controller\Dataset::delete'}
options:
_auth: [ 'basic_auth' ]
requirements:
_permission: 'post put delete datasets through the api'
dkan_api.organization_get:
path: '/api/v1/organization'
methods: [GET]
defaults:
{ _controller: '\Drupal\dkan_api\Controller\Organization::getAll'}
requirements:
_access: 'TRUE'
# Dynamic routes based on dkan_json_property_api's property_list config.
route_callbacks:
- '\Drupal\dkan_api\Routing\RouteProvider::routes'
7 changes: 0 additions & 7 deletions modules/custom/dkan_api/dkan_api.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ services:
class: \Drupal\dkan_api\Storage\DrupalNodeDataset
arguments:
- '@entity_type.manager'
- '@dkan_api.storage.theme_value_referencer'
dkan_api.storage.organization:
class: \Drupal\dkan_api\Storage\Organization
arguments: ['@dkan_api.storage.drupal_node_dataset']
dkan_api.storage.theme_value_referencer:
class: \Drupal\dkan_api\Storage\ThemeValueReferencer
arguments:
- '@entity_type.manager'
- '@uuid'
- '@queue'
69 changes: 54 additions & 15 deletions modules/custom/dkan_api/src/Controller/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class Api.
*
* @package Drupal\dkan_api\Controller
*/
abstract class Api extends ControllerBase {

/**
* Represents the data type passed via the HTTP request url schema_id slug.
*
* @var string
*/
protected $schemaId;

/**
* Drupal service container.
*
Expand All @@ -25,29 +34,34 @@ abstract class Api extends ControllerBase {
protected $dkanFactory;

/**
*
* Api constructor.
*/
public function __construct(ContainerInterface $container) {
$this->container = $container;
$this->dkanFactory = $container->get('dkan.factory');
}

/**
*
* Gets the json schema object.
*/
abstract protected function getJsonSchema();

/**
*
* Gets the storage object.
*/
abstract protected function getStorage();

/**
* Get all.
*
* @param string $schema_id
* The {schema_id} slug from the HTTP request.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The json response.
*/
public function getAll() {
public function getAll($schema_id = 'dataset') {
$this->schemaId = $schema_id;

$datasets = $this->getEngine()
->get();
Expand All @@ -73,10 +87,15 @@ function ($json_string) {
*
* @param string $uuid
* Identifier.
* @param string $schema_id
* The {schema_id} slug from the HTTP request.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse Json response.
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The json response.
*/
public function get($uuid) {
public function get($uuid, $schema_id = 'dataset') {
$this->schemaId = $schema_id;

try {

$data = $this->getEngine()
Expand All @@ -98,9 +117,15 @@ public function get($uuid) {
/**
* Implements POST method.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse Json response
* @param string $schema_id
* The {schema_id} slug from the HTTP request.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The json response.
*/
public function post() {
public function post($schema_id = 'dataset') {
$this->schemaId = $schema_id;

/* @var $engine \Sae\Sae */
$engine = $this->getEngine();

Expand Down Expand Up @@ -142,10 +167,15 @@ public function post() {
*
* @param string $uuid
* Identifier.
* @param string $schema_id
* The {schema_id} slug from the HTTP request.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse Json response
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The json response.
*/
public function put($uuid) {
public function put($uuid, $schema_id = 'dataset') {
$this->schemaId = $schema_id;

/* @var $engine \Sae\Sae */
$engine = $this->getEngine();

Expand Down Expand Up @@ -184,10 +214,15 @@ public function put($uuid) {
*
* @param string $uuid
* Identifier.
* @param string $schema_id
* The {schema_id} slug from the HTTP request.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse Json response
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The json response.
*/
public function patch($uuid) {
public function patch($uuid, $schema_id = 'dataset') {
$this->schemaId = $schema_id;

/* @var $engine \Sae\Sae */
$engine = $this->getEngine();

Expand Down Expand Up @@ -229,10 +264,13 @@ public function patch($uuid) {
*
* @param string $uuid
* Identifier.
* @param string $schema_id
* The {schema_id} slug from the HTTP request.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse Json response
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The json response.
*/
public function delete($uuid) {
public function delete($uuid, $schema_id = 'dataset') {
/* @var $engine \Sae\Sae */
$engine = $this->getEngine();

Expand All @@ -242,9 +280,10 @@ public function delete($uuid) {
}

/**
* Get isntance of.
* Get SAE instance.
*
* @return \Sae\Sae
* Service Api Engine
*/
public function getEngine() {

Expand Down
10 changes: 10 additions & 0 deletions modules/custom/dkan_api/src/Controller/Dataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,26 @@ public function __construct(ContainerInterface $container) {
* @return \Drupal\dkan_api\Storage\DrupalNodeDataset Dataset
*/
protected function getStorage() {
$this->nodeDataset->setSchema('dataset');
return $this->nodeDataset;
}

/**
* Get Json Schema.
*
* @return string
* Json schema.
*/
protected function getJsonSchema() {

// @Todo: mechanism to validate against additional schemas. For now,
// validate against the empty object, as it accepts any valid json.
if (isset($this->schemaId) && $this->schemaId != 'dataset') {
// @codeCoverageIgnoreStart
return '{ }';
// @codeCoverageIgnoreEnd
}

/** @var \Drupal\dkan_schema\SchemaRetriever $retriever */
$retriever = $this->container
->get('dkan_schema.schema_retriever');
Expand Down
93 changes: 93 additions & 0 deletions modules/custom/dkan_api/src/Routing/RouteProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Drupal\dkan_api\Routing;

use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
*
*/
class RouteProvider {

/**
* @return array
* list of json properties being considered from DKAN json property api
* config value.
*
* @Todo: consolidate with dkan_data ValueReferencer's getPropertyList.
*/
public function getPropertyList() {
$list = \Drupal::config('dkan_data.settings')->get('property_list');
if ($list) {
// Trim and split list on newlines whether Windows, MacOS or Linux.
return preg_split(
'/\s*\r\n\s*|\s*\r\s*|\s*\n\s*/',
trim($list),
-1,
PREG_SPLIT_NO_EMPTY
);
}
else {
return [];
}
}

/**
* {@inheritdoc}
*/
public function routes() {
$routes = new RouteCollection();
$public_routes = new RouteCollection();
$authenticated_routes = new RouteCollection();
$schemas = array_merge(['dataset'], $this->getPropertyList());

foreach ($schemas as $schema) {
// GET collection.
$get_all = $this->routeHelper($schema, "/api/v1/$schema", 'GET', 'getAll');
$public_routes->add("dkan_api.{$schema}.get_all", $get_all);
// GET individual.
$get = $this->routeHelper($schema, "/api/v1/$schema/{uuid}", 'GET', 'get');
$public_routes->add("dkan_api.{$schema}.get", $get);
// POST.
$post = $this->routeHelper($schema, "/api/v1/$schema", 'POST', 'post');
$authenticated_routes->add("dkan_api.{$schema}.post", $post);
// PUT.
$put = $this->routeHelper($schema, "/api/v1/$schema/{uuid}", 'PUT', 'put');
$authenticated_routes->add("dkan_api.{$schema}.put", $put);
// PATCH.
$patch = $this->routeHelper($schema, "/api/v1/$schema/{uuid}", 'PATCH', 'patch');
$authenticated_routes->add("dkan_api.{$schema}.patch", $patch);
// DELETE.
$delete = $this->routeHelper($schema, "/api/v1/$schema/{uuid}", 'DELETE', 'delete');
$authenticated_routes->add("dkan_api.{$schema}.delete", $delete);
}

$public_routes->addRequirements(['_access' => 'TRUE']);
$authenticated_routes->addRequirements(['_permission' => 'post put delete datasets through the api']);
$authenticated_routes->addOptions(['_auth' => ['basic_auth']]);
$routes->addCollection($public_routes);
$routes->addCollection($authenticated_routes);

return $routes;
}

/**
* @param string $path
* @param string $datasetMethod
* @param string $httpVerb
* @return Route
*/
protected function routeHelper(string $schema, string $path, string $httpVerb, string $datasetMethod) : Route {
$route = new Route(
$path,
[
'_controller' => '\Drupal\dkan_api\Controller\Dataset::' . $datasetMethod,
'schema_id' => $schema,
]
);
$route->setMethods([$httpVerb]);
return $route;
}

}
Loading

0 comments on commit be785f0

Please sign in to comment.