External Script calling external API - Timed out

Hello,

I have looked at a couple different example of how to call an external API using genieacs, I have even tried the one listed in the docs to get the lat/long of the ISS but all my checks keep coming back with time out.

For reference here is what the script should return when you give it an IP,

curl --location --request GET 'http://rancid.abccomm.com/tools/lte/api.php?ip=104.36.101.255'

Response:
{“ip_addr”:“104.36.foo.bar”,“IMSI”:“302425111112882”,“custom_ICCID”:“474544”,“PlatID”:null,“code”:“OK”}

Here is my code:

const getIMSI = ext("ipam_imsi","getIMSI", CPE_IP);

/opt/genieacs/ext/ipam_imsi.js

genieacs@acs:~$ cat /opt/genieacs/ext/ipam_imsi.js
var http = require("http");

function getIMSIfunc() {
  let IP = arguments[0];
  var options = {
    'method': 'GET',
    'hostname': 'rancid.abccomm.com',
    'path': '/tools/lte/api.php?ip=' + IP,
    'headers': {
    },
    'maxRedirects': 20
  };
var req = http.request(options, function (res) {
var chunks = [];

res.on("data", function (chunk) {
  chunks.push(chunk);
});

res.on("end", function (chunk) {
  var body = Buffer.concat(chunks);
  console.log(body.toString());
      var result = JSON.parse(body.toString());
      return result;
});
req.end();
res.on("error", function (error) {
  console.error(error);
 });
});
}
exports.getIMSI = getIMSIfunc;

I get this in my cwmp-access.log

Channel has faulted; channel="test" retries=0 faultCode="timeout" faultMessage="Extension timed out"

You are missing the callback. Try this:

var http = require("http");

function getIMSIfunc(args, callback) {
    let IP = args;
    var options = {
        'hostname': 'rancid.abccomm.com',
        'path': '/tools/lte/api.php?ip=' + IP,
        'headers': {
        },
        'maxRedirects': 20
    };
    var req = http.get(options, function (res) {
        var chunks = [];

        if (res.statusCode === 404) {
            return callback(null, null);
        }

        res.on("data", function (chunk) {
            chunks.push(chunk);
        });

        res.on("end", function (chunk) {
            var body = Buffer.concat(chunks);
            console.log(body.toString());
            var result = JSON.parse(body.toString());

            callback(null, result);
        });
    });


    req.on("error", function (err) {
        callback(err);
    });

}
exports.getIMSI = getIMSIfunc;

Thank you! I could’ve swore I tried the callback but maybe was using callback(result, null) instead. Either way it works now, thank you!