Calling a decision service by using the REST API

Your client calls a decision service through the REST API of Operational Decision Manager. The client calls the decision service by using the standard HTTP GET, POST, PUT, and DELETE commands that web browsers use when they interact with remote servers.

About this task

Operational Decision Manager provides business decisions as reusable operations that are known as decision services. A client application calls a decision service by creating a secure connection with a server, and then calling the decision service through the REST API.
The endpoint URI for a decision service in the Operational Decision Manager server uses the following format:
http://<host>:<port>/DecisionService/rest/v1/<ruleset_path>

The following list explains the parts of the URI:

  • <host>: Serves as the name of the host for the decision service.
  • <port>: Port number
  • <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:
http://localhost:9080/DecisionService/rest/v1/
MiniloanService/MiniloanServiceRuleset
The execution of the decision service ruleset is done through an HTTP POST at the specified endpoint URI.

Example

The following example shows Java™ code that calls the MiniloanService decision service on the Operational Decision Manager server. The example uses Apache HTTPClient v4.3.

import java.io.IOException;

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 <host> and <port>
		String endpointURI = "http://<host>:<port>/DecisionService/rest/v1/MiniloanService/MiniloanServiceRuleset";

		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);
			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();
		}
	}
	
}