Interface DataConnectors
public interface DataConnectors
DataConnectors
API provides the functionality to create, namespace and configure RESTDataConnectors
.
The RESTDataConnector
are wrapped inside a JavaScript object that are namespaced into one of two locations:
connectors.verjio.
: The namespace location to store Data Connectors that have been downloaded from the Verj.io Materials and downloaded from the Resource Hubconnectors.dataconnectors.
: The namespace location to store customized Data Connectors that have been created in the workspace
RESTDataConnector
to be used in any JavaScript event, for example, a button click.
The DataConnectors
API provides a function to retrieve the Data Connector configuration
from the Server Administration Application
- Since:
- V5.13
- See Also:
RESTDataConnector
-
Method Summary
Modifier and Type Method Description RESTDataConnector
createRESTDataConnector(java.lang.String baseUri)
Creates a RESTful Data Connector constructed with a base URI that provides the following functionality: Direct communication with a REST service without having to callRestServices
API helper functions. Customisable headers are sent with all REST calls. Authentication can be sent with all REST calls. Error handling for typical HTTP error codes. Response handling for Data Connector REST calls.java.util.Map<java.lang.String,java.lang.Object>
getConfig(java.lang.String name)
Returns the Data Connector Configuration configured using the Server Administration Console.java.util.Map<java.lang.String,java.lang.Object>
getDataconnectors()
Namespaced map of REST Data Connectors prefixed withconnectors.dataconnectors.
Errors
getErrors()
TheErrors
is a server side JavaScript object that contains functions that allows the creation of customizable error handling functionality specifically for the HTTP error response status returned from the server, for example a 404 Not Found error.java.util.Map<java.lang.String,java.lang.Object>
getVerjio()
Namespaced map of REST Data Connectors prefixed withconnectors.verjio.
-
Method Details
-
createRESTDataConnector
Creates a RESTful Data Connector constructed with a base URI that provides the following functionality:- Direct communication with a REST service without having to call
RestServices
API helper functions. - Customisable headers are sent with all REST calls.
- Authentication can be sent with all REST calls.
- Error handling for typical HTTP error codes.
- Response handling for Data Connector REST calls.
The
RESTDataConnector
is constructed with a base URI that is used to construct calls to a specific endpoint when calling the appropriate RESTful web service call.The HTTP header Content-Type: application/json is set as default. This can be overridden by calling the
RESTDataConnector#setDefaultHeader(String, Object)
function.JavaScript example:
const restDataConnector = connectors.createRESTDataConnector("https://jukeboxapi.example.com/"); // Build the URI for the /playlists endpoint const uri = restDataConnector.get("playlists"); // Call the endpoint using the HTTP GET method var response = restDataConnector.get(uri); // Iterate through the playlists array in the response object response.playlists.forEach(function(playlist) { tables.playlists.insertRow(); tables.playlists.name.value = playlist.playlist_name; });
- Parameters:
baseUri
- - the URI to call, typically this should be used withRESTDataConnector.createUri(String)
to concatenate the base URI with the endpoint- Returns:
RESTDataConnector
object used for RESTful data connector calls- Since:
- v5.13.0
- See Also:
RESTDataConnector
- Direct communication with a REST service without having to call
-
getVerjio
java.util.Map<java.lang.String,java.lang.Object> getVerjio()Namespaced map of REST Data Connectors prefixed withconnectors.verjio.
The namespace location to store Data Connectors that have been downloaded from the Verj.io Materials and downloaded from the Resource Hub
Using the Data Connector example:
On Click Event:
function checkMOTValid() { var valid; try { var resp = connectors.verjio.dvla.getVehicleInfo('HA58 TAG'); valid = resp.motStatus; } catch (e) { if (e instanceof HttpNotFoundError) { // Registration number was not found valid = null; } } return valid; }
- Returns:
- Map of data connectors stored within the namespace
connectors.verjio
- Since:
- v5.13.0
-
getConfig
java.util.Map<java.lang.String,java.lang.Object> getConfig(java.lang.String name)Returns the Data Connector Configuration configured using the Server Administration Console.The properties are accessed using the name of the property using standard Java Object notation.
JavaScript example:
var config = connectors.getConfig("OpenAI"); var apiKey = config.apikey; var organisationId = config["org"];
- Parameters:
name
- connector configuration name- Returns:
- data connector configuration map of configuration properties
- Since:
- v5.13.0
-
getDataconnectors
java.util.Map<java.lang.String,java.lang.Object> getDataconnectors()Namespaced map of REST Data Connectors prefixed withconnectors.dataconnectors.
connectors.dataconnectors.
: The namespace location to store customized Data Connectors that are been created in the workspace and have not been downloaded from the Resource HubData Connector example:
if (!connectors.dataconnectors.myservice) { connectors.dataconnectors.myservice = new MyService(); } // MyService Data Connector function MyService() { const restDataConnector = new RESTDataConnector("https://my.service.example.com/api/2.1/"); this.authorize = function (params) { const uri = restDataConnector.createUri("authorise"); const body = { "user" : fields.user.value, "password" : fields.password.value }; const options = { parameters: params }; return restDataConnector.post(uri, body, options); } }
On Click Event:
try { var response = connectors.dataconnectors.myservice.authorize(); log(response.authorised); } catch(e) { event.owner.addErrorMessage("Unable to authorize user"); }
- Returns:
- Map of data connectors stored within the namespace
connectors.dataconnectors
- Since:
- v5.13.0
-
getErrors
Errors getErrors()TheErrors
is a server side JavaScript object that contains functions that allows the creation of customizable error handling functionality specifically for the HTTP error response status returned from the server, for example a 404 Not Found error. The error handlers are created using a description string and an error code. The errors are as follows:HttpRequestError
- This is typically used to create HTTP response failure errors, for example the server responds with a 400 Bad Request codeHttpAuthorisationError
- This should be used when generating Authentication errors, for example the server responds with a 401 Unauthorized codeHttpNotFoundError
- This should be used when generating not found errors, for example the server responds with a 404 Not Found codeHttpServerError
- This should be used when generating generic server errors, for example the server responds with a 500 Server Error codeHttpUnknownError
- This should be used when generating an unknown or unrecognized error, for example the HTTP code is not recognized
errorHandler
function is called to handle errors. The default error handler throws the errors described above as exceptions that can be caught from theRESTDataConnector
RESTFul web service call.The error handler can be overidden. The error handler is set by calling the
RESTDataConnector.setErrorHandler(Function)
function.JavaScript example:
// Define a custom error handler function handleError(response) { switch(response.code) { case 401: // Send an email stating that the service is authorized fields.unauthorizedError.value = response.body; resources.sysAdmin.sendMail(); throw connectors.errors.createAuthorisationError("Unauthorized", response.code); default: throw connectors.errors.createRequestError("Request error", response.code); } }; try { const restDataConnector = connectors.createRESTDataConnector("https://jukeboxapi.example.com/"); // Override the default error handler restDataConnector.setErrorHandler(errors); // Call an endpoint const uri = restDataConnector.get("playlists"); var response = restDataConnector.get(uri); // ... } catch(e) { // Show the user the error message event.owner.addErrorMessage(e); }
- Returns:
Errors
an interface containing functions to create theHttpError
errors- Since:
- v5.13.0
-