Tips & Tricks - Integrate REST services

General

This workshop provides an example for integrating REST services. The translation service DeepL is used for this. When the user saves a text in German, it will be translated into English and French.

Installation

Download the example application here. Import the downloaded file "advanced-techniques-rest-services-integration.zip" and select the application and process it contains. Then switch to the Processes module, open the process "Tips & Tricks - Integrate REST services" and activate it.

Enter the API key

To use the translation app, an API key is required. This is available from the DeepL website.

Open the example application in the browser and enter the API key on the "API key" page. Click on "Save" when you are finished.

Translate

Go to the edit page and enter a text of your choice for the translation. Then click on "Save".

The translated text is then displayed in the other languages.

Process structure

The example process responds via a data group event handler to data records added on the edit page in the example application. Afterwards, the DeepL request is made via the following script in a Groovy script action.

import de.uplanet.scripting.groovy.util.Safely
import org.apache.http.*
import org.apache.http.entity.*
import org.apache.http.util.*
import org.apache.http.client.*
import org.apache.http.client.methods.*
import org.apache.http.client.utils.*
import org.apache.http.impl.client.*
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;

def key    = g_sysDg.getValueHolderByFieldGuid('AE0F3F49EDAB0B3381ADD35815E7E8E81CAB7DDF') //API Key

assert key.hasValue()  : "No DeepL API-Key specified."

def deText = g_record["34078FB863412A7C561F0C8335EAAB5C3EAFAEAD"].value /* datafield DE <text> */

def translations   = translateJson(deText, "EN", key.getValue().toString())
g_sharedState.enText = translations.translations[0].text

translations   = translateJson(deText, "FR", key.getValue().toString())
g_sharedState.frText = translations.translations[0].text


def translateJson(textToTranslate, targetLang, key)
{
	URI uri = new URIBuilder()
	        .setScheme("https")
	        .setHost("api.deepl.com")
	        .setPath("v1/translate")
	        .setParameter("auth_key", key)
	        .setParameter("source_lang", "DE")
	        .setParameter("target_lang", targetLang)
	        .setParameter("text", textToTranslate)
	        .setParameter("split_sentences", "1")
	        .setParameter("preserve_formatting", "1")
	        .build();

	g_json.httpGet(url: uri, readTimeout: 20000)
}

The English and French translation is transferred to the sharedState in the variables "enText" and "frText".

In the subsequent data group action, both variables, as user-defined system values from the "Processing context", are mapped to the correpsonding fields in the example application.