From 887857fbdd3667cda77a2bdef77f7da3a873f7dd Mon Sep 17 00:00:00 2001 From: Sander Hagenaars Date: Wed, 3 Jul 2019 09:29:32 +0200 Subject: [PATCH] set endpoint_aliases on RestfulServer to specify fixed aliases for exposed dataobjects --- src/RestfulServer.php | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/RestfulServer.php b/src/RestfulServer.php index eb142f7..f31b4e5 100644 --- a/src/RestfulServer.php +++ b/src/RestfulServer.php @@ -2,6 +2,20 @@ 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\Control\Controller; use SilverStripe\Control\Director; @@ -72,6 +86,15 @@ class RestfulServer extends Controller */ 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 * 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) { - $className = $this->unsanitiseClassName($request->param('ClassName')); + $endpoint = $request->param('ClassName'); + $className = $this->unsanitiseClassName($endpoint); $id = $request->param('ID') ?: null; $relation = $request->param('Relation') ?: null; + if ($alias = $this->getEndpointAlias($endpoint)) { + $className = $alias; + } + // Check input formats if (!class_exists($className)) { return $this->notFound(); @@ -885,4 +913,15 @@ class RestfulServer extends Controller { return Security::getCurrentUser(); } + + /** + * @param $endpoint + * @return null | string + */ + protected function getEndpointAlias($endpoint) + { + $aliases = self::config()->get('endpoint_aliases'); + + return $aliases[$endpoint] ?? null; + } }