OpenAPI の使用

意思決定サービスのルール・セット用に作成された OpenAPI ファイルからクライアントを生成できます。

開始前に

クライアント・アプリケーションは、クラウド・ポータルの認証資格情報を渡す必要があります。

重要:

Swagger Editor やその他のオープンソースの Swagger ツールを使用して、C#、Go Apache、 JavaScript,、PHP、 Python、Ruby、 Scala、Swift、 TypeScript など、お好みの言語でクライアントを生成することができます。

このタスクについて

クライアント・アプリケーションから意思決定サービスのルール・セットを呼び出すには、ルール・セット・パス用に生成された OpenAPI ファイルからプロキシー・クラスを作成します。

手順

  1. 意思決定サービスのルール・セットの OpenAPI ファイルを取得します。
  2. OpenAPI ファイルからプロキシー・クラスを生成します。
  3. プロキシー・クラスを使用して、クライアント・アプリケーションからルール・セットを呼び出します。

Rule Execution Serverから OpenAPI ファイルを取得するには、以下のようにします。
  1. 意思決定サービスのルール・セットを Rule Execution Serverに配布します。
  2. Rule Execution Server コンソールで、 エクスプローラー タブに移動します。
  3. 「ナビゲーター」ペインで、RuleApp をクリックし、意思決定サービスのルール・セットをクリックします。
  4. 「ルール・セット・ビュー」で、「HTDS 記述ファイルの取得」をクリックします。
  5. サービス・プロトコル・タイプとして「REST」を選択します。
  6. OpenAPI ファイルを YAML または JSON で生成するには、「OpenAPI – YAML」または「OpenAPI – JSON」をフォーマットとして選択します。
  7. 「最新のルール・セット・バージョン」「最新の RuleApp バージョン」にチェック・マークを付けて、最新バージョン用の OpenAPI ファイルを生成します。
  8. 「ダウンロード」 をクリックします。
Swagger Editor でプロキシー・クラスを生成するには、以下のようにします。
  1. Web ブラウザーで http://editor.swagger.io/ を開きます。
  2. 「ファイル」 > 「ファイルのインポート」をクリックします。
  3. 「参照」をクリックして OpenAPI ファイルを選択してから、「開く」をクリックします。
  4. インポート をクリックします。
  5. 「クライアントの生成」 > 「Java」をクリックします。
以下の Java™ コード・サンプルは、Swagger Editor でルール・セット用に生成されたプロキシー・クラスをインポートし、Java アプリケーションからルール・セットを呼び出します。
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);		
		}
	}
	
}