-
Notifications
You must be signed in to change notification settings - Fork 0
5. HandlersProvider SPI
Under construction. Check back later.
Module: arestme/data_service
HandlersProvider is an abstract class having its descendants implementing functions that use the HTTP Data Service request context as input to corresponding data records operations and write back response compliant with the protocol.
It has a single function getHandlers
with no parameters, that is the contract between DataService
and its handler to provide handler functions for the different HTTP Data Service operations that are required to be supported by this DataService
.
The getHandlers function must return an object with functions named: "create", "update", "remove", "query", "get", "count", "metadata", "associationList", "associationCreate". Functions with other names are ignored by 'DataService', which will use those that are supplied (it may be a subset of the full list) to setup resource handler configurations as appropriate. Each of these functions is invoked upon the corresponding HTTP Data Service request with arguments context and io. The data service will take care to serve only requests for which there are handling functions provided by this handles map.
//create custom handler var HandlersProvider = require('arestme/data_service').HandlersProvider; var MyHandlersProvider = function(){ HandlersProvider.call(this); }; MyHandlersProvider.prototype = Object.create(HandlersProvider.prototype); MyHandlersProvider.prototype.constructor = MyHandlersProvider; MyHandlersProvider.prototype.getHandlers= function(){ return { query: function(ctx, io){ var entitySet = [{id:1, text: "text 1"}, {id:2, text: "text 2"}]; io.response.setStatus(io.response.OK); io.response.println(JSON.stringify(entitySet, null, 2)); }, get: function(ctx, io){ var id = ctx.athParams.id; //Dummy, just to illustrate options if(id!=="1"){ this.sendError(404, "No such entity"); return; } var entity = {id:1, text: "text 1"}; io.response.setStatus(io.response.OK); io.response.println(JSON.stringify(entity, null, 2)); } }; }; //create a DataService using the custom provider and service requests var DataService = require('arestme/data_service').DataService; var provider = new MyHandlersProvider(); var ds = new DataService(provider) ds.service(); Trying it out: Given that this code is provided in ScriptingServices/myservice.js sending GET /services/js/myservice.js will return response body [{id:1, text: "text 1"}, {id:2, text: "text 2"}] with status code 200 sending GET /services/js/myservice.js/1 will return response body {id:1, text: "text 1"} with status code 200 sending GET /services/js/myservice.js/4 will return status code 404