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
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
- Obtain a OpenAPI file for a decision service ruleset.
- Generate proxy classes from the OpenAPI file.
- Use the proxy classes to invoke the ruleset from your client application.
Example
To get the OpenAPI file from Rule Execution Server:
- Deploy the decision service ruleset to Rule Execution Server.
- In the Rule Execution Server console, go to the Explorer tab.
- In the Navigator pane, click a RuleApp, and then click a ruleset for your decision service.
- In ruleset view, click Retrieve HTDS Description File.
- Select REST as a service protocol type.
- Select OpenAPI – YAML or OpenAPI – JSON as a format to generate an OpenAPI file in YAML or JSON.
- Check Latest ruleset version and Latest RuleApp version to generate the OpenAPI file for the latest versions.
- Click Download.
To generate the proxy classes in Swagger Editor:
- Open http://editor.swagger.io/
in a web browser. - Click .
- Click Browse to select the OpenAPI file, and then click Open.
- Click Import.
- Click .
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);
}
}
}