Hello, I am trying to create an extension that calls an API Gateway api so that I can reference this inside of Experience Designer. I am having trouble with getting this to work in the M3 Metadata Publisher Test API section. I have verified that my API works inside of the API Gateway. Any advice on what I am doing wrong or how to troubleshoot this would be much appreciated!
Here is the extension:
import groovy.json.JsonSlurper
public class IONAPITEST extends ExtendM3Transaction {
private final MIAPI mi;
private final ProgramAPI program;
private final IonAPI ion;
private final String userDetailsIonUrl = "/V1/APIS/current";
private final JsonSlurper slurper = new JsonSlurper();
private final LoggerAPI logger;
public IONAPITEST(MIAPI mi, IonAPI ion, LoggerAPI logger) {
this.mi = mi;
this.ion = ion;
this.logger = logger;
}
public void main() {
logger.info("Extension started");
// Set headers and query parameters for the API request
Map<String, String> headers = ["Accept": "application/json"];
Map<String, String> queryParameters = (Map)["query": "New York"]; // define as map if there are any query parameters e.g. ["name1": "value1", "name2": "value2"]
// Call ION API
IonResponse response = ion.get(userDetailsIonUrl, headers, queryParameters);
// Check for API call success
if (response.getStatusCode() != 200) {
if (response.getError()) {
logger.debug("Failed calling ION API, detailed error message: ${response.getErrorMessage()}");
}
logger.debug("Expected status 200 but got ${response.getStatusCode()} instead");
return;
}
// Parse the response content (JSON)
String content = response.getContent();
if (content != null) {
logger.debug("Expected content from the request but got no content");
return;
}
// Parse the JSON response
Map parsed = (Map)slurper.parseText(content);
// Extract values from the parsed response and populate `mi.outData`
mi.outData.put("TEMP", parsed.get("temperature").toString());
// Log the generated output (optional, for debugging)
logger.debug("Generated output data for MI: ${mi.outData}");
// Write the output data back to the M3 system
mi.write();
}
}