set endpoint_aliases on RestfulServer to specify fixed aliases for exposed dataobjects

This commit is contained in:
Sander Hagenaars 2019-07-03 09:29:32 +02:00
parent e0f4e5684f
commit 887857fbdd

View File

@ -2,6 +2,20 @@
namespace SilverStripe\RestfulServer; namespace SilverStripe\RestfulServer;
use NobrainerWeb\App\DataObjects\Deviation;
use NobrainerWeb\App\Forms\Client;
use NobrainerWeb\App\Forms\Form;
use NobrainerWeb\App\Forms\Submission as FormSubmission;
use NobrainerWeb\App\Invoices\Invoice;
use NobrainerWeb\App\Logs\ErrorRequestLog;
use NobrainerWeb\App\Logs\ImageRequestLog;
use NobrainerWeb\App\MobileConfig\MobileConfig;
use NobrainerWeb\App\Projects\HealthFacility;
use NobrainerWeb\App\Projects\Order;
use NobrainerWeb\App\Projects\Project;
use NobrainerWeb\App\Troubleshooters\Troubleshooter;
use NobrainerWeb\App\Units\LocationSubmission;
use NobrainerWeb\App\Units\Unit;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
@ -72,6 +86,15 @@ class RestfulServer extends Controller
*/ */
private static $default_extension = "xml"; private static $default_extension = "xml";
/**
* Custom endpoints that map to a specific class.
* This is done to make the API have fixed endpoints, instead of using fully namespaced classnames, as the module does by default
* The fully namespaced classnames can also still be used though
*
* @config array
*/
private static $endpoint_aliases = [];
/** /**
* Whether or not to send an additional "Location" header for POST requests * Whether or not to send an additional "Location" header for POST requests
* to satisfy HTTP 1.1: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html * to satisfy HTTP 1.1: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
@ -173,10 +196,15 @@ class RestfulServer extends Controller
*/ */
public function index(HTTPRequest $request) public function index(HTTPRequest $request)
{ {
$className = $this->unsanitiseClassName($request->param('ClassName')); $endpoint = $request->param('ClassName');
$className = $this->unsanitiseClassName($endpoint);
$id = $request->param('ID') ?: null; $id = $request->param('ID') ?: null;
$relation = $request->param('Relation') ?: null; $relation = $request->param('Relation') ?: null;
if ($alias = $this->getEndpointAlias($endpoint)) {
$className = $alias;
}
// Check input formats // Check input formats
if (!class_exists($className)) { if (!class_exists($className)) {
return $this->notFound(); return $this->notFound();
@ -885,4 +913,15 @@ class RestfulServer extends Controller
{ {
return Security::getCurrentUser(); return Security::getCurrentUser();
} }
/**
* @param $endpoint
* @return null | string
*/
protected function getEndpointAlias($endpoint)
{
$aliases = self::config()->get('endpoint_aliases');
return $aliases[$endpoint] ?? null;
}
} }