Utilización de la API REST para llamar a un conjunto de reglas
La aplicación cliente llama a un conjunto de reglas de servicio de decisiones a través de la API REST. Utiliza los comandos HTTP estándar
GET, POST, PUT, y DELETE. Antes de empezar
La aplicación cliente debe pasar las credenciales de autenticación para el portal de nube.
Acerca de esta tarea
El URI de punto final del conjunto de reglas en el servidor tiene el aspecto siguiente:
https://<vhostname>.bpm.ibmcloud.com/odm/<odm_on_cloud_environment>/DecisionService/rest/v1/<ruleset_path>La lista siguiente explica las partes del URI:
- <vhostname>: sirve como nombre del host para el servicio de decisiones.
- <odm_on_cloud_environment>: utiliza dev, test o prod en función de si el conjunto de reglas del servicio de decisiones está en el entorno de desarrollo, de prueba o de producción.
- <ruleset_path>: proporciona la vía de acceso para la ejecución de la regla de servicio de decisiones y debe tener uno de los formatos siguientes:
- ruleAppName/rulesetName
- ruleAppName/ruleAppVersion/rulesetName
- ruleAppName/ruleAppVersion/rulesetName/rulesetVersion
El ejemplo siguiente muestra un URI para MiniloanServiceRuleset:
https://vhost005.bpm.ibmcloud.com/odm/dev/DecisionService/rest/v1/
MiniloanService/MiniloanServiceRulesetLa ejecución del conjunto de reglas se ejecuta a través de un POST de tipo " HTTP " en el URI de punto final especificado. El nombre de usuario y la contraseña para la autenticación se pasan con una cabecera de autorización (consulte Autenticación para invocación REST y SOAP).Ejemplo
MiniloanServiceRuleset en el servidor. El ejemplo utiliza 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();
}
}
}