forked from aces/Loris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[API] added endpoints/project/dicoms.class.inc that was left behind
- Loading branch information
Showing
2 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
|