API support, serving JSON formatted data

Paged api endpoints accept "page" and "pagesize" params.
New config file config/settings/open_data.global.php
ACL is dynamically manipulated by the module.
The "index" endpoint provides links to available endpoints.
Documentation is provided in swagger.
Add doc links to open data page #14
parent 9a4e9ac8
......@@ -6,21 +6,20 @@
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
return [
'acl' => [
'default_role' => 'guest',
'roles' => [
'guest' => [],
'user' => [],
'roles' => [
'guest' => [],
'user' => [],
'school' => ['user'],
'admin' => ['user'],
'admin' => ['user'],
],
'resoures' => [],
'guards' => [
'guards' => [
'resources' => [],
'callables' => [],
'routes' => [
'routes' => [
['/', ['guest', 'user'], ['get']],
['/about', ['guest', 'user'], ['get']],
['/school', ['school'], ['get']],
......
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
return [
'open_data' => [
'pagesize' => 20, // default page size
'maxpagesize' => 200, // maximum page size
],
];
This diff is collapsed.
This diff is collapsed.
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\Action;
/**
* @inheritdoc
*/
class AllSchools extends PagedApiAction
{
}
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\Action;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use GrEduLabs\OpenData\Service\DataProviderInterface;
use GrEduLabs\OpenData\Service\RedBeanQueryPagedDataProvider;
/**
* Base class implementaing the api action.
* A dataprovider object is used to retrieve data.
*
* @see GrEduLabs\OpenData\Service\DataProviderInterface
*/
class ApiAction
{
/**
* @var GrEduLabs\OpenData\Service\DataProviderInterface
*/
protected $dataProvider;
/**
* @var Slim\Container
*/
protected $container;
/**
* @var boolean Respond with a 404 instead of 200 if data from dataprovider is an empty array.
*/
protected $empty_data_404;
public function __construct(Container $container, DataProviderInterface $dataProvider, $empty_data_404 = false)
{
$this->container = $container;
$this->dataProvider = $dataProvider;
$this->empty_data_404 = $empty_data_404;
}
public function setEmptyData404()
{
$this->empty_data_404 = true;
}
public function setEmptyData200()
{
$this->empty_data_404 = false;
}
public function __invoke(Request $req, Response $res, array $args = [])
{
/**
* Get data from dataprovider.
* If data is null, respond with a 500 status.
* If data is empty, respond with a 404 when $empty_data_404 is true.
* In any other case respond with a 200 status.
*
* @see GrEduLabs\OpenData\Service\DataProviderInterface
*/
$data = $this->dataProvider->getData();
$status = 200;
if ($data === null) {
$status = 500;
$data = ['message' => 'An error occured while retrieving data'];
} elseif (!is_array($data)) {
$status = 500;
$data = ['message' => 'Unexpected data: ' . var_export($data, true)];
} elseif (count($data) === 0) {
$status = (($this->empty_data_404 === true) ? 404 : 200);
}
return $this->respond($res, 'JSON', $this->prepareResponseData($status, $data), $status);
}
/**
*
* @param int $status HTTP status code
* @param type $data the real data to wrap in response
* @return array An array with predetermined keys:
* - status HTTP status code
* - success boolean
* - data The actual data
* - labels Data labels
* - count Data count in resultset
* - countall Data count without paging
* - page Data current page
* - pages Data number of pages available
*/
protected function prepareResponseData($status, $data = null)
{
$is_success = ($status == 200);
$has_data = $is_success && isset($data);
$has_paging = $this->dataProvider instanceof RedBeanQueryPagedDataProvider && $has_data;
$response_data = [
'status' => $status,
'success' => $is_success,
'count' => ($has_data ? $this->dataProvider->getCount() : null),
'countall' => ($has_paging ? $this->dataProvider->getCountAll() : null),
'page' => ($has_paging ? $this->dataProvider->getPage() : null),
'pages' => ($has_paging ? $this->dataProvider->getPages() : null),
'labels' => ($has_data ? $this->dataProvider->getLabels() : null),
'data' => $data,
];
return $response_data;
}
/**
* Send the final response. Only Json format is currently supported.
*
* @param string $outputFormat i.e. JSON
* @param array $response_data typically an array to send ot data to the client
* @param int $status HTTP status code
* @return mixed response object
*/
protected function respond(Response $res, $outputFormat, array $response_data, $status)
{
switch ($outputFormat) {
case 'JSON':
default:
return $res->withJson($response_data, $status, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
}
}
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\Action;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use GrEduLabs\OpenData\Service\DataProviderInterface;
use GrEduLabs\OpenData\InputFilter\EduadminNameInputFilter;
use GrEduLabs\OpenData\InputFilter\RegioneduadminNameInputFilter;
/**
* @inheritdoc
*/
class EduadminFilteredPagedApiAction extends PagedApiAction
{
/**
* @var InputFilter for eduadmin name
*/
private $_eduadminInputFilter;
/**
* @var InputFilter for region eduadmin name
*/
private $_regioneduadminInputFilter;
public function __construct(Container $container, DataProviderInterface $dataProvider, $empty_data_404 = false)
{
parent::__construct($container, $dataProvider, $empty_data_404);
$this->_eduadminInputFilter = new EduadminNameInputFilter();
$this->_regioneduadminInputFilter = new RegioneduadminNameInputFilter();
}
public function __invoke(Request $req, Response $res, array $args = [])
{
$this->_eduadminInputFilter->setData([
'name' => (isset($args['eduadmin']) ? $args['eduadmin'] : null)
]);
$this->_regioneduadminInputFilter->setData([
'name' => (isset($args['regioneduadmin']) ? $args['regioneduadmin'] : null)
]);
if ($this->_eduadminInputFilter->isValid() &&
$this->_regioneduadminInputFilter->isValid()) {
$this->dataProvider->queryFilter('eduadmin.name', $this->_eduadminInputFilter->getValue('name'), 'LIKE');
$this->dataProvider->queryFilter('regioneduadmin.name', $this->_regioneduadminInputFilter->getValue('name'), 'LIKE');
return parent::__invoke($req, $res, $args);
} else {
$messages = array_merge($this->_eduadminInputFilter->getMessages(), $this->_regioneduadminInputFilter->getMessages());
$responseData = $this->prepareResponseData(400, [
'errors' => array_reduce(array_keys($messages), function ($m, $k) use ($messages) {
$m[$k] = array_values($messages[$k]);
return $m;
}, [])
]);
return $this->respond($res, 'JSON', $responseData, 400);
}
}
}
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\Action;
//use Slim\Container;
//use Slim\Http\Request;
//use Slim\Http\Response;
//use GrEduLabs\OpenData\Service\DataProviderInterface;
/**
* @inheritdoc
*/
class Index extends ApiAction
{
}
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\Action;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use GrEduLabs\OpenData\Service\DataProviderInterface;
use GrEduLabs\OpenData\InputFilter\GroupFlagInputFilter;
/**
* @inheritdoc
*/
class ItemCategoryNames extends ApiAction
{
/**
* @var InputFilter
*/
private $inputFilter;
public function __construct(Container $container, DataProviderInterface $dataProvider, $empty_data_404 = false)
{
parent::__construct($container, $dataProvider, $empty_data_404);
$this->inputFilter = new GroupFlagInputFilter();
}
public function __invoke(Request $req, Response $res, array $args = [])
{
$this->inputFilter->setData([
'group' => $req->getParam('group', null),
]);
if ($this->inputFilter->isValid()) {
$this->dataProvider->filterGroupflag($this->inputFilter->getValue('group'));
return parent::__invoke($req, $res, $args);
} else {
$messages = $this->inputFilter->getMessages();
$responseData = $this->prepareResponseData(400, [
'errors' => array_reduce(array_keys($messages), function ($m, $k) use ($messages) {
$m[$k] = array_values($messages[$k]);
return $m;
}, [])
]);
return $this->respond($res, 'JSON', $responseData, 400);
}
}
}
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\Action;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use GrEduLabs\OpenData\Service\DataProviderInterface;
use GrEduLabs\OpenData\Action\EduadminFilteredPagedApiAction;
use GrEduLabs\OpenData\InputFilter\PrefectureNameInputFilter;
use GrEduLabs\OpenData\InputFilter\MunicipalityNameInputFilter;
/**
* @inheritdoc
*/
class MunicipalityPrefectureEduadminFilteredPagedApiAction extends EduadminFilteredPagedApiAction
{
/**
* @var InputFilter for prefecture name
*/
private $_prefecturesInputFilter;
/**
* @var InputFilter for municipality name
*/
private $_municipalityInputFilter;
public function __construct(Container $container, DataProviderInterface $dataProvider, $empty_data_404 = false)
{
parent::__construct($container, $dataProvider, $empty_data_404);
$this->_prefecturesInputFilter = new PrefectureNameInputFilter();
$this->_municipalityInputFilter = new MunicipalityNameInputFilter();
}
public function __invoke(Request $req, Response $res, array $args = [])
{
$this->_prefecturesInputFilter->setData([
'name' => (isset($args['prefecture']) ? $args['prefecture'] : null)
]);
$this->_municipalityInputFilter->setData([
'name' => (isset($args['municipality']) ? $args['municipality'] : null)
]);
if ($this->_prefecturesInputFilter->isValid() &&
$this->_municipalityInputFilter->isValid()) {
$this->dataProvider->queryFilter('prefecture.name', $this->_prefecturesInputFilter->getValue('name'));
$this->dataProvider->queryFilter('school.municipality', $this->_municipalityInputFilter->getValue('name'));
return parent::__invoke($req, $res, $args);
} else {
$messages = array_merge($this->_prefecturesInputFilter->getMessages(), $this->_municipalityInputFilter->getMessages());
$responseData = $this->prepareResponseData(400, [
'errors' => array_reduce(array_keys($messages), function ($m, $k) use ($messages) {
$m[$k] = array_values($messages[$k]);
return $m;
}, [])
]);
return $this->respond($res, 'JSON', $responseData, 400);
}
}
}
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\Action;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use GrEduLabs\OpenData\Service\DataProviderInterface;
use GrEduLabs\OpenData\InputFilter\PagerInputFilter;
/**
* @inheritdoc
*/
class PagedApiAction extends ApiAction
{
/**
* @var InputFilter
*/
private $inputFilter;
/**
* @var array Settings of open_data module; MUST be defined
*/
private $open_data_settings;
public function __construct(Container $container, DataProviderInterface $dataProvider, $empty_data_404 = false)
{
parent::__construct($container, $dataProvider, $empty_data_404);
$this->open_data_settings = $container['settings']['open_data'];
$this->inputFilter = new PagerInputFilter($this->open_data_settings['maxpagesize']);
}
public function __invoke(Request $req, Response $res, array $args = [])
{
$this->inputFilter->setData([
'page' => $req->getParam('page', 1),
'pagesize' => $req->getParam('pagesize', $this->open_data_settings['pagesize'])
]);
if ($this->inputFilter->isValid()) {
$this->dataProvider->setPage($this->inputFilter->getValue('page'));
$this->dataProvider->setPagesize($this->inputFilter->getValue('pagesize'));
return parent::__invoke($req, $res, $args);
} else {
$messages = $this->inputFilter->getMessages();
$responseData = $this->prepareResponseData(400, [
'errors' => array_reduce(array_keys($messages), function ($m, $k) use ($messages) {
$m[$k] = array_values($messages[$k]);
return $m;
}, [])
]);
return $this->respond($res, 'JSON', $responseData, 400);
}
}
}
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\Action;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use GrEduLabs\OpenData\Service\DataProviderInterface;
use GrEduLabs\OpenData\InputFilter\PrefectureNameInputFilter;
/**
* @inheritdoc
*/
class Prefectures extends PagedApiAction
{
/**
* @var InputFilter
*/
private $_prefecturesInputFilter;
public function __construct(Container $container, DataProviderInterface $dataProvider, $empty_data_404 = false)
{
parent::__construct($container, $dataProvider, $empty_data_404);
$this->_prefecturesInputFilter = new PrefectureNameInputFilter();
}
public function __invoke(Request $req, Response $res, array $args = [])
{
$this->_prefecturesInputFilter->setData([
'name' => (isset($args['name']) ? $args['name'] : null)
]);
if ($this->_prefecturesInputFilter->isValid()) {
$this->dataProvider->filterName($this->_prefecturesInputFilter->getValue('name'));
return parent::__invoke($req, $res, $args);
} else {
$messages = $this->_prefecturesInputFilter->getMessages();
$responseData = $this->prepareResponseData(400, [
'errors' => array_reduce(array_keys($messages), function ($m, $k) use ($messages) {
$m[$k] = array_values($messages[$k]);
return $m;
}, [])
]);
return $this->respond($res, 'JSON', $responseData, 400);
}
}
}
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\Action;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use GrEduLabs\OpenData\Service\DataProviderInterface;
/**
* @inheritdoc
*/
class RegistryEduadminFilteredPagedApiAction extends EduadminFilteredPagedApiAction
{
public function __construct(Container $container, DataProviderInterface $dataProvider, $empty_data_404 = false)
{
parent::__construct($container, $dataProvider, $empty_data_404);
}
public function __invoke(Request $req, Response $res, array $args = [])
{
$this->dataProvider->queryFilter('school.registry_no', (isset($args['registry_no']) ? $args['registry_no'] : null));
return parent::__invoke($req, $res, $args);
}
}
<?php
/**
* gredu_labs.
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
*
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\Action;
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
use GrEduLabs\OpenData\Service\DataProviderInterface;
use GrEduLabs\OpenData\InputFilter\PrefectureNameInputFilter;
use GrEduLabs\OpenData\InputFilter\EducationLevelNameInputFilter;
/**
* @inheritdoc
*/
class SchoolsFilteredAction extends PagedApiAction
{
/**
* @var InputFilter for prefecture name
*/
private $_prefecturesInputFilter;
/**
* @var InputFilter for education level label
*/
private $_educationLevelInputFilter;
public function __construct(Container $container, DataProviderInterface $dataProvider, $empty_data_404 = false)
{
parent::__construct($container, $dataProvider, $empty_data_404);
$this->_prefecturesInputFilter = new PrefectureNameInputFilter();
$this->_educationLevelInputFilter = new EducationLevelNameInputFilter();
}
public function __invoke(Request $req, Response $res, array $args = [])
{
$this->_prefecturesInputFilter->setData([
'name' => (isset($args['prefecture']) ? $args['prefecture'] : null)
]);
$this->_educationLevelInputFilter->setData([
'name' => (isset($args['education_level']) ? $args['education_level'] : null)
]);
if ($this->_prefecturesInputFilter->isValid() &&
$this->_educationLevelInputFilter->isValid()) {
$this->dataProvider->queryFilter('prefecture.name', $this->_prefecturesInputFilter->getValue('name'));
$this->dataProvider->queryFilter('educationlevel.name', $this->_educationLevelInputFilter->getValue('name'));
return parent::__invoke($req, $res, $args);
} else {
$messages = array_merge($this->_prefecturesInputFilter->getMessages(), $this->_educationLevelInputFilter->getMessages());
$responseData = $this->prepareResponseData(400, [
'errors' => array_reduce(array_keys($messages), function ($m, $k) use ($messages) {
$m[$k] = array_values($messages[$k]);
return $m;
}, [])
]);
return $this->respond($res, 'JSON', $responseData, 400);
}
}
}
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\OpenData\InputFilter;
use Zend\Filter;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\Validator;
use RedBeanPHP\R;
class EduadminNameInputFilter extends InputFilter
{
public function __construct()
{
$eduadminName = new Input('name');
$eduadminName->setRequired(false)
->getFilterChain()
->attach(new Filter\StripNewlines())