Using the REST API to call a ruleset

Your client application calls a decision service ruleset through the REST API. It uses standard HTTP GET, POST, PUT, and DELETE commands.

Before you begin

The client application must pass authentication credentials for the cloud portal.

About this task

A client application calls a decision service ruleset by connecting with an execution server and calling the ruleset through the REST API.
The endpoint URI for the ruleset in the server looks as follows:
https://<vhostname>.bpm.ibmcloud.com/odm/<odm_on_cloud_environment>/DecisionService/rest/v1/<ruleset_path>

The following list explains the parts of the URI:

  • <vhostname>: Serves as the name of the host for the decision service.
  • <odm_on_cloud_environment>: Uses dev, test, or prod depending on whether the decision service ruleset is in the development, test, or production environment.
  • <ruleset_path>: Provides the path for the decision service rule execution and must take one of the following formats:
    • ruleAppName/rulesetName
    • ruleAppName/ruleAppVersion/rulesetName
    • ruleAppName/ruleAppVersion/rulesetName/rulesetVersion
The following example shows a URI to MiniloanServiceRuleset:
https://vhost005.bpm.ibmcloud.com/odm/dev/DecisionService/rest/v1/
MiniloanService/MiniloanServiceRuleset
The execution of the ruleset is performed through an HTTP POST at the specified endpoint URI. The user name and password for the authentication are passed with an authorization header (see Authentication for REST and SOAP invocation).

Example

The following example shows Java™ code that calls MiniloanServiceRuleset on the server. The example uses Apache HTTPClient v4.3.
import java.io.IOException;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DecisionServiceExecution {

	public static void main(String[] args) throws ClientProtocolException, IOException {
		
		// Replace <vhostname> with the name of the host of the cloud portal
		String endpointURI = "https://<vhostname>.bpm.ibmcloud.com/odm/dev/DecisionService/rest/v1/MiniloanService/MiniloanServiceRuleset";

		// Replace "loginID" with the functional ID of the service account you are using to authenticate with your tenant in the Cloud
		String userName = "loginID";
		
		// Replace "password" with the password of the service account you are using to authenticate with your tenant in the Cloud
		String password = "password";

		String credentials = userName + ":" + password;
		String encodedValue = Base64.encodeBase64String(credentials.getBytes());
		String authorization = "Basic " + new String(encodedValue);

		String contentType = "application/json";

		// Set the borrower and the loan
		String payload = "{\"borrower\": {" +
				"\"name\": \"John\"," +
				"\"creditScore\": 600," +
				"\"yearlyIncome\": 80000" +
				"}," +
				"\"loan\": {" +
				"\"amount\": 500000," +
				"\"duration\": 240," +
				"\"yearlyInterestRate\": 0.05" +
				"}" +
				"}";
		
		CloseableHttpClient client = HttpClients.createDefault();
		
		try {
			HttpPost httpPost = new HttpPost(endpointURI);
			// Add the basic authentication header
			httpPost.addHeader("Authorization", authorization);
			httpPost.setEntity(new StringEntity(payload));
			httpPost.addHeader("Content-Type", contentType);
			CloseableHttpResponse response = client.execute(httpPost);
			try {
				if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
					System.err.println("Status Code: " + response.getStatusLine().getStatusCode());
					System.err.println("Status Line: " + response.getStatusLine());
					String responseEntity = EntityUtils.toString(response.getEntity());
					System.err.println("Response Entity: " + responseEntity);

					throw new RuntimeException(
							"An error occurred when invoking Decision Service at: "
									+ endpointURI
									+ "\n"
									+ response.getStatusLine() + ": " + responseEntity);
				} else {
					String result = EntityUtils.toString(response.getEntity());
					System.out.println("Result: " + result);
				}
			} finally {
				if (response != null) {
					response.close();
				}
			}
		} finally {
			client.close();
		}
	}
	
}