Trigger

From Tygron Support wiki
Revision as of 14:36, 13 December 2021 by Rudolf@tygron.nl (talk | contribs)
Jump to navigation Jump to search
This article is a stub.

API Triggers can be used to execute external scripts and applications.

When an API Trigger executes, it sends a web request to a specified url. The web request includes query parameters which the receiving server can use to interact with the session from which the call originated.

Request

The following query parameters are relevant:

Type Key Meaning Notes
Querystring / GET parameter token API token for session access
Querystring / GET parameter f Expected return format. Defaults to "JSON"
Request header Referer The originating Tygron server's url "Referer" is misspelled in the original http specification.

Response

The web server is expected to send a response which is a parseable JSON object. If the trigger type is TQL, each key of the object must be a valid TQL update statement, and each value must be a value in decimal notation.

Examples

Some simple examples exist to demonstrate the call-and-response functionality required for API triggers.

TQL example: Geometry counter

The following example is PHP code which reads out a number of Item types, and writes the results into Globals.

$apitoken = $_GET['token'];
$serverurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $_GET['server'];

$urls= [
		'buildings' => $serverurl . 'api/session/items/buildings?f=JSON&token=' . $apitoken,
		'neighborhoods' => $serverurl . 'api/session/items/neighborhoods?f=JSON&token=' . $apitoken,
		'areas' => $serverurl . 'api/session/items/areas?f=JSON&token=' . $apitoken,
	];


$queries = [];

foreach( $urls as $key => $url ) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$webcallResult = curl_exec($ch);
	
	curl_close($ch);
	
	$features = json_decode($webcallResult, true);
	$amountOfFeatures = count($features);

	$globalName = strtoupper($key).'_COUNTED';
	$queries[ 'UPDATE_GLOBAL_VALUE_WHERE_NAME_IS_' . $globalName ] = $amountOfFeatures . '.0' ;
}

exit( json_encode( $queries, JSON_PRETTY_PRINT ) );