Sample Idle Optimizer API integration
This is an example of how to call the Idle Optimizer API from your Order Management System to integrate with Optimization service.
Invoking the API using a REST client
- Obtain the Authorization Token by completing the procedure in Accessing APIs.
- Using the access token, you can make an API call along with the header and input body. For
example:
https://<Base URL>/<tenantId>/otmz/services/idle/V1/promiseOrder
Header:"Authorization" : "Bearer [access_token]"
Invoking the API using the OMPGetExternalCostForOptionsUE
user exit
package sfo.sample;
import javax.xml.parsers.DocumentBuilderFactory;
import org.json.JSONObject;
import org.w3c.dom.Document;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.yantra.yfs.japi.YFSEnvironment;
import com.yantra.yfs.japi.YFSUserExitException;
import com.yantra.yfs.japi.ue.OMPGetExternalCostForOptionsUE;
public class CallSFOIdleAPI implements OMPGetExternalCostForOptionsUE
{
//replace with the OAuth URL provided for your provisioned account
private static final String SFO_OAUTH_URL = "https://otmz-preview.orderoptimizer.ibm.com/oauth2/endpoint/xxxxOTMZOAuthProvider/token";
//replace with the client id for your provisioned account
private static final String SFO_CLIENT_ID = "xxxxOTMZClient";
//replace with the client secret for your provisioned account
private static final String SFO_CLIENT_SECRET = "xxxx";
//replace with the URL for your provisioned account
private static final String SFO_ASYNCH_OPTIMIZATION_URL = "https://otmz-preview.orderoptimizer.ibm.com/xxxx/otmz/services/idle/V1/promiseOrder";
//call out to SFO
/**
* @param oEnv - The OMS Environment context
* @param inXML - The XML representing the Promise
* @return
* @throws YFSUserExitException
*/
public Document getExternalCostForOptions(final YFSEnvironment oEnv, final Document inXML)
throws YFSUserExitException {
Document outXML;
try {
//fetch the Bearer Token
HttpResponse<String> authTokenResponse = Unirest.post(SFO_OAUTH_URL)
.header("Content-Type", "application/x-www-form-urlencoded")
.body("grant_type=client_credentials&client_id=" + SFO_CLIENT_ID + "&client_secret=" + SFO_CLIENT_SECRET)
.asString();
String accessToken = extractToken(authTokenResponse);
//convert XML to JSON
String jsonInput = convertXMLToJson(inXML);
//populate orderDate attribute in input
//call the Optimization API
HttpResponse<String> optimizerApiResponse = Unirest
.post(SFO_ASYNCH_OPTIMIZATION_URL)
.header("Content-Type", "application/json").header("authorization", "Bearer " + accessToken).body(jsonInput)
.asString();
outXML = convertJsonToXML(optimizerApiResponse.getBody());
//populate attribute Overridden=N output XML
return outXML;
} catch (Exception ex) {
ex.printStackTrace();
throw new YFSUserExitException("Error in calling SFO.");
}
}
/**
* @param inXML - The XML to be converted to JSON
* @return The JSON string
*/
private String convertXMLToJson(Document inXML) {
String toReturn = "";
//convert XML to JSON String
return toReturn;
}
/**
* @param inXML - The JSON String to be converted to XML
* @return The XML Document
*/
private Document convertJsonToXML(String string)
throws Exception {
Document outXML = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
//convert JSON string to XML
return outXML;
}
/**
* @param authTokenResponse - The HTTP Response containing the access token
* @return The access token
*/
private String extractToken(HttpResponse<String> authTokenResponse) {
return new JSONObject(authTokenResponse.getBody()).getString("access_token");
}
}