This post provides you with the full code used in the article Automate Tasks with Teneo Studio API. With the following code you can automate the Auto-test , Class management and Publishing tasks without opening Teneo Studio. The code is written in Groovy and needs Java 11 or a later version to run.
// Import essential packages
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
// Create class and methods
class SolutionApi {
private static final String serverURL = "" // fill in with your server url
private static final String username = "" // fill in with your user name
private static final String password = "" // fill in with your password
private static String accessToken = "" // leave it as an empty String
// log in the Teneo Studio with your account
public static boolean login() {
def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/auth/login"
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("content-type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString(new JsonBuilder(["username":username,"password":password]).toString()))
.build()
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
if (response.statusCode() == 200) {
accessToken = new JsonSlurper().parseText(response.body())
return true
} else {
return false
}
}
// log out from the Teneo Studio after you finish your task
public static void logout() {
def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/auth/logout"
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("content-type", "application/json")
.header("Authorization", "Bearer "+accessToken)
.method("POST", HttpRequest.BodyPublishers.noBody())
.build()
HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
}
// run Auto-test and return a summary ID
public static String runAutotest(String solutionId, boolean trigger = true, boolean transition = true, boolean url = true, boolean stable = false, boolean flow_scope = false) {
def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/auto-tests/begin/" + solutionId + "?trigger-tests=" + trigger + "&transition-tests=" + transition + "&url-tests=" + url + "&stable=" + stable + "&flow-scope=" + flow_scope
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("content-type", "application/json")
.header("Authorization", "Bearer "+accessToken)
.method("POST", HttpRequest.BodyPublishers.ofString('{}'))
.build()
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
def autotestId = new JsonSlurper().parseText(response.body())
return autotestId
}
// get Auto-test result by summary ID
public static HashMap getAutotestSummary(String solutionId, String summaryId, boolean ignore_skipped = true, boolean ignore_success = true, boolean ignore_warnings = false) {
def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/auto-tests/summaries/" + solutionId + "/" + summaryId + "?ignore-skipped=" + ignore_skipped + "&ignore_success=" + ignore_success + "&ignore_warnings=" + ignore_warnings
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("content-type", "application/json")
.header("Authorization", "Bearer "+accessToken)
.method("GET", HttpRequest.BodyPublishers.noBody())
.build()
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
def summary = new JsonSlurper().parseText(response.body())
return summary
}
// create a new class in the Class Manager
public static HashMap createClass(String solutionId, String className, List<String> trainingData) {
def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/classes/" + solutionId
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("content-type", "application/json")
.header("Authorization", "Bearer "+accessToken)
.method("POST",HttpRequest.BodyPublishers.ofString(new JsonBuilder(["name":className,"trainingData":trainingData]).toString()))
.build()
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
def classAdded = new JsonSlurper().parseText(response.body())
return classAdded
}
// modify an existing class in the Class Manager
public static HashMap updateClass(String solutionId, String classId, String className, List<String> trainingData) {
def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/classes/" + solutionId + "/" + classId
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("content-type", "application/json")
.header("Authorization", "Bearer "+accessToken)
.method("PUT",HttpRequest.BodyPublishers.ofString(new JsonBuilder(["name":className,"trainingData":trainingData]).toString()))
.build()
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
def classUpdated = new JsonSlurper().parseText(response.body())
return classUpdated
}
// get the details of an existing class in the Class Manager
public static HashMap getClassInfo(String solutionId, String classId) {
def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/classes/" + solutionId + "/" + classId
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("content-type", "application/json")
.header("Authorization", "Bearer "+accessToken)
.method("GET", HttpRequest.BodyPublishers.noBody())
.build()
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
def classes = new JsonSlurper().parseText(response.body())
return classes
}
// publish your solution to a publish environment
public static String publishToEnvironment(String solutionId, String environmentId, boolean forceFullPublish = false) {
def apiUrl = (serverURL.endsWith("/")?serverURL:serverURL + "/") + "teneo-studio/rest/publish-environments/publish/begin/" + solutionId + "/" + environmentId
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(apiUrl))
.header("content-type", "application/json")
.header("Authorization", "Bearer "+accessToken)
.method("POST",HttpRequest.BodyPublishers.ofString(new JsonBuilder(["forceFullPublish":forceFullPublish]).toString()))
.build()
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
def publishId = new JsonSlurper().parseText(response.body())
return publishId
}
}
// execute the following line to log in
def loginSuccessful = SolutionApi.login()
// only execute the following lines when you’ve logged in succesfully
if(loginSuccessful) {
// run an Auto-test and get the summary
def summaryId = SolutionApi.runAutotest(solutionId)
def summary = SolutionApi.getAutotestSummary(solutionId,summaryId)
// add a class in the Class Manager
def classAdded = SolutionApi.createClass(solutionId,className, trainingData)
// modify an existing class in the Class Manager
def classModified = SolutionApi.updateClass(solutionId,classId,className,trainingData)
// add more training data to an existing class
def classInfo = SolutionApi.getClassInfo(solutionId,classId)
def newTrainingData = [] // your new training examples here
def classUpdated = SolutionApi.updateClass(solutionId,classId,classInfo.name,classInfo.trainingData + newTrainingData)
// publish your solution
def publishTaskId = SolutionApi.publishToEnvironment(solutionId,environmentId)
// log out from the Teneo Studio
SolutionApi.logout()
}