Skip to main content

HTTP Connector

The SDK Connector is primarily a plug-in-based connector composed of two parts: connectorType and action.

  • connectorType: Used to describe the plug-in and perform initialization tasks.
  • action: Executes the corresponding operations using the initialized information.

For detailed definitions, usage methods, and basic configurations of connectorType and action, please refer to the documentation related to http-connector.


1. SDK Connector Type Script

The connectorType is mainly written in the Groovy language. The script receives inputs (a Map structure containing the values configured for connectorType) and ultimately returns an SDKConnectorInstance object.

Key Field Descriptions:

  • script: The script for initializing the plug-in, which can accept two parameters, sdkInputs and inputs. The return value of the script will be stored in SDKConnectorInstance.executeScriptResult.
  • sdkInputs: Input parameters passed to the script.
  • appType: Generally set to file.
  • jarPath: The directory where the plug-in JAR package is stored.

Example 1: Database Connection Plug-in

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import groovy.sql.Sql

// Database connection information
def dbUrl = "jdbc:sqlserver://" + sdkInputs.host + ":" + sdkInputs.port + ";databaseName=" + sdkInputs.database
def dbUser = sdkInputs.username
def dbPassword = sdkInputs.password

// Configure HikariCP connection pool
def config = new HikariConfig()
config.jdbcUrl = dbUrl
config.username = dbUser
config.password = dbPassword
config.driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
config.maximumPoolSize = 10
config.minimumIdle = 5
config.connectionTimeout = 30000
config.idleTimeout = 600000
config.maxLifetime = 1800000

// Create HikariDataSource data source
def dataSource = new HikariDataSource(config)
return dataSource
def connectorInstance = connector.sdk {
appType "file"
script scripts
sdkInputs (["host": inputs.host, "port": inputs.port, "username": inputs.username, "password": inputs.password, "database": inputs.database])
jarPath inputs.driverPath
}

connectorInstance.cache = true
connectorInstance.expires = 10000000
connectorInstance.timeUnit = "DAYS"
connectorInstance.canSetToGroovy = true
return connectorInstance

Example 2: Third-Party API Authentication Plug-in (Obtaining Token)

package com.king.demo.example;

import com.epoint.token.TokenAuthUtil;

String appKey = inputs.appKey;
String appSecrets = inputs.appSecrets;
long timeout = 300L; // Expiration time (seconds)

try {
String token = TokenAuthUtil.createToken(appKey, appSecrets, timeout);
boolean isValid = TokenAuthUtil.validateToken(token, appKey, appSecrets);
return ["token": token, "valid": isValid]
} catch (Exception e) {
return ["success": false]
}
def connectorInstance = connector.sdk {
appType "file"
script scripts
jarPath inputs.path
}

connectorInstance.cache = true
connectorInstance.expires = 10000000
connectorInstance.timeUnit = "DAYS"
connectorInstance.canSetToGroovy = true
return connectorInstance

2. Action Script

In the action, you can obtain the information returned during the initialization of connectorType through connectorInstance.executeScriptResult.

Example 1: Querying Database Data

import groovy.sql.Sql

// Obtain the database connection instance
def sql = new Sql(connectorInstance.executeScriptResult)

// Execute the query
def query = inputs.sql
List<Map<String, Object>> resultList = []
try {
resultList = sql.rows(query)
} finally {
sql.close()
}
return ["resultList": resultList]

Example 2: Inserting Data into the Database

import groovy.sql.Sql

// Obtain the database connection instance
def sql = new Sql(connectorInstance.executeScriptResult)
try {
// Insert statement
String insertQuery = """
INSERT INTO Test.dbo.connector_type_2 (
id, name, uuid, base_connector_type_uuid
) VALUES (
?, ?, ?, ?
)
"""

// List of records to be inserted
List<List<Object>> records = inputs.records

// Iterate and insert data
records.each { record ->
def insertRecord = [record.id, record.name, record.uuid, record.base_connector_type_uuid]
List<Object> keys = sql.executeInsert(insertQuery, insertRecord)
println "Inserted record with primary key: $keys"
}
} finally {
sql.close()
}

return ["success": true]