Sample Plugin Code
BioT provides a sample JavaScript Plugin code that describes the steps needed in plugin development.
The code is available free under the MIT license and can be browsed here.
Prerequisites
To pack and deploy the sample, you require Node.js installed on your computer.
You can download it for free from here.
Download the Code
To download the code, make sure you have GIT installed and execute the following command from our command line:
git clone https://bitbucket.org/softimize/plugin-lambda-seed-public.git
To download dependencies, execute the following command line:
npm install
Basic Plugin Flow
The following boilerplate functions should be executed before the code can be executed:
(You may change them as required)
- extractDataFromEvent - Extracts the data and metadata from the event.
- getTraceId - Get a trace ID from the event (or fallback to a trace ID from a BioT service). The trace ID is used for better logging traceability.
- configureLogger - Create new logs format (including a traceId).
- authenticate - Authenticate the token sent by the notification service.
- login - Login to BioT (using a service user) and get a token.
- perform - An example of the different event types.
- response - Send response (see errorResponseCreator for error responses).
extractDataFromEvent
A helper function that obtains the raw JSON data from the event.
getTraceId
The trace ID is a unique ID string that is set in the header of each API call and saved to the log. Using this unique ID allows us to trace the whole flow of the call, in case of a problem, beginning with the plugin and going through all the API calls that the plugin made, and any internal API calls made by the platform until the plugin response.
configureLogger
This function prepares the log structure for a well-structured log that will enable easy searching when needed.
authenticate
This function validates the JWT token received from BioT to ensure the call originated from the platform and not from any unauthorized origin.
login
Before making an API call to BioT the plugin needs to obtain a new JWT token using the service user credentials.
perform
According to the event type, the demo plugin maps the event to the appropriate event handler.
Under the src folder, a specific handler was written to each event type:
Notification handler from "./notification/index.js"
Interceptor Post from "./interceptorPost/index.js"
Interceptor Pre from "./interceptorPre/index.js"
Interceptor Post Entity from "./interceptorPostEntity/index.js"
Check the appropriate code for each plugin type for an example on how to handle the event and how to make API calls.
response
The response depends on the plugin and should be updated according to your business logic.
Deploy the Plugin
You may want to zip the source code before deploying it by executing the following command:
npm run zip
To deploy the code, refer to Custom Plugin Deployment.
Set Environment Variables
The following environment variables need to be set before the plugin can be executed:
Variable Name | Description |
---|---|
BIOT_APP_NAME | Name of this Plugin, used for logging purposes. |
BIOT_BASE_URL | The base API URL of your BioT account. For example, https://api.dev.biot-med.com |
BIOT_JWT_PERMISSION | Set as ACTION_NOTIFICATION |
BIOT_PUBLIC_KEY | The public key of BioT, used to ensure the call originated from BioT. The key can be obtained by making a request to: GET: https://<your_biot_domain>/ums/v1/security/public-key |
BIOT_SERVICE_USER_ID | The service user ID. See Creating a Service User for Your Plugin. |
BIOT_SERVICE_USER_SECRET_KEY | The service user secret key. See Creating a Service User for Your Plugin. |
BIOT_SHOULD_VALIDATE_JWT | Specifies to the lambda if it should check for a valid JWT token in the request header. This is done to make sure requests originate from BioT. If you have your own validation method, or you want to make the lambda truly public, set it to "false" |
To set these environment variables:
- Login to the AWS console and go to the Lambda service.
- From the list, select your Lambda function and click on "Configuration", then "Environment Variables" and finally "Edit".
- In the Edit Environment Variables box that appears, enter the environment variables as keys and values.
- To add more than one environment variable, click Add environment variable.
- When finished, click Save.
Test Your Plugin
Now register your Plugin as a Notification or Interceptor and verify that it works.
Updated 2 months ago