Using OpenAPI

You can generate a client from an OpenAPI file that is made for a decision service ruleset.

Before you begin

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

Important:

You can use Swagger Editor or other open source Swagger tools External link opens a new window or tab to generate a client in your preferred language, for example: C®#, Go, Apache Groovy, JavaScript, PHP, Python, Ruby, Scala, Swift, or TypeScript.

About this task

To call a decision service ruleset from a client application, you create proxy classes from an OpenAPI file generated for a ruleset path.

Procedure

  1. Obtain a OpenAPI file for a decision service ruleset.
  2. Generate proxy classes from the OpenAPI file.
  3. Use the proxy classes to invoke the ruleset from your client application.

Example

To get the OpenAPI file from Rule Execution Server:
  1. Deploy the decision service ruleset to Rule Execution Server.
  2. In the Rule Execution Server console, go to the Explorer tab.
  3. In the Navigator pane, click a RuleApp, and then click a ruleset for your decision service.
  4. In ruleset view, click Retrieve HTDS Description File.
  5. Select REST as a service protocol type.
  6. Select OpenAPI – YAML or OpenAPI – JSON as a format to generate an OpenAPI file in YAML or JSON.
  7. Check Latest ruleset version and Latest RuleApp version to generate the OpenAPI file for the latest versions.
  8. Click Download.
To generate the proxy classes in Swagger Editor:
  1. Open http://editor.swagger.io/ External link opens a new window or tab in a web browser.
  2. Click File > Import File.
  3. Click Browse to select the OpenAPI file, and then click Open.
  4. Click Import.
  5. Click Generate Client > Java.
The following Java™ code sample imports the proxy classes generated in Swagger Editor for a ruleset and calls the ruleset from a Java application:
import java.util.List;

import io.swagger.client.ApiClient;
import io.swagger.client.ApiException;
import io.swagger.client.model.Borrower;
import io.swagger.client.model.Loan;
import io.swagger.client.model.Request;
import io.swagger.client.model.Response;

public class DecisionServiceExecution {

	public static void main(String[] args) {
		
		ApiClient apiClient = new ApiClient();
		
		// Replace "loginID" with the ID of a user who has access to the Cloud portal
		apiClient.setUsername("loginID");
		
		// Replace "password" with the password of a user who has access to the Cloud portal
		apiClient.setPassword("password");
		
		DefaultApi api = new DefaultApi(apiClient);
		
		// Create the request
		Request request = new Request();
		
		// Set the borrower
		Borrower borrower = new Borrower();
		borrower.setName("John");
		borrower.setCreditScore(600);
		borrower.setYearlyIncome(80000);
		request.setBorrower(borrower);

		// Set the loan
		Loan loan = new Loan();
		loan.setAmount(500000);
		loan.setDuration(240);
		loan.setYearlyInterestRate(0.05);
		// approved (set to true by default, to be computed by the decision engine)
		loan.setApproved(true);
		request.setLoan(loan);
		
		// Retrieve the response
		try {
			Response response = api.callDecisionOperation(request);
			System.out.println("Rules executed.");
			System.out.println("Approved: " + response.getLoan().getApproved());
			System.out.println("Yearly interest rate: " + response.getLoan().getYearlyInterestRate());
			System.out.println("Yearly repayment: " + response.getLoan().getYearlyRepayment());
			List<String> messages = response.getLoan().getMessages();
			if (messages != null) {
				System.out.println("Messages: ");
				for (String message : messages) {
					System.out.println(message);
				}
			}
		} catch (ApiException e) {
			throw new RuntimeException("An error occurred when invoking Decision Service", e);		
		}
	}
	
}