Sample Optimizer API integration
This is an example of how to call the Optimizer API from your order management system to integrate with Fulfillment Optimizer.
Invoking the API using a REST client
- Obtain the Authorization Token by completing the procedure in Accessing the APIs.
- Using the access token, you can make an API call along with the header and input body. For
example:
https://<BaseURL>/<tenantId>/otmz/services/optimizer/V2/<tenantId>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 CallSFOOptimizerAPI 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_OPTIMIZATION_URL = "https://otmz-preview.orderoptimizer.ibm.com/xxxx/otmz/services/optimizer/V2/";
//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);
//populate orderDate attribute in input
//call the Optimization API
HttpResponse<String> optimizerApiResponse = Unirest
.post(SFO_OPTIMIZATION_URL)
.header("Content-Type", "application/xml").header("authorization", "Bearer " + accessToken).body(inXML)
.asString();
outXML = parseOutputXML(optimizerApiResponse.getBody());
//To use the Optimizer recommendation in the Order Management System, populate attribute Overridden=Y in output XML
//else populate attribute Overridden = N in output XML
return outXML;
} catch (Exception ex) {
ex.printStackTrace();
throw new YFSUserExitException("Error in calling SFO.");
}
}
/**
* @param inXML - The JSON String to be converted to XML
* @return The XML Document
*/
private Document parseOutputXML(String string)
throws Exception {
Document outXML = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
//Parse string and generate output 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");
}
}