Updated on 2023-02-28 GMT+08:00

Function API Script Compilation Guide

This section describes how to compile the definition script when developing a function API on the custom backend.

A function API encapsulates multiple services into one service by compiling function scripts.

The script of a function API is compiled using JavaScript, which complies with the Java Nashorn standard and supports ECMAScript Edition 5.1. The JavaScript engine runs on the Java virtual machine and can invoke the Java class provided by the custom backend to implement specific functions.

Example 1: Helloworld

Define the execute function as an entry. The content returned by the execute function is used as the response body of the function API.

1
2
3
function execute(data) {
    return "Hello world!"
}

Example 2: Obtaining Request Parameters

The data parameter inputted by the execute function contains request parameters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function execute(data) {
    data = JSON.parse(data)
    return {
        "method":data["method"],
        "uri":data["uri"],
        "headers":data["headers"],
        "param":data["param"],
        "body":data["body"],
    }
}

Example 3: Setting Response Parameters

The APIConnectResponse object is returned in the execute function. You can specify the HTTP status code, return header, and body returned by the API.

1
2
3
4
importClass(com.roma.apic.livedata.provider.v1.APIConnectResponse);
function execute(data) {
    return new APIConnectResponse(401, {"X-Type":"Demo"}, "unauthorized", false);
}

In this example, the HTTP status code returned when the function API is called is 401, the response header contains "X-Type: Demo", and the response body is "unauthorized".

Example 4: Invoking the Java Function

In the following example, the Java class is introduced using importClass and the functions in it are called. For details about all Java classes, see APIConnectResponse.

1
2
3
4
5
importClass(com.roma.apic.livedata.common.v1.Md5Utils);
function execute(data) {
    var sourceCode = "Hello world!";
    return Md5Utils.encode(sourceCode);
}

Referencing Public Configuration

  1. Log in to the ROMA Connect console. On the Instances page, click View Console next to a specific instance.
  2. In the navigation pane, choose API Connect > Custom Backend. On the Configurations tab page, click Add Configuration.
  3. In the dialog box displayed, configure related information and click OK.
    Table 1 Public reference configurations

    Parameter

    Description

    Configuration Name

    Enter a configuration name.

    Configuration Type

    Select a configuration type. The value can be Template Variable, Password, or Certificate.

    Integration Application

    Select the integration application to which the configuration belongs.

    Configuration Value

    This parameter is available only when Configuration Type is set to Template variable or Password.

    Enter the template variable or password.

    Confirm Value

    This parameter is available only when Configuration Type is set to Password.

    Enter the password again, which must be the same as the value of Configuration Value.

    Certificate

    This parameter is mandatory only when Configuration Type is set to Certificate.

    Enter the certificate in PEM format.

    Private Key

    This parameter is mandatory only when Configuration Type is set to Certificate.

    Enter the private key of the certificate in PEM format.

    Password

    This parameter is available only when Configuration Type is set to Certificate.

    Enter the password of the certificate private key.

    Confirm Password

    This parameter is mandatory only when Configuration Type is set to Certificate.

    Enter the password of the certificate private key again, which must be the same as the value of Password.

    Description

    Enter a description of the configuration.

  4. Reference the configuration in the Java Script function script.
    If the configuration name is example, the reference format is as follows:
    • Template variable: #{example}
    • Password: CipherUtils.getPlainCipherText("example")
    • Certificate: CipherUtils.getPlainCertificate("example")