Reading Build Version in code

I think this has been answered before but I can’t find it in search. Is there a way in Agent code to read the Build Version from the IDE? I want to log it as one of my values to track any bugs that may pop up.

Not at this point, but the request is in our task system…

Tanks!

Hey, @ljbeng, try this:

`class BuildAPI {

    // 'Constants'
    static BASE_URL = "https://build.electricimp.com/v4/";

    // Private properties
    _header = null;

    // Constructor requires a Build API key
    constructor(apiKey = null) {
        if (apiKey == null) {
            // No API key? Bail
            throw "BuildAPI class will not instantiate without an API key";
        } else {
            // Build the header for all future Build API requests
            _header = { "Authorization" : "Basic " + http.base64encode(apiKey) };
        }
    }

    // *** PUBLIC FUNCTIONS ***

    function getCurrentVersion(modelName = null) {
        // Gets and returns the latest build number for a given model
        // If there is an error, always returns -1

        if (modelName == null || (typeof modelName != "string")) {
            server.error("BuildAPI.getVersion() requires a model name passed as a string");
            return -1;
        }

        local maxBuild = -1;
        local data = _getModelsList();
        if (data == null) return -1;
        foreach (model in data.models) {
            if (model.name == modelName) {
                local result = _getLatestRevision(model.id);
                if (result == null) return -1;
                foreach (rev in result.revisions) {
                    local v = rev.version.tointeger();
                    if (v > maxBuild) maxBuild = v;
                }
                break;
            }
        }

        return maxBuild;
    }

    // **** PRIVATE FUNCTIONS - DO NOT CALL ****

    function _getModelsList() {
        return _sendGetRequest("models");
    }

    function _getLatestRevision(modelID) {
        return _sendGetRequest("models/" + modelID + "/revisions");
    }

    function _sendGetRequest(url) {
        // Issues a GET request based on the passed URL using stock header
        local result = http.get(BASE_URL + url, _header).sendsync();
        if (result.statuscode == 200) {
            return http.jsondecode(result.body);
        } else {
            if (result.statuscode == 401) {
                server.error("Build API Error: " + result.statuscode + " - Unrecognised API key");
            } else {
                // TODO Handlers for common errors
                server.error("Build API Error: " + result.statuscode + " - " + result.body);
            }

            return null;
        }
    }
}
`

Add the class to your program then use with this code:

`local build = BuildAPI("<YOUR_BUILD_API_KEY>");
local modelName = "<YOUR_MODEL_NAME>";
local latest = build.getCurrentVersion(modelName);
if (latest != -1) {
    server.log("Model: " + modelName);
    server.log("Build: " + latest);
}`

You can get a Build API key via the IDE

Oh, I admire your sneakiness Tony!

Sweet! That worked great. I changed
if (version..

to

if(latest...

Works like a charm :wink:
Thanks!

Update You can speed up the code a bit by making this change in getCurrentVersion()’s main foreach loop:

`foreach (model in data.models) {
    if (model.name == modelName) {
        local result = _getLatestRevision(model.id);
        if (result == null) return null;
        maxBuild = result.revisions.len();
        break;
    }
}`

That gets the latest version not the current version.

Hey guys,

Tried it out but get
2016-04-21 20:29:26 UTC-4 [Device] ERROR: the index 'http' does not exist 2016-04-21 20:29:26 UTC-4 [Device] ERROR: at constructor:16 2016-04-21 20:29:26 UTC-4 [Device] ERROR: from main:115

The http object is only available in the agent.

OK, got it changed to the agent and it is working find.

Thanks overdriven!!!

Is this still the preferred way to do this? Or is there an 'imp’roved way?

Seems like a good bit of data to be in imp.configparams…