Event Scripting
Event Scripting allows users to write scripts that run when certain system events happen. Scriptable events, a.k.a process events, are "fired" when certain parts of the API are called, externally by clients, or internally by other scripts. Events are generated for all services and most resources defined by those services.
Event Format
API-generated events typically take the form ofservice[.resource].verb[.type]where
- service is the API name of the service,
- resource is the optional resource path ('/' replaced with '.') either statically defined, or using replacement parameters within {}, i.e. db._table.{table_name}.get.pre_process,
- verb is the HTTP verb used in the request
- type is the type of event, see below.
Scriptable Event Types
Scriptable events currently fall under three categories: pre-process, post-process, and queued. The pre- and post-process types affect processing of the API call, being run between the time the server receives the call and the response to the client. Note: Care must be taken as the time to process the script itself affects the response time to the client.
Pre-Process
Following the anatomy of an API call, pre-process scripts are run after authentication and authorization, but before the API call is processed by to the handling service. This allows for the following scenarios...
- interrupting the API call before it is processed by throwing exceptions that are returned to the client, i.e. payload validation failures, etc.,
- modifying the contents, parameters, or headers of the request to change how and/or what is processed, i.e. payload modification, etc. (see allow_event_modification in DreamFactory/Features/Scripting#Resources_Available_To_A_Script)
- perform additional functionality before the call is processed, i.e. external validation, tracking API usage, etc.
Post-Process
Likewise, post-process scripts are run after the API call has been processed by the handling service, but before any final formatting and returning to the client. This allows for the following scenarios...
- modifying the contents, status code, or formatting of the response, i.e. adding related data from other services, or removing sensitive information, etc. (see allow_event_modification in DreamFactory/Features/Scripting#Resources_Available_To_A_Script)
- perform additional functionality before the response is returned to the client, i.e. triggering other services upon success or failure, logging, etc.
Queued (Coming Soon)
Queued scripts on the other hand, do not and can not affect the processing of the original API call. Both the request and response of the event are saved along with the script and queued for later execution.
Event Script Configuration
Retrieving a List of Events
To retrieve a list of events that can be scripted, issue a GET on the following API endpoint. Use the service filter the output. Adding the as_list parameter set to true returns just the list of names.
http://example.com/api/v2/system/event?type=process[&service=<service_name>][&as_list=true]
Retrieving a List of Scripted Events
To retrieve a list of events that already have associated scripts, issue a GET on the same API endpoint but add the only_scripted set to true
http://example.com/api/v2/system/event?type=process&only_scripted=true[&service=<service_name>][&as_list=true]
Retrieving Details of an Event Script
Select an event by name to retrieve a list of its details including content of the script itself, by issuing a GET on the following API endpoint. Use the fields URL parameter to select specific fields to return, '*' by default.
http://example.com/api/v2/system/event/{event_name}[?fields=<field1,field2>]
Creating/Updating an Event Script
Configuring an event script consist of the following steps...
- Select an event by name. A list of events to choose from can be retrieved by the following API endpoint. Use the service and only_scripted optional parameters to filter the output. Adding the as_list parameter set to true returns just the list of names.
http://example.com/api/v2/system/event?type=process&only_scripted=false
- Select the language type you wish to write the script in. See DreamFactory/Features/Scripting#Supported_Scripting_Languages.
- Enable the script to run when the event fires by setting the is_active boolean to true; false to disable.
- Enable the script to be allowed to modify the event resource (pre- and post-processing only) by setting allow_event_modification to true; false to disable.
- Write the content or body of your script. Note: This need to be stringified to pass as content in the payload.
- POST the below format to the following API endpoint.
http://example.com/api/v2/system/event
{ "name": "event_name", "type": "string", "is_active": true, "affects_process": true, "content": "string", "config": "string", "allow_event_modification": true }
Event Examples
Below are some example base event names (not including the type section) for various service types.
Database Services
- db.get,
- db._schema.get,
- db._schema.post,
- db._schema.put,
- db._schema.patch,
- db._schema.{table_name}.get,
- db._schema.{table_name}.post,
- db._schema.{table_name}.put,
- db._schema.{table_name}.patch,
- db._schema.{table_name}.delete,
- db._schema.{table_name}.{field_name}.get,
- db._schema.{table_name}.{field_name}.put,
- db._schema.{table_name}.{field_name}.patch,
- db._schema.{table_name}.{field_name}.delete,
- db._table.get,
- db._table.{table_name}.get,
- db._table.{table_name}.post,
- db._table.{table_name}.put,
- db._table.{table_name}.patch,
- db._table.{table_name}.delete,
- db._table.{table_name}.{id}.get,
- db._table.{table_name}.{id}.put,
- db._table.{table_name}.{id}.patch,
- db._table.{table_name}.{id}.delete,
File Services
- files.get,
- files.post,
- files.patch,
- files.delete,
- files.{folder_path}.get,
- files.{folder_path}.post,
- files.{folder_path}.patch,
- files.{folder_path}.delete,
- files.{file_path}.get,
- files.{file_path}.post,
- files.{file_path}.put,
- files.{file_path}.patch,
- files.{file_path}.delete,
HTTP Services
HTTP Services, by default, fire no events unless there are paths defined in the Open-API/Swagger Specification. In that case, events are generated based on calls matching paths defined. myservice.makes.count.get would be generated for a call to GET http://example.com/api/v2/myservice/makes/count.
SOAP Services
SOAP Services fire events based on the methods defined in the given WSDL file. For example, myservice.call_method.post would be generated for a call to POST http://example.com/api/v2/myservice/call_method.
Auth Services
- user.get,
- user.password.post,
- user.profile.get,
- user.profile.post,
- user.register.post,
- user.session.get,
- user.session.post,
- user.session.delete,
- user.custom.get,
- user.custom.post,
- user.custom.patch,
- user.custom.delete,
- user.custom.{id}.get,
- user.custom.{id}.patch,
- user.custom.{id}.delete,
System Resources
The system service generates a lot of events, for example, for the root retrieval of resources...
- system.get
for common resources like admin, app, app_group, custom, cors, roles, services, users...
- system.service.get,
- system.service.post,
- system.service.patch,
- system.service.delete,
- system.service.{id}.get,
- system.service.{id}.patch,
- system.service.{id}.delete,
some others include...
- system.cache.delete,
- system.cache.{service}.delete,
- system.config.get,
- system.config.post,
- system.constant.get,
- system.constant.{type}.get,
- system.environment.get,
- system.event.get,
- system.event.{event_name}.get,
- system.event.{event_name}.post,
- system.event.{event_name}.delete,
- system.service_type.get,
- system.script_type.get,
- system.package.get,
- system.package.post,