Skip to content

Commit

Permalink
[API] added endpoints/project/dicoms.class.inc that was left behind
Browse files Browse the repository at this point in the history
  • Loading branch information
spell00 committed Nov 4, 2020
1 parent a50d83c commit 3c7c1e6
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 1 deletion.
148 changes: 148 additions & 0 deletions modules/api/php/endpoints/project/dicoms.class.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/**
* This implements the Images page class under Project
*
* PHP Version 7
*
* @category API
* @package Loris
* @author Simon Pelletier <simon.pelletier@mcin.ca>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://github.com/aces/Loris
*/
namespace LORIS\api\Endpoints\Project;

use \Psr\Http\Message\ServerRequestInterface;
use \Psr\Http\Message\ResponseInterface;
use \LORIS\api\Endpoint;

/**
* A class for handling the /projects/$projectname/dicoms endpoint.
*
* @category API
* @package Loris
* @author Simon Pelletier <simon.pelletier@mcin.ca>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://github.com/aces/Loris
*/
class Dicoms extends Endpoint implements \LORIS\Middleware\ETagCalculator
{
/**
* A cache of the results of the endpoint, so that
* it doesn't need to be recalculated for the ETag and handler
*/
private $_cache;

/**
* The requested project
*/
private $_project;

/**
* Contructor
*
* @param \Project $project The requested project
*/
public function __construct(\Project $project)
{
$this->_project = $project;
}

/**
* Return which methods are supported by this endpoint.
*
* @return array supported HTTP methods
*/
protected function allowedMethods() : array
{
return ['GET'];
}

/**
* Versions of the LORIS API which are supported by this
* endpoint.
*
* @return array a list of supported API versions.
*/
protected function supportedVersions() : array
{
return ["v0.0.3"];
}

/**
* Handles a request that starts with /projects/$projectname/candidates
*
* @param ServerRequestInterface $request The incoming PSR7 request
*
* @return ResponseInterface The outgoing PSR7 response
*/
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$pathparts = $request->getAttribute('pathparts');
if (count($pathparts) !== 0) {
return new \LORIS\Http\Response\JSON\NotFound();
}

switch ($request->getMethod()) {
case 'GET':
return $this->_handleGET($request);

case 'OPTIONS':
return (new \LORIS\Http\Response())
->withHeader('Allow', $this->allowedMethods());
default:
return new \LORIS\Http\Response\JSON\MethodNotAllowed(
$this->allowedMethods()
);
}
}

/**
* Create an array representation of this endpoint's reponse body
*
* @param ServerRequestInterface $request The incoming PSR7 request
*
* @return ResponseInterface The outgoing PSR7 response
*/
private function _handleGET(ServerRequestInterface $request): ResponseInterface
{
if (isset($this->_cache)) {
return $this->_cache;
}

try {
$datestring = $request->getQueryParams()['since'] ?? '1970-01-01';
$since = new \DateTime($datestring);
} catch (\Exception $e) {
return new \LORIS\Http\Response\JSON\BadRequest(
$e->getMessage()
);
}

$provisioner = new \LORIS\api\Provisioners\ProjectDicomsRowProvisioner(
$this->_project,
$since
);
$dicoms = (new \LORIS\Data\Table())
->withDataFrom($provisioner)
->toArray($request->getAttribute('user'));

$this->_cache = new \LORIS\Http\Response\JsonResponse(
['Dicoms' => $dicoms]
);

return $this->_cache;
}

/**
* Implements the ETagCalculator interface
*
* @param ServerRequestInterface $request The PSR7 incoming request.
*
* @return string etag summarizing value of this request.
*/
public function ETag(ServerRequestInterface $request) : string
{
return md5(json_encode($this->_handleGET($request)->getBody()));
}
}
2 changes: 1 addition & 1 deletion modules/api/php/endpoints/project/project.class.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php
/**
* This implements the Project page class under Projects
*
Expand Down

0 comments on commit 3c7c1e6

Please sign in to comment.